using System; using System.Collections.Generic; using Luticate2.Utils.Dbo; using Luticate2.Utils.Dbo.OrderBy; using Luticate2.Utils.Interfaces; namespace Luticate2.Utils.Business { public abstract class LuCrudBusiness : LuBusiness, ILuCrudInterface where TNextCrud : ILuCrudInterface where TDboCreate : class where TDboRead : class where TDboUpdate : class { protected readonly TNextCrud NextCrud; protected LuCrudBusiness(TNextCrud nextCrud) { NextCrud = nextCrud; } protected virtual LuResult CheckAdd(TDboCreate obj) { return LuResult.Ok(obj); } protected virtual LuResult> CheckAdd(IEnumerable objs) { var list = new List(); foreach (var obj in objs) { var res = CheckAdd(obj); if (!res) { return res.To>(); } list.Add(res.Data); } return LuResult>.Ok(list); } protected virtual LuResult CheckEdit(TDboRead dbo, TDboUpdate update) { return LuResult.Ok(update); } protected LuResult GetAndCheckEdit(string id, TDboUpdate update) { var res = GetSingleById(id); if (!res) { return res.To(); } return CheckEdit(res.Data, update); } protected LuResult GetAndCheckEdit(long id, TDboUpdate update) { var res = GetSingleById(id); if (!res) { return res.To(); } return CheckEdit(res.Data, update); } public LuResult Add(IEnumerable objs, Func, T> returnFunc) { var res = CheckAdd(objs); if (!res) { return res.To(); } return NextCrud.Add(res.Data, returnFunc); } public LuResult Add(TDboCreate obj, Func returnFunc) { var res = CheckAdd(obj); if (!res) { return res.To(); } return NextCrud.Add(res.Data, returnFunc); } public LuResult> AddGuid(IEnumerable objs) { var res = CheckAdd(objs); if (!res) { return res.To>(); } return NextCrud.AddGuid(res.Data); } public LuResult AddGuid(TDboCreate obj) { var res = CheckAdd(obj); if (!res) { return res.To(); } return NextCrud.AddGuid(res.Data); } public LuResult> AddId(IEnumerable obj) { var res = CheckAdd(obj); if (!res) { return res.To>(); } return NextCrud.AddId(res.Data); } public LuResult AddId(TDboCreate obj) { var res = CheckAdd(obj); if (!res) { return res.To(); } return NextCrud.AddId(res.Data); } public LuResult> AddDbo(IEnumerable obj) { var res = CheckAdd(obj); if (!res) { return res.To>(); } return NextCrud.AddDbo(res.Data); } public LuResult AddDbo(TDboCreate obj) { var res = CheckAdd(obj); if (!res) { return res.To(); } return NextCrud.AddDbo(res.Data); } public LuResult GetSingleById(string id) { return NextCrud.GetSingleById(id); } public LuResult GetSingleById(long id) { return NextCrud.GetSingleById(id); } public LuResult> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue) { return NextCrud.GetMultiple(orderBy, 0, perPage); } public LuResult EditSingleById(long id, TDboUpdate update, Func returnFunc) { var obj = GetAndCheckEdit(id, update); if (!obj) { return obj.To(); } return NextCrud.EditSingleById(id, obj.Data, returnFunc); } public LuResult EditSingleByIdId(long id, TDboUpdate update) { var obj = GetAndCheckEdit(id, update); if (!obj) { return obj.To(); } return NextCrud.EditSingleByIdId(id, obj.Data); } public LuResult EditSingleByIdDbo(long id, TDboUpdate update) { var obj = GetAndCheckEdit(id, update); if (!obj) { return obj.To(); } return NextCrud.EditSingleByIdDbo(id, obj.Data); } public LuResult EditSingleById(string id, TDboUpdate update, Func returnFunc) { var obj = GetAndCheckEdit(id, update); if (!obj) { return obj.To(); } return NextCrud.EditSingleById(id, obj.Data, returnFunc); } public LuResult EditSingleByIdGuid(string id, TDboUpdate update) { var obj = GetAndCheckEdit(id, update); if (!obj) { return obj.To(); } return NextCrud.EditSingleByIdGuid(id, obj.Data); } public LuResult EditSingleByIdDbo(string id, TDboUpdate update) { var obj = GetAndCheckEdit(id, update); if (!obj) { return obj.To(); } return NextCrud.EditSingleByIdDbo(id, obj.Data); } public LuResult DeleteSingleById(string id, Func returnFunc) { return NextCrud.DeleteSingleById(id, returnFunc); } public LuResult DeleteSingleByIdGuid(string id) { return NextCrud.DeleteSingleByIdGuid(id); } public LuResult DeleteSingleByIdDbo(string id) { return NextCrud.DeleteSingleByIdDbo(id); } public LuResult DeleteSingleById(long id, Func returnFunc) { return NextCrud.DeleteSingleById(id, returnFunc); } public LuResult DeleteSingleByIdId(long id) { return NextCrud.DeleteSingleByIdId(id); } public LuResult DeleteSingleByIdDbo(long id) { return NextCrud.DeleteSingleByIdDbo(id); } } }