123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using Luticate2.Utils.Dbo;
- using Luticate2.Utils.Interfaces;
- using Microsoft.EntityFrameworkCore;
-
- namespace Luticate2.Utils.DataAccess
- {
- public abstract class LuEfCrudInterface<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext> :
- LuEfDataAccess<TModel, TDbContext>, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
- where TModel : class
- where TDboCreate : class
- where TDboRead : class
- where TDboUpdate : class
- where TDbContext : DbContext
- {
- protected LuEfCrudInterface(TDbContext db, DbSet<TModel> table) : base(db, table)
- {
- }
-
- protected abstract TModel GetModelFromTCreate(TDboCreate obj);
-
- protected abstract void EditModelFromTUpdate(TDboUpdate obj, TModel model);
-
- protected abstract TDboRead GetDboFromModel(TModel model);
-
- protected Func<TDboRead, T> GetIdFunc<T>()
- {
- var param = Expression.Parameter(typeof(TDboRead), "x");
- var exp = Expression.Property(param, "Id");
- var lambda = Expression.Lambda<Func<TDboRead, T>>(exp, param);
- var func = lambda.Compile();
- return func;
- }
-
- protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
- {
- EditModelFromTUpdate(obj, model);
- return model;
- }
-
-
-
-
- public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
- {
- return Execute(() =>
- {
- var models = objs.Select(GetModelFromTCreate).ToList();
- Table.AddRange(models);
- Db.SaveChanges();
- var dbos = models.Select(GetDboFromModel).ToList();
- var res = returnFunc(dbos);
- return LuResult<T>.Ok(res);
- });
- }
-
- public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
- {
- return Add(new List<TDboCreate> {obj}, list => returnFunc(list.First()));
- }
-
-
- public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
- {
- var func = GetIdFunc<string>();
- return Add(objs, list => list.Select(func));
- }
-
- public LuResult<string> AddGuid(TDboCreate obj)
- {
- return AddGuid(new List<TDboCreate> {obj}).To(list => list.First());
- }
-
-
- public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
- {
- var func = GetIdFunc<long>();
- return Add(obj, list => list.Select(func));
- }
-
- public LuResult<long> AddId(TDboCreate obj)
- {
- return AddId(new List<TDboCreate> {obj}).To(list => list.First());
- }
-
-
- public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
- {
- return Add(obj, read => read);
- }
-
- public LuResult<TDboRead> AddDbo(TDboCreate obj)
- {
- return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
- }
-
-
-
-
- public LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
- {
- return Execute(() =>
- {
- var model = Table.FirstOrDefault(predicate);
- if (model == null)
- {
- return LuResult<TDboRead>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
- }
- var dbo = GetDboFromModel(model);
- return LuResult<TDboRead>.Ok(dbo);
- });
- }
-
- public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
- {
- return GetSingle(GetExpression(keys));
- }
-
- public LuResult<TDboRead> GetSingleById(string id)
- {
- return GetSingleByKeys(new KeyValuePair<string, object>("id", new Guid(id)));
- }
-
- public LuResult<TDboRead> GetSingleById(long id)
- {
- return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
- }
-
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IQueryable<TModel>> orderBy,
- Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
- params Func<DbSet<TModel>, IQueryable<TModel>>[] otherOrderBy)
- {
- return Execute(() =>
- {
- var count = Table.Count(predicate);
- var ordered = orderBy(Table);
- foreach (var func in otherOrderBy)
- {
- ordered = func(Table);
- }
- var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList();
- var result = new LuPaginatedDbo<TDboRead>
- {
- Count = count,
- Data = data
- };
- return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
- });
- }
-
- public 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<DbSet<TModel>,
- IQueryable<TModel>>>(expression => (t => t.OrderBy(expression))).ToArray());
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IQueryable<TModel>> orderBy,
- int page = 0, int perPage = int.MaxValue, params Func<DbSet<TModel>, IQueryable<TModel>>[] otherOrderBy)
- {
- return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
- int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, object>>[] otherOrderBy)
- {
- return GetMultiple(table => table.OrderBy(orderBy), x => true, page, perPage,
- otherOrderBy.Select<Expression<Func<TModel, object>>, Func<DbSet<TModel>,
- IQueryable<TModel>>>(expression => (t => t.OrderBy(expression))).ToArray());
- }
-
-
-
-
- public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
- Func<IEnumerable<TDboRead>, T> returnFunc)
- {
- return Execute(() =>
- {
- var models = Table.Where(predicate);
- var editedDbos = new List<TDboRead>();
- foreach (var model in models)
- {
- update(model);
- editedDbos.Add(GetDboFromModel(model));
- Db.Entry(model).State = EntityState.Modified;
- }
- Db.SaveChanges();
- var res = returnFunc(editedDbos);
- return LuResult<T>.Ok(res);
- });
- }
-
- public LuResult<IEnumerable<string>> EditGuid(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
- {
- var func = GetIdFunc<string>();
- return Edit(predicate, update, reads => reads.Select(func));
- }
-
- public LuResult<IEnumerable<long>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
- {
- var func = GetIdFunc<long>();
- return Edit(predicate, update, reads => reads.Select(func));
- }
-
- public LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
- {
- return Edit(predicate, update, read => read);
- }
-
-
- public LuResult<T> EditSingleById<T>(long id, Action<TModel> update, Func<TDboRead, T> returnFunc)
- {
- return Edit(GetExpression(new KeyValuePair<string, object>("id", id)), update,
- list => returnFunc(list.FirstOrDefault()));
- }
-
- public LuResult<long> EditSingleByIdId(long id, Action<TModel> update)
- {
- var func = GetIdFunc<long>();
- return EditSingleById(id, update, func);
- }
-
- public LuResult<TDboRead> EditSingleByIdDbo(long id, Action<TModel> update)
- {
- return EditSingleById(id, update, read => read);
- }
-
-
- public LuResult<T> EditSingleById<T>(string id, Action<TModel> update, Func<TDboRead, T> returnFunc)
- {
- return Edit(GetExpression(new KeyValuePair<string, object>("id", new Guid(id))), update,
- list => returnFunc(list.FirstOrDefault()));
- }
-
- public LuResult<string> EditSingleByIdGuid(string id, Action<TModel> update)
- {
- return EditSingleById(id, update, GetIdFunc<string>());
- }
-
- public LuResult<TDboRead> EditSingleByIdDbo(string id, Action<TModel> update)
- {
- return EditSingleById(id, update, read => read);
- }
-
-
- public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
- {
- return Edit(GetExpression(new KeyValuePair<string, object>("id", id)),
- model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
- }
-
- public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
- {
- return EditSingleById(id, update, GetIdFunc<long>());
- }
-
- public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
- {
- return EditSingleById(id, update, read => read);
- }
-
-
- public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
- {
- return Edit(GetExpression(new KeyValuePair<string, object>("id", new Guid(id))),
- model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
- }
-
- public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
- {
- return EditSingleById(id, update, GetIdFunc<string>());
- }
-
- public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
- {
- return EditSingleById(id, update, read => read);
- }
-
-
-
-
- public LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
- Func<IEnumerable<TDboRead>, T> returnFunc)
- {
- return Execute(() =>
- {
- var models = Table.Where(predicate).ToList();
- Table.RemoveRange(models);
- Db.SaveChanges();
- var dbos = models.Select(GetDboFromModel);
- return LuResult<T>.Ok(returnFunc(dbos));
- });
- }
-
- public LuResult<IEnumerable<string>> DeleteGuid(Expression<Func<TModel, bool>> predicate)
- {
- var func = GetIdFunc<string>();
- return Delete(predicate, reads => reads.Select(func));
- }
-
- public LuResult<IEnumerable<long>> DeleteId(Expression<Func<TModel, bool>> predicate)
- {
- var func = GetIdFunc<long>();
- return Delete(predicate, reads => reads.Select(func));
- }
-
- public LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
- {
- return Delete(predicate, read => read);
- }
-
-
- public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
- {
- return Delete(GetExpression(new KeyValuePair<string, object>("id", new Guid(id))),
- reads => returnFunc(reads.First()));
- }
-
- public LuResult<string> DeleteSingleByIdGuid(string id)
- {
- var func = GetIdFunc<string>();
- return DeleteSingleById(id, func);
- }
-
- public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
- {
- return DeleteSingleById(id, read => read);
- }
-
-
- public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
- {
- return Delete(GetExpression(new KeyValuePair<string, object>("id", id)),
- reads => returnFunc(reads.First()));
- }
-
- public LuResult<long> DeleteSingleByIdId(long id)
- {
- var func = GetIdFunc<long>();
- return DeleteSingleById(id, func);
- }
-
- public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
- {
- return DeleteSingleById(id, read => read);
- }
- }
- }
|