SearchLogic extended
This commit is contained in:
		| @@ -1,8 +1,8 @@ | |||||||
| <Project Sdk="Microsoft.NET.Sdk"> | <Project Sdk="Microsoft.NET.Sdk"> | ||||||
| 	<ItemGroup> | 	<ItemGroup> | ||||||
| 		<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-rc.2.21480.5" /> | 		<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" /> | ||||||
| 		<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0-rc.2.21480.5" /> | 		<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||||||
| 		<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-rc.2.21480.5" /> | 		<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" /> | ||||||
| 	</ItemGroup> | 	</ItemGroup> | ||||||
| 	<ItemGroup> | 	<ItemGroup> | ||||||
| 		<ProjectReference Include="..\..\Datamodels\Datamodels.csproj" /> | 		<ProjectReference Include="..\..\Datamodels\Datamodels.csproj" /> | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ using Newtonsoft.Json; | |||||||
| using System.Linq; | using System.Linq; | ||||||
| using System.Linq.Expressions; | using System.Linq.Expressions; | ||||||
| using SearchApi.Exceptions; | using SearchApi.Exceptions; | ||||||
|  | using Datamodels.Enums; | ||||||
|  |  | ||||||
| namespace SearchApi.SearchLogic | namespace SearchApi.SearchLogic | ||||||
| { | { | ||||||
| @@ -18,11 +19,11 @@ namespace SearchApi.SearchLogic | |||||||
|         private readonly ILogger<SearchLogic> _logger; |         private readonly ILogger<SearchLogic> _logger; | ||||||
|         private readonly IFwMariaSearchRepo _searchRepo; |         private readonly IFwMariaSearchRepo _searchRepo; | ||||||
|  |  | ||||||
|         private List<string> _tableNames; |         private List<string> _tableNames = new List<string>(); | ||||||
|         private List<string> _joinConditions; |         private List<string> _joinConditions = new List<string>(); | ||||||
|         private List<string> _fieldNames; |         private List<string> _fieldNames = new List<string>(); | ||||||
|         private List<string> _whereConditions; |         private List<string> _whereConditions = new List<string>(); | ||||||
|         private List<string> _sortFields; |         private List<string> _sortFields = new List<string>(); | ||||||
|          |          | ||||||
|         public SearchLogic(ILogger<SearchLogic> logger, IFwMariaSearchRepo searchRepo) |         public SearchLogic(ILogger<SearchLogic> logger, IFwMariaSearchRepo searchRepo) | ||||||
|         { |         { | ||||||
| @@ -41,7 +42,7 @@ namespace SearchApi.SearchLogic | |||||||
|             var result = new DataResult<ResultModel>(); |             var result = new DataResult<ResultModel>(); | ||||||
|             result.Data = new ResultModel(); |             result.Data = new ResultModel(); | ||||||
|             result.Data.SearchModel = filter; |             result.Data.SearchModel = filter; | ||||||
|  |             var sql = await Task.Run(() => { return AssembleSqlQuery(filter); } ); | ||||||
|             return result; |             return result; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -73,6 +74,7 @@ namespace SearchApi.SearchLogic | |||||||
|                         throw new FieldAliasMissingException($"No Alias defined for Field {field.FieldName} in Table {table.TableName}"); |                         throw new FieldAliasMissingException($"No Alias defined for Field {field.FieldName} in Table {table.TableName}"); | ||||||
|                     } |                     } | ||||||
|                     _fieldNames.Add($"{table.TableAlias}.{field.FieldName} {field.FieldAlias}"); |                     _fieldNames.Add($"{table.TableAlias}.{field.FieldName} {field.FieldAlias}"); | ||||||
|  |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| @@ -84,11 +86,63 @@ namespace SearchApi.SearchLogic | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         // Extract Where Conditions |         // 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 |         // Extract Sort Items | ||||||
|         private void GetSortFields(FilterModel filter) |         private void GetSortFields(FilterModel filter) | ||||||
|         { |         { | ||||||
| @@ -96,9 +150,35 @@ namespace SearchApi.SearchLogic | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         // Assemble SQL Query |         // 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; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ | |||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0-rc.2.21480.5" /> |     <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||||||
|     <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> |     <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,15 +3,15 @@ | |||||||
| 		<ProjectReference Include="..\Datamodels\Datamodels.csproj" /> | 		<ProjectReference Include="..\Datamodels\Datamodels.csproj" /> | ||||||
| 	</ItemGroup> | 	</ItemGroup> | ||||||
| 	<ItemGroup> | 	<ItemGroup> | ||||||
| 		<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-rc.2.21480.5" /> | 		<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" /> | ||||||
| 		<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-rc.2.21480.5"> | 		<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0"> | ||||||
| 			<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | 			<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||||||
| 			<PrivateAssets>all</PrivateAssets> | 			<PrivateAssets>all</PrivateAssets> | ||||||
| 		</PackageReference> | 		</PackageReference> | ||||||
| 		<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0-rc.2.21480.5" /> | 		<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||||||
| 		<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0-preview.7" /> | 		<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" /> | ||||||
| 		<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-rc.2.21480.5" /> | 		<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" /> | ||||||
| 		<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-rc.2.21480.5" /> | 		<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" /> | ||||||
| 	</ItemGroup> | 	</ItemGroup> | ||||||
| 	<PropertyGroup> | 	<PropertyGroup> | ||||||
| 		<TargetFramework>net6.0</TargetFramework> | 		<TargetFramework>net6.0</TargetFramework> | ||||||
|   | |||||||
| @@ -6,15 +6,15 @@ | |||||||
| 	<ItemGroup> | 	<ItemGroup> | ||||||
| 		<PackageReference Include="Elastic.Apm.EntityFrameworkCore" Version="1.11.1" /> | 		<PackageReference Include="Elastic.Apm.EntityFrameworkCore" Version="1.11.1" /> | ||||||
| 		<PackageReference Include="Elastic.Apm.NetCoreAll" Version="1.11.1" /> | 		<PackageReference Include="Elastic.Apm.NetCoreAll" Version="1.11.1" /> | ||||||
| 		<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0-rc.2.21480.10" /> | 		<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" /> | ||||||
| 		<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0-rc.2.21480.5" /> | 		<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||||||
| 		<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> | 		<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> | ||||||
| 		<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta2" /> | 		<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta2" /> | ||||||
| 		<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.1.0" /> | 		<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.1.0" /> | ||||||
| 		<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.1.0" /> | 		<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.1.0" /> | ||||||
| 		<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc7" /> | 		<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc8" /> | ||||||
| 		<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc7" /> | 		<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc8" /> | ||||||
| 		<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc7" /> | 		<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc8" /> | ||||||
| 		<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" /> | 		<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" /> | ||||||
| 		<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" /> | 		<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" /> | ||||||
| 		<PackageReference Include="Serilog.Enrichers.ExceptionStackTraceHash" Version="1.3.0" /> | 		<PackageReference Include="Serilog.Enrichers.ExceptionStackTraceHash" Version="1.3.0" /> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user