123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using System;
- using System.Collections.Generic;
- 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> : LuBusiness, ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
- where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate>
- where TDboCreate : class
- where TDboRead : class
- where TDboUpdate : class
- {
- protected readonly TDataAccess DataAccess;
-
- protected LuCrudBusiness(TDataAccess dataAccess)
- {
- DataAccess = dataAccess;
- }
-
- 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 LuResult<TDboUpdate> GetAndCheckEdit(string id, TDboUpdate update)
- {
- var res = GetSingleById(id);
- if (!res)
- {
- return res.To<TDboUpdate>();
- }
- return CheckEdit(res.Data, update);
- }
-
- protected LuResult<TDboUpdate> GetAndCheckEdit(long id, TDboUpdate update)
- {
- var res = GetSingleById(id);
- if (!res)
- {
- return res.To<TDboUpdate>();
- }
- return CheckEdit(res.Data, update);
- }
-
-
-
-
- public 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 LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<T>();
- }
- return DataAccess.Add(res.Data, returnFunc);
- }
-
- public LuResult<IEnumerable<string>> AddGuid(IEnumerable<TDboCreate> objs)
- {
- var res = CheckAdd(objs);
- if (!res)
- {
- return res.To<IEnumerable<string>>();
- }
- return DataAccess.AddGuid(res.Data);
- }
-
- public LuResult<string> AddGuid(TDboCreate obj)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<string>();
- }
- return DataAccess.AddGuid(res.Data);
- }
-
- public LuResult<IEnumerable<long>> AddId(IEnumerable<TDboCreate> obj)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<IEnumerable<long>>();
- }
- return DataAccess.AddId(res.Data);
- }
-
- public LuResult<long> AddId(TDboCreate obj)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<long>();
- }
- return DataAccess.AddId(res.Data);
- }
-
- public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<IEnumerable<TDboRead>>();
- }
- return DataAccess.AddDbo(res.Data);
- }
-
- public LuResult<TDboRead> AddDbo(TDboCreate obj)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<TDboRead>();
- }
- return DataAccess.AddDbo(res.Data);
- }
-
-
-
-
- public LuResult<TDboRead> GetSingleById(string id)
- {
- return DataAccess.GetSingleById(id);
- }
-
- public LuResult<TDboRead> GetSingleById(long id)
- {
- return DataAccess.GetSingleById(id);
- }
-
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
- int page = 0, int perPage = int.MaxValue)
- {
- return DataAccess.GetMultiple(orderBy, filter, page, perPage);
- }
-
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0,
- int perPage = int.MaxValue)
- {
- return DataAccess.GetMultiple(orderBy, page, perPage);
- }
-
-
-
-
- public LuResult<T> EditSingleById<T>(long id, TDboUpdate update, Func<TDboRead, T> returnFunc)
- {
- var obj = GetAndCheckEdit(id, update);
- if (!obj)
- {
- return obj.To<T>();
- }
- return DataAccess.EditSingleById(id, obj.Data, returnFunc);
- }
-
- public LuResult<long> EditSingleByIdId(long id, TDboUpdate update)
- {
- var obj = GetAndCheckEdit(id, update);
- if (!obj)
- {
- return obj.To<long>();
- }
- return DataAccess.EditSingleByIdId(id, obj.Data);
- }
-
- public LuResult<TDboRead> EditSingleByIdDbo(long id, TDboUpdate update)
- {
- var obj = GetAndCheckEdit(id, update);
- if (!obj)
- {
- return obj.To<TDboRead>();
- }
- return DataAccess.EditSingleByIdDbo(id, obj.Data);
- }
-
- public LuResult<T> EditSingleById<T>(string id, TDboUpdate update, Func<TDboRead, T> returnFunc)
- {
- var obj = GetAndCheckEdit(id, update);
- if (!obj)
- {
- return obj.To<T>();
- }
- return DataAccess.EditSingleById(id, obj.Data, returnFunc);
- }
-
- public LuResult<string> EditSingleByIdGuid(string id, TDboUpdate update)
- {
- var obj = GetAndCheckEdit(id, update);
- if (!obj)
- {
- return obj.To<string>();
- }
- return DataAccess.EditSingleByIdGuid(id, obj.Data);
- }
-
- public LuResult<TDboRead> EditSingleByIdDbo(string id, TDboUpdate update)
- {
- var obj = GetAndCheckEdit(id, update);
- if (!obj)
- {
- return obj.To<TDboRead>();
- }
- return DataAccess.EditSingleByIdDbo(id, obj.Data);
- }
-
-
-
-
- public LuResult<T> DeleteSingleById<T>(string id, Func<TDboRead, T> returnFunc)
- {
- return DataAccess.DeleteSingleById(id, returnFunc);
- }
-
- public LuResult<string> DeleteSingleByIdGuid(string id)
- {
- return DataAccess.DeleteSingleByIdGuid(id);
- }
-
- public LuResult<TDboRead> DeleteSingleByIdDbo(string id)
- {
- return DataAccess.DeleteSingleByIdDbo(id);
- }
-
- public LuResult<T> DeleteSingleById<T>(long id, Func<TDboRead, T> returnFunc)
- {
- return DataAccess.DeleteSingleById(id, returnFunc);
- }
-
- public LuResult<long> DeleteSingleByIdId(long id)
- {
- return DataAccess.DeleteSingleByIdId(id);
- }
-
- public LuResult<TDboRead> DeleteSingleByIdDbo(long id)
- {
- return DataAccess.DeleteSingleByIdDbo(id);
- }
- }
- }
|