using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; 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; using MMDAL.CustomCode.ItemSubCategory; using MMDAL.DTO.ItemSubCategory; namespace MMBLL.ItemSubCategory { public class ItemSubCategoryBLL : IItemSubCategoryBLL { private readonly IItemSubCategoryDAL _ItemSubCategoryDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public ItemSubCategoryBLL(IItemSubCategoryDAL ItemSubCategoryDAL, AutoNumber AutoNumber, IValidation validation) { _ItemSubCategoryDAL = ItemSubCategoryDAL; _AutoNumber = AutoNumber; _Validation = validation; } public async Task GetItemSubCategory(int ItemSubCategoryId, LoginDTO LoginDTO) { try { return await _ItemSubCategoryDAL.GetItemSubCategory(ItemSubCategoryId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveItemSubCategory(ItemSubCategoryDTO ItemSubCategoryDTO, LoginDTO LoginDTO) { if (ItemSubCategoryDTO == null) throw new ArgumentNullException(nameof(ItemSubCategoryDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(ItemSubCategoryDTO.ItemSubCategoryCode, nameof(ItemSubCategoryDTO.ItemSubCategoryCode)); await _Validation.NotEmpty(ItemSubCategoryDTO.ItemSubCategoryName, nameof(ItemSubCategoryDTO.ItemSubCategoryName)); ItemSubCategoryDTO.ItemSubCategoryModifiedById = LoginDTO.UserId; ItemSubCategoryDTO.ItemSubCategoryModifiedOn = DateTime.UtcNow; int InitialId = ItemSubCategoryDTO.ItemSubCategoryId; int count = 1 + InitialId; if (InitialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "ITEMSUBCATEGORY", LoginDTO); ItemSubCategoryDTO.ItemSubCategoryId = AutoNumberDTO.StartNumber; ItemSubCategoryDTO.ItemSubCategoryCreatedById = LoginDTO.UserId; ItemSubCategoryDTO.ItemSubCategoryCreatedOn = DateTime.UtcNow; await _ItemSubCategoryDAL.SaveItemSubCategory(ItemSubCategoryDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessName} {ItemSubCategoryDTO.ItemSubCategoryName}"; } else { await _ItemSubCategoryDAL.UpdateItemSubCategory(ItemSubCategoryDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessName} {ItemSubCategoryDTO.ItemSubCategoryName}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && ItemSubCategoryDTO.ItemSubCategoryId > 0) await _AutoNumber.RollbackAutoNumber("ITEMSUBCATEGORY", ItemSubCategoryDTO.ItemSubCategoryId, LoginDTO); throw; } } public async Task GetSelectListItemSubCategory(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _ItemSubCategoryDAL.GetSelectListItemSubCategory(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task DeleteItemSubCategory(int ItemSubCategoryId, LoginDTO LoginDTO) { try { return await _ItemSubCategoryDAL.DeleteItemSubCategory(ItemSubCategoryId, LoginDTO); } catch (Exception) { throw; } } } }