using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using Luticate2.Utils.Dbo; using Microsoft.EntityFrameworkCore; namespace Luticate2.Utils.DataAccess { public abstract class LuEfCrudDataAccess : LuEfDataAccess where TModel : class where TDboCreate : class where TDboRead : class where TDboUpdate : class where TDbContext : DbContext { protected LuEfCrudDataAccess(TDbContext db, DbSet table) : base(db, table) { } protected abstract TModel GetModelFromTCreate(TDboCreate obj); protected abstract TModel GetModelFromTUpdate(TDboUpdate obj); protected abstract TDboRead GetDboFromModel(TModel model); public LuResult Add(IEnumerable objs, Func, 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.Ok(res); }); } public LuResult Add(TDboCreate obj, Func returnFunc) { return Add(new List {obj}, list => returnFunc(list.First())); } public LuResult> AddGuid(IEnumerable objs) { var param = Expression.Parameter(typeof(TDboRead), "x"); var exp = Expression.Property(param, "Id"); var lambda = Expression.Lambda>(exp, param); var func = lambda.Compile(); return Add(objs, list => list.Select(func)); } public LuResult AddGuid(TDboCreate obj) { return AddGuid(new List {obj}).To(list => list.First()); } public LuResult> AddId(IEnumerable obj) { var param = Expression.Parameter(typeof(TDboRead), "x"); var exp = Expression.Property(param, "Id"); var lambda = Expression.Lambda>(exp, param); var func = lambda.Compile(); return Add(obj, list => list.Select(func)); } public LuResult AddId(TDboCreate obj) { return AddId(new List {obj}).To(list => list.First()); } public LuResult> AddDbo(IEnumerable obj) { return Add(obj, read => read); } public LuResult AddDbo(TDboCreate obj) { return AddDbo(new List {obj}).To(list => list.First()); } public LuResult GetSingle(Expression> predicate) { return Execute(() => { var model = Table.FirstOrDefault(predicate); if (model == null) { return LuResult.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", ""); } var dbo = GetDboFromModel(model); return LuResult.Ok(dbo); }); } public LuResult GetSingleByKeys(params KeyValuePair[] keys) { return GetSingle(GetExpression(keys)); } public LuResult GetSingleById(Guid id) { return GetSingleByKeys(new KeyValuePair("id", id)); } public LuResult GetSingleById(string id) { return GetSingleById(new Guid(id)); } public LuResult GetSingleById(long id) { return GetSingleByKeys(new KeyValuePair("id", id)); } public LuResult> GetMultiple(Func> orderBy, Expression> predicate, int page = 0, int perPage = int.MaxValue, params Expression>[] otherOrderBy) { return Execute(() => { var count = Table.Count(predicate); var ordered = orderBy(); var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList(); var result = new LuPaginatedDbo { Count = count, Data = data }; return LuResult>.Ok(result); }); } public LuResult> GetMultiple(Expression> orderBy, Expression> predicate, int page = 0, int perPage = int.MaxValue) { return GetMultiple(() => Table.OrderBy(orderBy), predicate, page, perPage); } public LuResult> GetMultiple(Expression> orderBy, int page = 0, int perPage = int.MaxValue) { return GetMultiple(orderBy, x => true, page, perPage); } public LuResult Edit(Expression> predicate, Action update, Func, T> returnFunc) { return Execute(() => { var models = Table.Where(predicate); var editedDbos = new List(); 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.Ok(res); }); } public LuResult EditSingleByPkId(TId id, Action update, Func returnFunc) { return Edit(GetExpression(new KeyValuePair("id", id)), update, list => list.Count == 0 ? returnFunc(default(TDboRead)) : returnFunc(list[0])); } public LuResult EditSingleById(long id, Action update, Func returnFunc) { return EditSingleByPkId(id, update, returnFunc); } public LuResult EditSingleById(Guid id, Action update, Func returnFunc) { return EditSingleByPkId(id, update, returnFunc); } public LuResult EditSingleById(string id, Action update, Func returnFunc) { return EditSingleById(new Guid(id), update, returnFunc); } } }