123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using Luticate2.Utils.Dbo.Basic;
- using Luticate2.Utils.Dbo.Filter;
- using Luticate2.Utils.Dbo.OrderBy;
- using Luticate2.Utils.Dbo.Result;
- using Luticate2.Utils.Interfaces;
- using Luticate2.Utils.Utils;
- using Microsoft.EntityFrameworkCore;
-
- namespace Luticate2.Utils.DataAccess
- {
- public abstract class LuEfCrudDataAccess<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext, TId> :
- LuEfDataAccess<TModel, TDbContext>,
- ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
- where TModel : class
- where TDboCreate : class
- where TDboRead : class
- where TDboUpdate : class
- where TDbContext : DbContext
- {
- protected LuEfCrudDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
- {
- }
-
- protected abstract TModel GetModelFromTCreate(TDboCreate obj);
-
- protected abstract void EditModelFromTUpdate(TDboUpdate obj, TModel model);
-
- protected abstract TDboRead GetDboFromModel(TModel model);
-
- protected virtual Expression<Func<TModel, object>> GetOrderByFieldExpression(string fieldName)
- {
- fieldName = fieldName.ToSnakeCase();
- if (!typeof(TModel).HasProperty(fieldName))
- {
- return null;
- }
- var param = Expression.Parameter(typeof(TModel), "x");
- var prop = Expression.Property(param, fieldName);
- var converted = Expression.Convert(prop, typeof(object));
- var exp = Expression.Lambda<Func<TModel, object>>(converted, param);
- return exp;
- }
-
- protected virtual Expression<Func<TModel, bool>> GetFilterExpression(LuFilterDbo filter)
- {
- return model => true;
- }
-
- protected virtual object GetId(TId id)
- {
- var s = id as string;
- if (s != null)
- {
- Guid guid;
- if (Guid.TryParse(s, out guid))
- {
- return guid;
- }
- return null;
-
- }
- return id;
- }
-
- protected virtual object GetModelId(object id)
- {
- var g = id as Guid?;
- if (g != null)
- {
- return g.ToString();
- }
- return id;
- }
-
- protected virtual IQueryable<TModel> GetGetQueryable(TDbContext db, DbSet<TModel> table)
- {
- return table;
- }
-
- protected virtual IQueryable<TModel> GetEditQueryable(TDbContext db, DbSet<TModel> table)
- {
- return table;
- }
-
- protected virtual IQueryable<TModel> GetDeleteQueryable(TDbContext db, DbSet<TModel> table)
- {
- return table;
- }
-
- protected virtual Func<TDboRead, T> GetIdFunc<T>()
- {
- return x => ((dynamic) x).Id;
- }
-
- protected virtual TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
- {
- EditModelFromTUpdate(obj, model);
- return model;
- }
-
- protected virtual LuResult<T> GetNotFoundResult<T>()
- {
- return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
- }
-
- protected virtual IQueryable<TModel> GetFilterQuery(LuFilterDbo filter, TDbContext db, DbSet<TModel> table)
- {
- return GetGetQueryable(db, table).Where(GetFilterExpression(filter));
- }
-
-
-
-
- public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
- {
- var models = objs.Select(GetModelFromTCreate).ToList();
- var addRes = Execute((db, table) =>
- {
- table.AddRange(models);
- db.SaveChanges();
- foreach (var model in models)
- {
- db.Entry(model).State = EntityState.Detached;
- }
- return LuResult<T>.Ok(default(T));
- });
- if (!addRes)
- {
- return addRes;
- }
- return GetMultiple(models, returnFunc);
- }
-
- public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
- {
- return Add(new List<TDboCreate> {obj}, list => returnFunc(list.First()));
- }
-
-
- public virtual LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs)
- {
- var func = GetIdFunc<TId>();
- return Add(objs, list => list.Select(func));
- }
-
- public virtual LuResult<TId> AddId(TDboCreate obj)
- {
- return AddId(new List<TDboCreate> {obj}).To(list => list.First());
- }
-
-
- public virtual LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
- {
- return Add(obj, read => read);
- }
-
- public virtual LuResult<TDboRead> AddDbo(TDboCreate obj)
- {
- return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
- }
-
-
-
-
- public virtual LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
- {
- return Execute((db, table) =>
- {
- var model = GetGetQueryable(db, table).AsNoTracking().FirstOrDefault(predicate);
- if (model == default(TModel))
- {
- return GetNotFoundResult<TDboRead>();
- }
- var dbo = GetDboFromModel(model);
- return LuResult<TDboRead>.Ok(dbo);
- });
- }
-
- public virtual LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
- {
- return GetSingle(GetExpression(keys));
- }
-
- public virtual LuResult<TDboRead> GetSingleById(TId id)
- {
- var guid = GetId(id);
- if (guid == null)
- {
- return GetNotFoundResult<TDboRead>();
- }
- return GetSingleByKeys(new KeyValuePair<string, object>("id", guid));
- }
-
- protected virtual LuResult<T> GetMultiple<T>(IList<TModel> models, Func<IEnumerable<TDboRead>, T> returnFunc)
- {
- var dbos = new List<TDboRead>();
- foreach (var model in models)
- {
- var getRes = GetSingleById((TId) GetModelId(((dynamic)model).id));
- if (!getRes)
- {
- return getRes.To<T>();
- }
- dbos.Add(getRes.Data);
- }
- var res = returnFunc(dbos);
- return LuResult<T>.Ok(res);
- }
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
- Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
- params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
- {
- return Execute((db, table) =>
- {
- var queryable = GetGetQueryable(db, table);
- var count = queryable.Count(predicate);
- var ordered = orderBy(queryable);
- foreach (var func in otherOrderBy)
- {
- ordered = func(ordered);
- }
- var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).AsNoTracking().Select(GetDboFromModel).ToList();
- var result = new LuPaginatedDbo<TDboRead>
- {
- Count = count,
- Data = data
- };
- return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
- });
- }
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
- Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
- params Expression<Func<TModel, object>>[] otherOrderBy)
- {
- return GetMultiple(table => table.OrderBy(orderBy), predicate, page, perPage,
- otherOrderBy.Select<Expression<Func<TModel, object>>, Func<IOrderedQueryable<TModel>,
- IOrderedQueryable<TModel>>>(expression => (t => t.ThenBy(expression))).ToArray());
- }
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
- int page = 0, int perPage = int.MaxValue, params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
- {
- return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
- }
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
- int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, object>>[] otherOrderBy)
- {
- return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
- }
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, Func<TDbContext, DbSet<TModel>, IQueryable<TModel>> getQueryable,
- int page = 0, int perPage = int.MaxValue)
- {
- return Execute((db, table) =>
- {
- var queryable = getQueryable(db, table);
- var count = queryable.Count();
- IOrderedQueryable<TModel> ordered = null;
- foreach (var field in orderBy.Fields)
- {
- var exp = GetOrderByFieldExpression(field.Name);
- if (exp == null)
- {
- return LuResult<LuPaginatedDbo<TDboRead>>.Error(LuStatus.InputError,
- string.Format("LuEfCrudDataAccess: {0}", field.Name), "Invalid order by field");
- }
- if (ordered != null)
- {
- ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);
- }
- else
- {
- ordered = field.Asc ? queryable.OrderBy(exp) : queryable.OrderByDescending(exp);
- }
- }
-
- var data = ordered.Skip(page * perPage).Take(perPage).AsNoTracking().Select(GetDboFromModel).ToList();
- var result = new LuPaginatedDbo<TDboRead>
- {
- Count = count,
- Data = data
- };
- return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
- });
- }
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
- int page = 0, int perPage = int.MaxValue)
- {
- return GetMultiple(orderBy, (db, table) => GetFilterQuery(filter, db, table), page, perPage);
- }
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
- {
- return GetMultiple(orderBy, GetGetQueryable, page, perPage);
- }
-
-
-
- public virtual LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
- Func<IEnumerable<TDboRead>, T> returnFunc)
- {
- IList<TModel> models = null;
- var editRes = Execute((db, table) =>
- {
- models = GetEditQueryable(db, table).Where(predicate).AsNoTracking().ToList();
- foreach (var model in models)
- {
- update(model);
- db.Entry(model).State = EntityState.Modified;
- }
- db.SaveChanges();
- return LuResult<T>.Ok(default(T));
- });
- if (!editRes)
- {
- return editRes;
- }
- return GetMultiple(models, returnFunc);
- }
-
- public virtual LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
- {
- var func = GetIdFunc<TId>();
- return Edit(predicate, update, reads => reads.Select(func));
- }
-
- public virtual LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
- {
- return Edit(predicate, update, read => read);
- }
-
-
- public virtual LuResult<T> EditSingleById<T>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
- {
- var guid = GetId(id);
- if (guid == null)
- {
- return GetNotFoundResult<T>();
- }
- return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)), update,
- list => returnFunc(list.FirstOrDefault()));
- }
-
- public virtual LuResult<TId> EditSingleByIdId(TId id, Action<TModel> update)
- {
- return EditSingleById(id, update, GetIdFunc<TId>());
- }
-
- public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, Action<TModel> update)
- {
- return EditSingleById(id, update, read => read);
- }
-
-
- public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
- {
- var guid = GetId(id);
- if (guid == null)
- {
- return GetNotFoundResult<T>();
- }
- return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)),
- model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
- }
-
- public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
- {
- return EditSingleById(id, update, GetIdFunc<TId>());
- }
-
- public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
- {
- return EditSingleById(id, update, read => read);
- }
-
-
-
-
- public virtual LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
- Func<IEnumerable<TDboRead>, T> returnFunc)
- {
- return Execute((db, table) =>
- {
- var models = GetDeleteQueryable(db, table).Where(predicate).AsNoTracking().ToList();
- var getRes = GetMultiple(models, returnFunc);
- if (!getRes)
- {
- return getRes;
- }
- table.RemoveRange(models);
- db.SaveChanges();
- return getRes;
- });
- }
-
- public virtual LuResult<IEnumerable<TId>> DeleteId(Expression<Func<TModel, bool>> predicate)
- {
- var func = GetIdFunc<TId>();
- return Delete(predicate, reads => reads.Select(func));
- }
-
- public virtual LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
- {
- return Delete(predicate, read => read);
- }
-
-
- public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
- {
- var guid = GetId(id);
- if (guid == null)
- {
- return GetNotFoundResult<T>();
- }
- return Delete(GetExpression(new KeyValuePair<string, object>("id", guid)),
- reads => returnFunc(reads.FirstOrDefault()));
- }
-
- public virtual LuResult<TId> DeleteSingleByIdId(TId id)
- {
- var func = GetIdFunc<TId>();
- return DeleteSingleById(id, func);
- }
-
- public virtual LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
- {
- return DeleteSingleById(id, read => read);
- }
- }
- }
|