123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using Luticate2.Auth.Business.Fields;
- using Luticate2.Auth.Business.Fields.DMEC;
- using Luticate2.Auth.Dbo;
- using Luticate2.Auth.Dbo.Fields;
- using Luticate2.Auth.Dbo.Pagination;
- using Luticate2.Auth.Dbo.PartialObjectCopier;
- using Luticate2.Auth.Dbo.Result;
- using Luticate2.Auth.Interfaces;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.DependencyInjection;
-
- namespace Luticate2.Auth.Business.Crud
- {
- public class LuEfCrudBusiness<TDbo, TModel, TDbContext> : ILuCrud<TDbo>
- where TModel : class
- where TDbContext : DbContext
- where TDbo : new()
- {
- protected IServiceProvider ServiceProvider { get; }
-
- public LuEfCrudBusiness(IServiceProvider serviceProvider)
- {
- ServiceProvider = serviceProvider;
- }
-
- protected virtual LuResult<TModel> ConvertDboToModel(LuPartialFieldsDbo partialInput, TDbo dbo, TModel model)
- {
- var copier = ServiceProvider.GetService<ILuPartialObjectCopier<TDbo, TModel>>();
- if (copier == null)
- {
- return LuResult<TModel>.Error(LuStatus.InternalError.ToInt(), $"Could not get service: ILuPartialObjectCopier<{typeof(TDbo).Name}, {typeof(TModel).Name}>");
- }
- var result = copier.Copy(partialInput, dbo, model, new LuPartialObjectCopierOptions());
- return result;
- }
-
- protected virtual LuResult<TDbo> ConvertModelToDbo(LuPartialFieldsDbo partialResponse, TModel model, TDbo dbo)
- {
- var copier = ServiceProvider.GetService<ILuPartialObjectCopier<TModel, TDbo>>();
- if (copier == null)
- {
- return LuResult<TDbo>.Error(LuStatus.InternalError.ToInt(), $"Could not get service: ILuPartialObjectCopier<{typeof(TModel).Name}, {typeof(TDbo).Name}>");
- }
- var result = copier.Copy(partialResponse, model, dbo, new LuPartialObjectCopierOptions());
- return result;
- }
-
- protected virtual LuResult<T> HandleError<T>(Exception e)
- {
- return LuResult<T>.Error(LuStatus.DbError.ToInt(), e);
- }
-
- protected LuResult<T> Execute<T>(Func<TDbContext, DbSet<TModel>, LuResult<T>> action)
- {
- try
- {
- using (var db = ServiceProvider.GetService<TDbContext>())
- {
- return action(db, db.Set<TModel>());
- }
- }
- catch (Exception e)
- {
- var result = HandleError<T>(e);
- return result;
- }
- }
-
- protected virtual LuResult<IQueryable<TModel>> Include(LuPartialFieldsDbo partialResponse, IQueryable<TModel> queryable)
- {
- return LuResult<IQueryable<TModel>>.Ok(queryable);
- }
-
- protected virtual LuResult<IQueryable<TModel>> Filter(LuFilterDbo filter, IQueryable<TModel> queryable)
- {
- var lamdbaDbo = (Expression<Func<TDbo, bool>>) filter.Expression;
-
- var options = new LuExpressionConverterOptions
- {
- Parameter = Expression.Parameter(typeof(TModel), "x")
- };
- var converter = new LuExpressionConverterVisitor(options, ServiceProvider);
- var result = converter.Visit(lamdbaDbo.Body);
- var lamdbaModel = result != null ? Expression.Lambda<Func<TModel, bool>>(result, options.Parameter) : null;
- return LuResult<IQueryable<TModel>>.Ok(queryable.Where(lamdbaModel));
-
- // var converter = ServiceProvider.GetService<ILuExpressionConverter<TDbo, TModel>>();
- // if (converter == null)
- // {
- // return LuResult<IQueryable<TModel>>.Error(LuStatus.InternalError.ToInt(), $"Could not get service: ILuDboModelExpressionConverter<{typeof(TModel).Name}, {typeof(TDbo).Name}>");
- // }
- //
- // var lamdba = (Expression<Func<TDbo, bool>>) filter.Expression;
- // var options = new LuDMECOptions
- // {
- // Parameter = Expression.Parameter(typeof(TModel), lamdba.Parameters[0].Name)
- // };
- // var convertResult = converter.ConvertExpression(lamdba.Body, options);
- // if (!convertResult)
- // {
- // return convertResult.To<IQueryable<TModel>>();
- // }
- //
- // var lambdaNoConvert = Expression.Lambda<Func<TModel, bool>>(convertResult.Data, options.Parameter);
- //
- // return LuResult<IQueryable<TModel>>.Ok(queryable.Where(lambdaNoConvert));
- return LuResult<IQueryable<TModel>>.Ok(queryable);
- }
-
- protected virtual LuResult<IQueryable<TModel>> OrderByField(LuOrderByFieldDbo orderByfield, IQueryable<TModel> queryable)
- {
- // var expressionBuilder = ServiceProvider.GetService<ILuDboModelExpressionConverter<TDbo, TModel>>();
- // var expressionResult = expressionBuilder.ConvertLamdba<TModel>(orderByfield.);
- // if (!expressionResult)
- // {
- // return expressionResult.To<IQueryable<TModel>>();
- // }
- //
- // var expression = expressionResult.Data;
- // var expressionNoConvert = LuExpressionUtils.GetFromConvert(expression);
- //
- // var ordered = queryable.OrderBy(expressionNoConvert);
- //
- // return LuResult<IQueryable<TModel>>.Ok(ordered);
- return LuResult<IQueryable<TModel>>.Ok(queryable);
- }
-
- protected virtual LuResult<IQueryable<TModel>> OrderBy(LuOrderByDbo orderBy, IQueryable<TModel> queryable)
- {
- var ordered = queryable;
- foreach (var field in orderBy.OrderByFields)
- {
- var orderedResult = OrderByField(field, ordered);
- if (!orderedResult)
- {
- return orderedResult.To<IQueryable<TModel>>();
- }
-
- ordered = orderedResult.Data;
- }
-
- return LuResult<IQueryable<TModel>>.Ok(ordered ?? queryable);
- }
-
- protected virtual LuResult<IQueryable<TModel>> Paginate(int page, int perPage, IQueryable<TModel> queryable)
- {
- var paginated = queryable.Skip(page * perPage).Take(perPage);
- return LuResult<IQueryable<TModel>>.Ok(paginated);
- }
-
- public virtual LuResult<IList<TDbo>> Create(LuPartialFieldsDbo partialResponse, LuPartialFieldsDbo partialInput, IEnumerable<TDbo> dbos)
- {
- throw new NotImplementedException();
- }
-
- public virtual LuResult<LuPaginatedDbo<TDbo>> Read(LuPartialFieldsDbo partialResponse, LuPaginatedParamsDbo paginationParams)
- {
- var result = Execute((context, set) =>
- {
- var includeResult = Include(partialResponse, set);
- if (!includeResult)
- {
- return includeResult.To<LuPaginatedDbo<TDbo>>();
- }
-
- var included = includeResult.Data;
-
- var orderByResult = OrderBy(paginationParams.OrderBy, included);
- if (!orderByResult)
- {
- return orderByResult.To<LuPaginatedDbo<TDbo>>();
- }
-
- var ordered = orderByResult.Data;
-
- var filteredResult = Filter(paginationParams.Filter, ordered);
- if (!filteredResult)
- {
- return filteredResult.To<LuPaginatedDbo<TDbo>>();
- }
-
- var filtered = filteredResult.Data;
- var count = filtered.Count();
-
- var paginatedResult = Paginate(paginationParams.Page, paginationParams.PerPage, filtered);
- if (!paginatedResult)
- {
- return paginatedResult.To<LuPaginatedDbo<TDbo>>();
- }
-
- var paginated = paginatedResult.Data;
-
- var data = paginated.AsEnumerable().Select(model =>
- {
- var dbo = new TDbo();
- ConvertModelToDbo(partialResponse, model, dbo);
- return dbo;
- }).ToList();
-
- return LuResult<LuPaginatedDbo<TDbo>>.Ok(new LuPaginatedDbo<TDbo>
- {
- Count = count,
- Data = data
- });
- });
- return result;
- }
-
- public virtual LuResult<IList<TDbo>> Update(LuPartialFieldsDbo partialResponse, LuPartialFieldsDbo partialInput, IEnumerable<TDbo> dbos)
- {
- throw new NotImplementedException();
- }
-
- public virtual LuResult<IList<TDbo>> Delete(LuPartialFieldsDbo partialResponse, LuPartialFieldsDbo partialInput, IEnumerable<TDbo> dbos)
- {
- throw new NotImplementedException();
- }
- }
- }
|