SearchLogic extended
This commit is contained in:
		| @@ -10,6 +10,7 @@ using Newtonsoft.Json; | ||||
| using System.Linq; | ||||
| using System.Linq.Expressions; | ||||
| using SearchApi.Exceptions; | ||||
| using Datamodels.Enums; | ||||
|  | ||||
| namespace SearchApi.SearchLogic | ||||
| { | ||||
| @@ -18,11 +19,11 @@ namespace SearchApi.SearchLogic | ||||
|         private readonly ILogger<SearchLogic> _logger; | ||||
|         private readonly IFwMariaSearchRepo _searchRepo; | ||||
|  | ||||
|         private List<string> _tableNames; | ||||
|         private List<string> _joinConditions; | ||||
|         private List<string> _fieldNames; | ||||
|         private List<string> _whereConditions; | ||||
|         private List<string> _sortFields; | ||||
|         private List<string> _tableNames = new List<string>(); | ||||
|         private List<string> _joinConditions = new List<string>(); | ||||
|         private List<string> _fieldNames = new List<string>(); | ||||
|         private List<string> _whereConditions = new List<string>(); | ||||
|         private List<string> _sortFields = new List<string>(); | ||||
|          | ||||
|         public SearchLogic(ILogger<SearchLogic> logger, IFwMariaSearchRepo searchRepo) | ||||
|         { | ||||
| @@ -41,7 +42,7 @@ namespace SearchApi.SearchLogic | ||||
|             var result = new DataResult<ResultModel>(); | ||||
|             result.Data = new ResultModel(); | ||||
|             result.Data.SearchModel = filter; | ||||
|  | ||||
|             var sql = await Task.Run(() => { return AssembleSqlQuery(filter); } ); | ||||
|             return result; | ||||
|         } | ||||
|  | ||||
| @@ -73,6 +74,7 @@ namespace SearchApi.SearchLogic | ||||
|                         throw new FieldAliasMissingException($"No Alias defined for Field {field.FieldName} in Table {table.TableName}"); | ||||
|                     } | ||||
|                     _fieldNames.Add($"{table.TableAlias}.{field.FieldName} {field.FieldAlias}"); | ||||
|  | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| @@ -84,11 +86,63 @@ namespace SearchApi.SearchLogic | ||||
|         } | ||||
|  | ||||
|         // Extract Where Conditions | ||||
|         private void GetWhereConditions(FilterModel filter) | ||||
|         // ToDo: wenn es mehrere Values gibt, werden die generell mit OR verknüpft? Oder nehmen wir lieber AND? | ||||
|         private void GetWhereConditions(string tableName, FieldModel field) | ||||
|         { | ||||
|             if (field.SearchDescriptions.Count > 0) | ||||
|             { | ||||
|                 foreach (var condition in field.SearchDescriptions) | ||||
|                 { | ||||
|                     var op = Operator(condition.SearchData.PhraseOperator); | ||||
|                     if (op != "NOTHING") | ||||
|                     { | ||||
|                         var usedField = $"{tableName}.{field.FieldName}{op}{condition.SearchData.Values[0]}"; | ||||
|                         _whereConditions.Add(usedField); | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         if (condition.SearchData.PhraseOperator == PhraseOperators.Contains) | ||||
|                         { | ||||
|                             var usedField = $"{tableName}.{field.FieldName} like '%{condition.SearchData.Values[0]}%'"; | ||||
|                             _whereConditions.Add(usedField); | ||||
|                         } | ||||
|                         if (condition.SearchData.PhraseOperator == PhraseOperators.StartsWith) | ||||
|                         { | ||||
|                             var usedField = $"{tableName}.{field.FieldName} like '%{condition.SearchData.Values[0]}'"; | ||||
|                             _whereConditions.Add(usedField); | ||||
|                         } | ||||
|                         if (condition.SearchData.PhraseOperator == PhraseOperators.EndsWith) | ||||
|                         { | ||||
|                             var usedField = $"{tableName}.{field.FieldName} like '{condition.SearchData.Values[0]} + %'"; | ||||
|                             _whereConditions.Add(usedField); | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|         } | ||||
|  | ||||
|         public string Operator(PhraseOperators ops) | ||||
|         { | ||||
|             switch (ops) | ||||
|             { | ||||
|                 case PhraseOperators.Equal: | ||||
|                     return "="; | ||||
|                 case PhraseOperators.NotEqual: | ||||
|                     return "!="; | ||||
|                 case PhraseOperators.GreaterThan: | ||||
|                     return ">"; | ||||
|                 case PhraseOperators.GreaterThanOrEqual: | ||||
|                     return ">="; | ||||
|                 case PhraseOperators.LowerThan: | ||||
|                     return "<"; | ||||
|                 case PhraseOperators.LowerThanOrEqual: | ||||
|                     return "<="; | ||||
|                 default: | ||||
|                     return "NOTHING"; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // Extract Sort Items | ||||
|         private void GetSortFields(FilterModel filter) | ||||
|         { | ||||
| @@ -96,9 +150,35 @@ namespace SearchApi.SearchLogic | ||||
|         } | ||||
|  | ||||
|         // Assemble SQL Query | ||||
|         private string AssembleSqlQuery() | ||||
|         private string AssembleSqlQuery(FilterModel filter) | ||||
|         { | ||||
|             return null; | ||||
|             GetTables(filter); | ||||
|             GetFields(filter); | ||||
|             var result = BuildSql(); | ||||
|             return result; | ||||
|         } | ||||
|  | ||||
|         //Build SQL String itself | ||||
|         private string BuildSql() | ||||
|         { | ||||
|             var sql = "SELECT "; | ||||
|             foreach (var field in _fieldNames) | ||||
|             { | ||||
|                 sql += field.ToUpper(); | ||||
|                 if (_fieldNames.IndexOf(field) < _fieldNames.Count - 1) sql += ","; | ||||
|             } | ||||
|  | ||||
|             sql += " FROM "; | ||||
|             foreach (var table in _tableNames) | ||||
|             { | ||||
|                 sql += table.ToUpper(); | ||||
|                 if (_tableNames.IndexOf(table) < _tableNames.Count - 1) | ||||
|                 { | ||||
|                     sql += ","; | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             return sql; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user