Initial
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
using Datamodels.BusinessModels;
|
||||
using Datamodels.DatabaseModels;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Repositories.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Datamodels.SearchModels;
|
||||
using System.Linq.Expressions;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Repositories
|
||||
{
|
||||
public class FwMariaSearchRepo : IFwMariaSearchRepo
|
||||
{
|
||||
private readonly FwDbContext _dbContext = new FwDbContext();
|
||||
private readonly ILogger<FwMariaSearchRepo> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public FwMariaSearchRepo(ILogger<FwMariaSearchRepo> logger, IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
try
|
||||
{
|
||||
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
|
||||
{
|
||||
_dbContext._connectionString = "SERVER=127.0.0.1;DATABASE=testdb;PORT=3306;USER=root;PASSWORD=example";
|
||||
}
|
||||
else
|
||||
{
|
||||
string usedDb = configuration.GetSection("GeneralSettings").GetSection("DbToUse").Get<string>();
|
||||
string connectionString = configuration.GetConnectionString(usedDb);
|
||||
_dbContext._connectionString = connectionString;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Database Connection Configuration not valid");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DataResult<List<Address>>> GetAllAddresses()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _dbContext.Address.Where(x => x.City == "Augsburg" || x.City == "München").Where(y => y.StreetName.Contains("bruck")).ToListAsync().ConfigureAwait(false);
|
||||
return new DataResult<List<Address>> { Data = result };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new DataResult<List<Address>>
|
||||
{
|
||||
GeneralResult = GeneralResults.DatabaseError,
|
||||
UserMessage = "Fehler beim Laden der Adressen",
|
||||
ServerMessage = ex.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public Task<object> GetAllSortedData(TableModel queryFieldDef, List<SortModel> sortModel)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<string> GetBlobData(string tableName, string fieldName, long id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<object> GetDataFilteredAndSorted(List<FilterModel> filter, List<SortModel> sortModel, long skip = 0, long take = -1)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public class FilterLinq<T>
|
||||
{
|
||||
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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user