using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using FrameworkDAL.CustomCode.EntityRelation; 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.EntityRelation { public class EntityRelationBLL : IEntityRelationBLL { private readonly IEntityRelationDAL _EntityRelationDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public EntityRelationBLL(IEntityRelationDAL EntityRelationDAL, AutoNumber AutoNumber, IValidation Validation) { _EntityRelationDAL = EntityRelationDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetEntityRelation(int EntityRelationId, LoginDTO LoginDTO) { try { return await _EntityRelationDAL.GetEntityRelation(EntityRelationId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveEntityRelation(EntityRelationDTO EntityRelationDTO, LoginDTO LoginDTO) { if (EntityRelationDTO == null) throw new ArgumentNullException(nameof(EntityRelationDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(EntityRelationDTO.EntityRelationRelatedEntityCode, nameof(EntityRelationDTO.EntityRelationRelatedEntityCode)); await _Validation.NotEmpty(EntityRelationDTO.EntityRelationName, nameof(EntityRelationDTO.EntityRelationName)); int count = 1; if (EntityRelationDTO.EntityRelationId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "ENTITYRELATION", LoginDTO); EntityRelationDTO.EntityRelationId = autoNumberDTO.StartNumber; await _EntityRelationDAL.SaveEntityRelation(EntityRelationDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {EntityRelationDTO.EntityRelationId}"; } else { await _EntityRelationDAL.UpdateEntityRelation(EntityRelationDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {EntityRelationDTO.EntityRelationId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && EntityRelationDTO.EntityRelationId > 0) await _AutoNumber.RollbackAutoNumber("ENTITYRELATION", EntityRelationDTO.EntityRelationId, LoginDTO); throw; } } public async Task DeleteEntityRelation(int EntityRelationId, LoginDTO LoginDTO) { try { return await _EntityRelationDAL.DeleteEntityRelation(EntityRelationId, LoginDTO); } catch (Exception) { throw; } } } }