123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Luticate2.Utils.Dbo.Basic;
- using Luticate2.Utils.Dbo.Filter;
- using Luticate2.Utils.Dbo.OrderBy;
- using Luticate2.Utils.Dbo.Result;
- using Luticate2.Utils.Interfaces;
-
- namespace Luticate2.Utils.Business
- {
- public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate, TId> :
- LuBusiness,
- ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
- where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
- where TDboCreate : class
- where TDboRead : class
- where TDboUpdate : class
- {
- protected readonly TDataAccess DataAccess;
-
- private readonly LuNotificationsBusiness _notificationsBusiness;
-
- protected virtual string EntityType { get; set; }
-
- protected virtual Func<string, TDboRead, bool> NotifyCreateFilter { get; set; }
-
- protected virtual Func<string, TDboRead, TDboRead, bool> NotifyUpdateFilter { get; set; }
-
- protected virtual Func<string, TDboRead, bool> NotifyDeleteFilter { get; set; }
-
- protected LuCrudBusiness(TDataAccess dataAccess, LuNotificationsBusiness notificationsBusiness)
- {
- DataAccess = dataAccess;
- _notificationsBusiness = notificationsBusiness;
- }
-
- protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
- {
- return LuResult<TDboCreate>.Ok(obj);
- }
-
- protected virtual LuResult<IEnumerable<TDboCreate>> CheckAdd(IEnumerable<TDboCreate> objs)
- {
- var list = new List<TDboCreate>();
- foreach (var obj in objs)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<IEnumerable<TDboCreate>>();
- }
- list.Add(res.Data);
- }
- return LuResult<IEnumerable<TDboCreate>>.Ok(list);
- }
-
- protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
- {
- return LuResult<TDboUpdate>.Ok(update);
- }
-
- protected virtual LuResult<TDboUpdate> GetAndCheckEdit(TId id, TDboUpdate update)
- {
- var res = GetSingleById(id);
- if (!res)
- {
- return res.To<TDboUpdate>();
- }
- return CheckEdit(res.Data, update);
- }
-
-
-
-
- public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
- {
- var res = CheckAdd(objs);
- if (!res)
- {
- return res.To<T>();
- }
- return DataAccess.Add(res.Data, returnFunc);
- }
-
- public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<T>();
- }
- var newEntity = DataAccess.AddDbo(res.Data);
- if (newEntity)
- {
- _notificationsBusiness.NotifyCreate(EntityType, newEntity.Data,
- NotifyCreateFilter != null ? s => NotifyCreateFilter(s, newEntity.Data) : (Func<string, bool>) null);
- return LuResult<T>.Ok(returnFunc(newEntity.Data));
- }
- return newEntity.To<T>();
- }
-
- public virtual LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs)
- {
- return Add(objs, reads => reads.Select(read => (TId)((dynamic)read).Id));
- }
-
- public virtual LuResult<TId> AddId(TDboCreate obj)
- {
- return Add(obj, read => (TId)((dynamic)read).Id);
- }
-
- public virtual LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> objs)
- {
- return Add(objs, reads => reads);
- }
-
- public virtual LuResult<TDboRead> AddDbo(TDboCreate obj)
- {
- return Add(obj, read => read);
- }
-
-
-
-
- public virtual LuResult<TDboRead> GetSingleById(TId id)
- {
- return DataAccess.GetSingleById(id);
- }
-
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
- int page = 0, int perPage = int.MaxValue)
- {
- return DataAccess.GetMultiple(orderBy, filter, page, perPage);
- }
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0,
- int perPage = int.MaxValue)
- {
- return GetMultiple(orderBy, new LuFilterDbo { Query = ""}, page, perPage);
- }
-
-
-
-
- public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
- {
- var originalEntity = GetSingleById(id);
- if (!originalEntity)
- {
- return originalEntity.To<T>();
- }
-
- var obj = CheckEdit(originalEntity.Data, update);
- if (!obj)
- {
- return obj.To<T>();
- }
- var editedEntity = DataAccess.EditSingleByIdDbo(id, obj.Data);
- if (editedEntity)
- {
- _notificationsBusiness.NotifyUpdate(EntityType, originalEntity.Data, editedEntity.Data,
- NotifyUpdateFilter != null ? s => NotifyUpdateFilter(s, originalEntity.Data, editedEntity.Data) : (Func<string, bool>) null);
- return LuResult<T>.Ok(returnFunc(editedEntity.Data));
- }
- return editedEntity.To<T>();
- }
-
- public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
- {
- return EditSingleById(id, update, read => (TId)((dynamic)read).Id);
- }
-
- public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
- {
- return EditSingleById(id, update, read => read);
- }
-
-
-
-
- public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
- {
- var originalEntity = GetSingleById(id);
- if (!originalEntity)
- {
- return originalEntity.To<T>();
- }
- var res = DataAccess.DeleteSingleById(id, returnFunc);
- if (res)
- {
- _notificationsBusiness.NotifyDelete(EntityType, originalEntity.Data,
- NotifyDeleteFilter != null ? s => NotifyDeleteFilter(s, originalEntity.Data) : (Func<string, bool>) null);
- }
- return res;
- }
-
- public virtual LuResult<TId> DeleteSingleByIdId(TId id)
- {
- return DeleteSingleById(id, read => (TId)((dynamic)read).Id);
- }
-
- public virtual LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
- {
- return DeleteSingleById(id, read => read);
- }
- }
- }
|