First idea of ResultModel

This commit is contained in:
Roland Fieger
2021-08-28 15:40:03 +02:00
parent 2de05c3856
commit 3b5ae86ce5
21 changed files with 139 additions and 187 deletions

View File

@@ -11,6 +11,6 @@ namespace SearchLogic
public interface ISearchLogic
{
Task<DataResult<List<Address>>> GetAllAddresses();
Task<DataResult<List<IEntityClass>>> GetFilteredData(FilterModel filter, long skip = 0, long take = -1);
Task<DataResult<ResultModel>> GetFilteredData(FilterModel filter, long skip = 0, long take = -1);
}
}

View File

@@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging;
using Repositories.Interfaces;
using Datamodels.BusinessModels;
using Datamodels.SearchModels;
using Newtonsoft.Json;
using System.Linq;
using System.Linq.Expressions;
@@ -28,41 +29,13 @@ namespace SearchLogic
return result;
}
public async Task<DataResult<List<IEntityClass>>> GetFilteredData(FilterModel filter, long skip = 0, long take = -1)
public async Task<DataResult<ResultModel>> GetFilteredData(FilterModel filter, long skip = 0, long take = -1)
{
List<Expression> expList = new List<Expression>();
var result = new DataResult<ResultModel>();
result.Data = new ResultModel();
result.Data.SearchModel = filter;
foreach (SearchDescription filterModel in filter.SearchDescriptions)
{
var exp = GetFilterCondition<Address>(filterModel);
if (exp != null)
{
expList.Add(exp);
}
}
var result = await _searchRepo.GetFilteredData<Address>(expList, 0, -1);
return result;
}
private Expression GetFilterCondition<T>(SearchDescription searchDescription) where T : IEntityClass
{
Expression<Func<T, bool>> predicate;
ParameterExpression pe = Expression.Parameter(typeof(T), typeof(T).Name);
if (typeof(T).GetProperty(searchDescription.SearchData.FieldName) != null)
{
//var exp = Expression.Parameter(typeof(T), "x");
var exp = Expression.Parameter(typeof(T), typeof(T).Name);
Expression left = Expression.Property(pe, searchDescription.SearchData.FieldName);
Expression right = Expression.Constant(searchDescription.SearchData.Values[0]);
Expression e1 = Expression.Equal(left, right);
//predicate = Expression.Lambda<Func<T, bool>>(e1, new ParameterExpression[] { pe });
return e1;
}
return null;
}
}
}

View File

@@ -4,6 +4,10 @@
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Datamodels\Datamodels.csproj" />
<ProjectReference Include="..\..\Repositories\Repositories.csproj" />

View File

@@ -29,5 +29,6 @@ namespace Datamodels.BusinessModels
public long TotalCount { get; set; }
public long Skip { get; set; }
public long Take { get; set; }
public TimeSpan Duration { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.Enums
{
public enum FieldTypes
{
String,
Integer,
Double,
Boolean,
DateTime,
Currency,
Blob
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.Enums
{
public enum JoinTypes
{
And,
Or
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.Enums
{
public enum PhraseOperators
{
Equal,
NotEqual,
LowerThan,
LowerThanOrEqual,
GreaterThan,
GreaterThanOrEqual,
StartsWith,
Contains,
EndsWith
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.Enums
{
public enum SearchTypes
{
Phrase,
Range,
Terms
}
}

View File

@@ -1,21 +1,15 @@
using Datamodels.Enums;
using System;
using System.Collections.Generic;
namespace Datamodels.SearchModels
{
public enum FieldTypes
{
String,
Integer,
Double,
Boolean,
DateTime,
Currency,
Blob
}
public sealed class FieldModel
{
public string FieldName { get; set; }
public string FieldAlias { get; set; }
public FieldTypes FieldType { get; set; }
public List<SearchDescription> SearchDescriptions { get; set; }
}
}

View File

@@ -6,8 +6,7 @@ namespace Datamodels.SearchModels
{
public class FilterModel
{
public TableModel TableName { get; set; }
public List<SearchDescription> SearchDescriptions { get; set; }
public List<TableModel> TableDescriptions { get; set; }
public long Skip { get; set; }
public long Take { get; set; }
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.SearchModels
{
public class ResultDataModel
{
public List<ResultRowModel> ResultRows { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using Datamodels.Enums;
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.SearchModels
{
public class ResultFieldModel
{
public FieldTypes FieldType { get; set; }
public string FieldContent { get; set;
public int FieldContentInt { get; set; }
public DateTime FieldContentDate { get; set; }
public Decimal FieldContentDecimal { get; set; }
public float FieldContentFloat { get; set; }
public string FieldContentString { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.SearchModels
{
public class ResultModel
{
public FilterModel SearchModel { get; set; }
public ResultDataModel ResultData { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.SearchModels
{
public class ResultRowModel
{
public List<ResultFieldModel> RowFields { get; set; }
}
}

View File

@@ -1,17 +1,13 @@
using System;
using Datamodels.Enums;
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.SearchModels
{
public enum JoinTypes
{
And,
Or
}
public class SearchDescription
{
public string TableName { get; set; }
public SearchObject SearchData { get; set; }
public JoinTypes JoinType { get; set; }

View File

@@ -1,33 +1,14 @@
using System;
using Datamodels.Enums;
using System;
using System.Collections.Generic;
using System.Text;
namespace Datamodels.SearchModels
{
public enum SearchTypes
{
Phrase,
Range,
Terms
}
public enum PhraseOperators
{
Equal,
NotEqual,
LowerThan,
LowerThanOrEqual,
GreaterThan,
GreaterThanOrEqual,
StartsWith,
Contains,
EndsWith
}
public class SearchObject
{
public List<string> Values { get; set; }
public string FieldName { get; set; }
public SearchTypes SearchType { get; set; }
public PhraseOperators PhraseOperator { get; set; }

View File

@@ -6,6 +6,7 @@ namespace Datamodels.SearchModels
public sealed class TableModel
{
public string TableName { get; set; }
public string TableAlias { get; set; }
public List<FieldModel> Fields { get; set; } = new List<FieldModel>();
}
}

View File

@@ -80,32 +80,7 @@ namespace Repositories
public async Task<DataResult<List<IEntityClass>>> GetFilteredData<T>(List<Expression> expressions, long skip = 0, long take = -1) where T : IEntityClass
{
var where = FilterLinq<T>.GetWherePredicate(wherefield, wherefieldvalue).Compile();
items = await _dbContext.Address.Where((Expression<Func<Address, bool>>)where).ToListAsync();
var result = new DataResult<List<IEntityClass>>();
if (typeof(T).Equals(typeof(Address)))
{
Expression combined = null;
foreach (var exp in expressions)
{
if (combined == null)
{
combined = exp;
}
else
{
combined = Expression.Or(combined, exp);
}
}
ParameterExpression pe = Expression.Parameter(typeof(T), typeof(T).Name);
combined = Expression.Lambda<Func<T, Boolean>>(combined, new ParameterExpression[] { pe });
//var addressFound = await _dbContext.Address.Where((Expression<Func<Address, bool>>)combined).ToListAsync();
var addressFound = await _dbContext.Address.Where(a => a.City == "Augsburg" | a.City == "München").ToListAsync();
result.Data = new List<IEntityClass>(addressFound.Cast<IEntityClass>());
}
return result;
throw new NotImplementedException();
}
}
@@ -113,39 +88,7 @@ namespace Repositories
{
public static Expression<Func<T, Boolean>> GetWherePredicate(string whereFieldList, string whereFieldValues)
{
//the 'IN' parameter for expression ie T=> condition
ParameterExpression pe = Expression.Parameter(typeof(T), typeof(T).Name);
//combine them with and 1=1 Like no expression
Expression combined = null;
if (whereFieldList != null)
{
string[] field = whereFieldList.Split(';');
string[] fieldValue = whereFieldValues.Split(';');
for (int i = 0; i < field.Count(); i++)
{
//Expression for accessing Fields name property
Expression columnNameProperty = Expression.Property(pe, field[i]);
//the name constant to match
Expression columnValue = Expression.Constant(fieldValue[i]);
//the first expression: PatientantLastName = ?
Expression e1 = Expression.Equal(columnNameProperty, columnValue);
if (combined == null)
{
combined = e1;
}
else
{
combined = Expression.And(combined, e1);
}
}
}
//create and return the predicate
return Expression.Lambda<Func<T, Boolean>>(combined, new ParameterExpression[] { pe });
throw new NotImplementedException();
}
}
}

View File

@@ -1,39 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FeatureWerkAPIs.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View File

@@ -1,15 +0,0 @@
using System;
namespace FeatureWerkAPIs
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
}

View File

@@ -7,7 +7,7 @@
}
},
"ConnectionStrings": {
"mariadb": "SERVER=127.0.0.1;DATABASE=testdb;PORT=3306;USER=root;PASSWORD=example",
"mariadb": "SERVER=localhost;DATABASE=textor_test;PORT=3333;USER=root;PASSWORD=$64,rf1209",
"postgres": "",
"sqlserver": "",
"oracle": ""