123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext> :
- LuEfDataAccess<TModel, TDbContext>
- where TModel : class
- where TDboCreate : class
- where TDboRead : class
- where TDboUpdate : class
- where TDbContext : DbContext
- {
- protected LuEfCrudDataAccess(TDbContext db, DbSet<TModel> table) : base(db, table)
- {
- }
-
- protected abstract TModel GetModelFromTCreate(TDboCreate obj);
-
- protected abstract TModel GetModelFromTUpdate(TDboUpdate obj);
-
- protected abstract TDboRead GetDboFromModel(TModel 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 param = Expression.Parameter(typeof(TDboRead), "x");
- var exp = Expression.Property(param, "Id");
- var lambda = Expression.Lambda<Func<TDboRead, string>>(exp, param);
- var func = lambda.Compile();
- 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 param = Expression.Parameter(typeof(TDboRead), "x");
- var exp = Expression.Property(param, "Id");
- var lambda = Expression.Lambda<Func<TDboRead, long>>(exp, param);
- var func = lambda.Compile();
- 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(Guid id)
- {
- return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
- }
-
- public LuResult<TDboRead> GetSingleById(string id)
- {
- return GetSingleById(new Guid(id));
- }
-
- public LuResult<TDboRead> GetSingleById(long id)
- {
- return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Func<IQueryable<TModel>> orderBy,
- Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
- params Expression<Func<TModel, TKey>>[] 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<TDboRead>
- {
- Count = count,
- Data = data
- };
- return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
- });
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
- Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue)
- {
- return GetMultiple<TKey>(() => Table.OrderBy(orderBy), predicate, page, perPage);
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
- int page = 0, int perPage = int.MaxValue)
- {
- return GetMultiple(orderBy, x => true, page, perPage);
- }
-
- public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
- Func<List<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<T> EditSingleByPkId<T, TId>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
- {
- return Edit(GetExpression(new KeyValuePair<string, object>("id", id)), update,
- list => list.Count == 0 ? returnFunc(default(TDboRead)) : returnFunc(list[0]));
- }
-
- public LuResult<T> EditSingleById<T>(long id, Action<TModel> update, Func<TDboRead, T> returnFunc)
- {
- return EditSingleByPkId(id, update, returnFunc);
- }
-
- public LuResult<T> EditSingleById<T>(Guid id, Action<TModel> update, Func<TDboRead, T> returnFunc)
- {
- return EditSingleByPkId(id, update, returnFunc);
- }
-
- public LuResult<T> EditSingleById<T>(string id, Action<TModel> update, Func<TDboRead, T> returnFunc)
- {
- return EditSingleById(new Guid(id), update, returnFunc);
- }
- }
- }
|