Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LuEfCrudDataAccess.cs 15KB

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