Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuEfCrudInterface.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Utils.Dbo;
  6. using Luticate2.Utils.Interfaces;
  7. using Microsoft.EntityFrameworkCore;
  8. namespace Luticate2.Utils.DataAccess
  9. {
  10. public abstract class LuEfCrudInterface<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext> :
  11. LuEfDataAccess<TModel, TDbContext>, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
  12. where TModel : class
  13. where TDboCreate : class
  14. where TDboRead : class
  15. where TDboUpdate : class
  16. where TDbContext : DbContext
  17. {
  18. protected LuEfCrudInterface(TDbContext db, DbSet<TModel> table) : base(db, table)
  19. {
  20. }
  21. protected abstract TModel GetModelFromTCreate(TDboCreate obj);
  22. protected abstract void EditModelFromTUpdate(TDboUpdate obj, TModel model);
  23. protected abstract TDboRead GetDboFromModel(TModel model);
  24. protected Func<TDboRead, T> GetIdFunc<T>()
  25. {
  26. var param = Expression.Parameter(typeof(TDboRead), "x");
  27. var exp = Expression.Property(param, "Id");
  28. var lambda = Expression.Lambda<Func<TDboRead, T>>(exp, param);
  29. var func = lambda.Compile();
  30. return func;
  31. }
  32. protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
  33. {
  34. EditModelFromTUpdate(obj, model);
  35. return model;
  36. }
  37. public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  38. {
  39. return Execute(() =>
  40. {
  41. var models = objs.Select(GetModelFromTCreate).ToList();
  42. Table.AddRange(models);
  43. Db.SaveChanges();
  44. var dbos = models.Select(GetDboFromModel).ToList();
  45. var res = returnFunc(dbos);
  46. return LuResult<T>.Ok(res);
  47. });
  48. }
  49. public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  50. {
  51. return Add(new List<TDboCreate> {obj}, list => returnFunc(list.First()));
  52. }
  53. public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
  54. {
  55. var func = GetIdFunc<string>();
  56. return Add(objs, list => list.Select(func));
  57. }
  58. public LuResult<string> AddGuid(TDboCreate obj)
  59. {
  60. return AddGuid(new List<TDboCreate> {obj}).To(list => list.First());
  61. }
  62. public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
  63. {
  64. var func = GetIdFunc<long>();
  65. return Add(obj, list => list.Select(func));
  66. }
  67. public LuResult<long> AddId(TDboCreate obj)
  68. {
  69. return AddId(new List<TDboCreate> {obj}).To(list => list.First());
  70. }
  71. public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
  72. {
  73. return Add(obj, read => read);
  74. }
  75. public LuResult<TDboRead> AddDbo(TDboCreate obj)
  76. {
  77. return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
  78. }
  79. public LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
  80. {
  81. return Execute(() =>
  82. {
  83. var model = Table.FirstOrDefault(predicate);
  84. if (model == null)
  85. {
  86. return LuResult<TDboRead>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
  87. }
  88. var dbo = GetDboFromModel(model);
  89. return LuResult<TDboRead>.Ok(dbo);
  90. });
  91. }
  92. public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
  93. {
  94. return GetSingle(GetExpression(keys));
  95. }
  96. public LuResult<TDboRead> GetSingleById(string id)
  97. {
  98. return GetSingleByKeys(new KeyValuePair<string, object>("id", new Guid(id)));
  99. }
  100. public LuResult<TDboRead> GetSingleById(long id)
  101. {
  102. return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
  103. }
  104. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IQueryable<TModel>> orderBy,
  105. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  106. params Func<DbSet<TModel>, IQueryable<TModel>>[] otherOrderBy)
  107. {
  108. return Execute(() =>
  109. {
  110. var count = Table.Count(predicate);
  111. var ordered = orderBy(Table);
  112. foreach (var func in otherOrderBy)
  113. {
  114. ordered = func(Table);
  115. }
  116. var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList();
  117. var result = new LuPaginatedDbo<TDboRead>
  118. {
  119. Count = count,
  120. Data = data
  121. };
  122. return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
  123. });
  124. }
  125. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
  126. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  127. params Expression<Func<TModel, object>>[] otherOrderBy)
  128. {
  129. return GetMultiple(table => table.OrderBy(orderBy), predicate, page, perPage,
  130. otherOrderBy.Select<Expression<Func<TModel, object>>, Func<DbSet<TModel>,
  131. IQueryable<TModel>>>(expression => (t => t.OrderBy(expression))).ToArray());
  132. }
  133. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IQueryable<TModel>> orderBy,
  134. int page = 0, int perPage = int.MaxValue, params Func<DbSet<TModel>, IQueryable<TModel>>[] otherOrderBy)
  135. {
  136. return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
  137. }
  138. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
  139. int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, object>>[] otherOrderBy)
  140. {
  141. return GetMultiple(table => table.OrderBy(orderBy), x => true, page, perPage,
  142. otherOrderBy.Select<Expression<Func<TModel, object>>, Func<DbSet<TModel>,
  143. IQueryable<TModel>>>(expression => (t => t.OrderBy(expression))).ToArray());
  144. }
  145. public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
  146. Func<IEnumerable<TDboRead>, T> returnFunc)
  147. {
  148. return Execute(() =>
  149. {
  150. var models = Table.Where(predicate);
  151. var editedDbos = new List<TDboRead>();
  152. foreach (var model in models)
  153. {
  154. update(model);
  155. editedDbos.Add(GetDboFromModel(model));
  156. Db.Entry(model).State = EntityState.Modified;
  157. }
  158. Db.SaveChanges();
  159. var res = returnFunc(editedDbos);
  160. return LuResult<T>.Ok(res);
  161. });
  162. }
  163. public LuResult<IEnumerable<string>> EditGuid(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  164. {
  165. var func = GetIdFunc<string>();
  166. return Edit(predicate, update, reads => reads.Select(func));
  167. }
  168. public LuResult<IEnumerable<long>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  169. {
  170. var func = GetIdFunc<long>();
  171. return Edit(predicate, update, reads => reads.Select(func));
  172. }
  173. public LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  174. {
  175. return Edit(predicate, update, read => read);
  176. }
  177. public LuResult<T> EditSingleById<T>(long id, Action<TModel> update, Func<TDboRead, T> returnFunc)
  178. {
  179. return Edit(GetExpression(new KeyValuePair<string, object>("id", id)), update,
  180. list => returnFunc(list.FirstOrDefault()));
  181. }
  182. public LuResult<long> EditSingleByIdId(long id, Action<TModel> update)
  183. {
  184. var func = GetIdFunc<long>();
  185. return EditSingleById(id, update, func);
  186. }
  187. public LuResult<TDboRead> EditSingleByIdDbo(long id, Action<TModel> update)
  188. {
  189. return EditSingleById(id, update, read => read);
  190. }
  191. public LuResult<T> EditSingleById<T>(string id, Action<TModel> update, Func<TDboRead, T> returnFunc)
  192. {
  193. return Edit(GetExpression(new KeyValuePair<string, object>("id", new Guid(id))), update,
  194. list => returnFunc(list.FirstOrDefault()));
  195. }
  196. public LuResult<string> EditSingleByIdGuid(string id, Action<TModel> update)
  197. {
  198. return EditSingleById(id, update, GetIdFunc<string>());
  199. }
  200. public LuResult<TDboRead> EditSingleByIdDbo(string id, Action<TModel> update)
  201. {
  202. return EditSingleById(id, update, read => read);
  203. }
  204. public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  205. {
  206. return Edit(GetExpression(new KeyValuePair<string, object>("id", id)),
  207. model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
  208. }
  209. public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
  210. {
  211. return EditSingleById(id, update, GetIdFunc<long>());
  212. }
  213. public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
  214. {
  215. return EditSingleById(id, update, read => read);
  216. }
  217. public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  218. {
  219. return Edit(GetExpression(new KeyValuePair<string, object>("id", new Guid(id))),
  220. model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
  221. }
  222. public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
  223. {
  224. return EditSingleById(id, update, GetIdFunc<string>());
  225. }
  226. public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
  227. {
  228. return EditSingleById(id, update, read => read);
  229. }
  230. public LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
  231. Func<IEnumerable<TDboRead>, T> returnFunc)
  232. {
  233. return Execute(() =>
  234. {
  235. var models = Table.Where(predicate).ToList();
  236. Table.RemoveRange(models);
  237. Db.SaveChanges();
  238. var dbos = models.Select(GetDboFromModel);
  239. return LuResult<T>.Ok(returnFunc(dbos));
  240. });
  241. }
  242. public LuResult<IEnumerable<string>> DeleteGuid(Expression<Func<TModel, bool>> predicate)
  243. {
  244. var func = GetIdFunc<string>();
  245. return Delete(predicate, reads => reads.Select(func));
  246. }
  247. public LuResult<IEnumerable<long>> DeleteId(Expression<Func<TModel, bool>> predicate)
  248. {
  249. var func = GetIdFunc<long>();
  250. return Delete(predicate, reads => reads.Select(func));
  251. }
  252. public LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
  253. {
  254. return Delete(predicate, read => read);
  255. }
  256. public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
  257. {
  258. return Delete(GetExpression(new KeyValuePair<string, object>("id", new Guid(id))),
  259. reads => returnFunc(reads.First()));
  260. }
  261. public LuResult<string> DeleteSingleByIdGuid(string id)
  262. {
  263. var func = GetIdFunc<string>();
  264. return DeleteSingleById(id, func);
  265. }
  266. public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
  267. {
  268. return DeleteSingleById(id, read => read);
  269. }
  270. public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
  271. {
  272. return Delete(GetExpression(new KeyValuePair<string, object>("id", id)),
  273. reads => returnFunc(reads.First()));
  274. }
  275. public LuResult<long> DeleteSingleByIdId(long id)
  276. {
  277. var func = GetIdFunc<long>();
  278. return DeleteSingleById(id, func);
  279. }
  280. public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
  281. {
  282. return DeleteSingleById(id, read => read);
  283. }
  284. }
  285. }