123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- 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;
- using Microsoft.EntityFrameworkCore.Metadata.Internal;
-
- 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(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 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 Func<TDboRead, T> GetIdFunc<T>()
- {
- return x => ((dynamic) x).Id;
- }
-
- protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
- {
- EditModelFromTUpdate(obj, model);
- return model;
- }
-
- 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 LuResult<T> GetNotFoundResult<T>()
- {
- return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
- }
-
- protected IQueryable<TModel> GetFilterQuery(LuFilterDbo filter)
- {
- return Table.Where(GetFilterExpression(filter));
- }
-
-
-
-
- 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<TId>> AddId(IEnumerable<TDboCreate> objs)
- {
- var func = GetIdFunc<TId>();
- return Add(objs, list => list.Select(func));
- }
-
- public LuResult<TId> 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 == default(TModel))
- {
- return GetNotFoundResult<TDboRead>();
- }
- 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(TId id)
- {
- var guid = GetId(id);
- if (guid == null)
- {
- return GetNotFoundResult<TDboRead>();
- }
- return GetSingleByKeys(new KeyValuePair<string, object>("id", guid));
- }
-
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<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(() =>
- {
- var count = Table.Count(predicate);
- var ordered = orderBy(Table);
- foreach (var func in otherOrderBy)
- {
- ordered = func(ordered);
- }
- 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<IOrderedQueryable<TModel>,
- IOrderedQueryable<TModel>>>(expression => (t => t.ThenBy(expression))).ToArray());
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<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 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 LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, IQueryable<TModel> queryable,
- int page = 0, int perPage = int.MaxValue)
- {
- return Execute(() =>
- {
- 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).Select(GetDboFromModel).ToList();
- var result = new LuPaginatedDbo<TDboRead>
- {
- Count = count,
- Data = data
- };
- return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
- });
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
- int page = 0, int perPage = int.MaxValue)
- {
- return GetMultiple(orderBy, GetFilterQuery(filter), page, perPage);
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
- {
- return GetMultiple(orderBy, Table, page, perPage);
- }
-
-
-
- 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<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
- {
- var func = GetIdFunc<TId>();
- 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>(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 LuResult<TId> EditSingleByIdId(TId id, Action<TModel> update)
- {
- return EditSingleById(id, update, GetIdFunc<TId>());
- }
-
- public LuResult<TDboRead> EditSingleByIdDbo(TId id, Action<TModel> update)
- {
- return EditSingleById(id, update, read => read);
- }
-
-
- public 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 LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
- {
- return EditSingleById(id, update, GetIdFunc<TId>());
- }
-
- public LuResult<TDboRead> EditSingleByIdDbo(TId 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<TId>> DeleteId(Expression<Func<TModel, bool>> predicate)
- {
- var func = GetIdFunc<TId>();
- 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>(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 LuResult<TId> DeleteSingleByIdId(TId id)
- {
- var func = GetIdFunc<TId>();
- return DeleteSingleById(id, func);
- }
-
- public LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
- {
- return DeleteSingleById(id, read => read);
- }
- }
- }
|