using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using FrameworkDAL.CustomCode.EntityMember; using FrameworkDAL.DTO.Entity; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; namespace FrameworkBLL.EntityMember { public class EntityMemberBLL : IEntityMemberBLL { private readonly IEntityMemberDAL _EntityMemberDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public EntityMemberBLL(IEntityMemberDAL EntityMemberDAL, AutoNumber AutoNumber, IValidation Validation) { _EntityMemberDAL = EntityMemberDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetEntityMember(int EntityMemberId, LoginDTO LoginDTO) { try { return await _EntityMemberDAL.GetEntityMember(EntityMemberId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveEntityMember(EntityMemberDTO EntityMemberDTO, LoginDTO LoginDTO) { if (EntityMemberDTO == null) throw new ArgumentNullException(nameof(EntityMemberDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(EntityMemberDTO.EntityMemberMemberName, nameof(EntityMemberDTO.EntityMemberMemberName)); int count = 1; if (EntityMemberDTO.EntityMemberId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "ENTITYMEMBER", LoginDTO); EntityMemberDTO.EntityMemberId = autoNumberDTO.StartNumber; await _EntityMemberDAL.SaveEntityMember(EntityMemberDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {EntityMemberDTO.EntityMemberId}"; } else { await _EntityMemberDAL.UpdateEntityMember(EntityMemberDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {EntityMemberDTO.EntityMemberId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && EntityMemberDTO.EntityMemberId > 0) await _AutoNumber.RollbackAutoNumber("ENTITYMEMBER", EntityMemberDTO.EntityMemberId, LoginDTO); throw; } } public async Task DeleteEntityMember(int EntityMemberId, LoginDTO LoginDTO) { try { return await _EntityMemberDAL.DeleteEntityMember(EntityMemberId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListEntityMember(CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { return await _EntityMemberDAL.GetSelectListEntityMember(criteriaDTO, LoginDTO); } catch (Exception) { throw; } } } }