You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuCrudBusiness.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using Luticate2.Utils.Dbo.Basic;
  3. using Luticate2.Utils.Dbo.PaginatedRequest;
  4. using Luticate2.Utils.Dbo.Result;
  5. using Luticate2.Utils.Interfaces;
  6. using Luticate2.Utils.Utils;
  7. namespace Luticate2.Utils.Business
  8. {
  9. public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate, TId> :
  10. LuBusiness,
  11. ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
  12. where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
  13. where TDboCreate : class
  14. where TDboRead : class
  15. where TDboUpdate : class
  16. {
  17. protected readonly TDataAccess DataAccess;
  18. private readonly ILuNotificationsBusiness _notificationsBusiness;
  19. protected virtual string EntityType { get; set; }
  20. protected virtual Func<string, TDboRead, bool> NotifyCreateFilter { get; set; }
  21. protected virtual Func<string, TDboRead, TDboRead, bool> NotifyUpdateFilter { get; set; }
  22. protected virtual Func<string, TDboRead, bool> NotifyDeleteFilter { get; set; }
  23. protected LuCrudBusiness(TDataAccess dataAccess, ILuNotificationsBusiness notificationsBusiness)
  24. {
  25. DataAccess = dataAccess;
  26. _notificationsBusiness = notificationsBusiness;
  27. }
  28. protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
  29. {
  30. return LuResult<TDboCreate>.Ok(obj);
  31. }
  32. protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
  33. {
  34. return LuResult<TDboUpdate>.Ok(update);
  35. }
  36. protected virtual LuResult<TDboUpdate> GetAndCheckEdit(TId id, TDboUpdate update)
  37. {
  38. var res = GetSingleById(id);
  39. if (!res)
  40. {
  41. return res.To<TDboUpdate>();
  42. }
  43. return CheckEdit(res.Data, update);
  44. }
  45. public TId GetDboId(TDboRead obj)
  46. {
  47. return DataAccess.GetDboId(obj);
  48. }
  49. public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  50. {
  51. var res = CheckAdd(obj);
  52. if (!res)
  53. {
  54. return res.To<T>();
  55. }
  56. var newEntity = DataAccess.AddDbo(res.Data);
  57. if (newEntity)
  58. {
  59. _notificationsBusiness.NotifyCreate(EntityType, newEntity.Data,
  60. NotifyCreateFilter != null ? s => NotifyCreateFilter(s, newEntity.Data) : (Func<string, bool>) null);
  61. return LuResult<T>.Ok(returnFunc(newEntity.Data));
  62. }
  63. return newEntity.To<T>();
  64. }
  65. public virtual LuResult<TDboRead> GetSingleById(TId id)
  66. {
  67. return DataAccess.GetSingleById(id);
  68. }
  69. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuPaginatedRequestDbo request)
  70. {
  71. return DataAccess.GetMultiple(request);
  72. }
  73. public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  74. {
  75. var originalEntity = GetSingleById(id);
  76. if (!originalEntity)
  77. {
  78. return originalEntity.To<T>();
  79. }
  80. var obj = CheckEdit(originalEntity.Data, update);
  81. if (!obj)
  82. {
  83. return obj.To<T>();
  84. }
  85. var editedEntity = DataAccess.EditSingleByIdDbo(id, obj.Data);
  86. if (editedEntity)
  87. {
  88. _notificationsBusiness.NotifyUpdate(EntityType, originalEntity.Data, editedEntity.Data,
  89. NotifyUpdateFilter != null ? s => NotifyUpdateFilter(s, originalEntity.Data, editedEntity.Data) : (Func<string, bool>) null);
  90. return LuResult<T>.Ok(returnFunc(editedEntity.Data));
  91. }
  92. return editedEntity.To<T>();
  93. }
  94. public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
  95. {
  96. var originalEntity = GetSingleById(id);
  97. if (!originalEntity)
  98. {
  99. return originalEntity.To<T>();
  100. }
  101. var res = DataAccess.DeleteSingleById(id, returnFunc);
  102. if (res)
  103. {
  104. _notificationsBusiness.NotifyDelete(EntityType, originalEntity.Data,
  105. NotifyDeleteFilter != null ? s => NotifyDeleteFilter(s, originalEntity.Data) : (Func<string, bool>) null);
  106. }
  107. return res;
  108. }
  109. }
  110. }