using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using MMDAL.DTO.ItemCategory; using GB5Shared.DTO.Framework.AutoNumber; using MMDAL.CustomCode.ItemCategory; using GB5Shared.Validation; using System.ComponentModel.DataAnnotations; using GB5Shared.Resource.Response; namespace MMBLL.ItemCategory { public class ItemCategoryBLL : IItemCategoryBLL { private readonly IItemCategoryDAL _ItemCategoryDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public ItemCategoryBLL(IItemCategoryDAL ItemCategoryDAL, AutoNumber AutoNumber, IValidation validation) { _ItemCategoryDAL = ItemCategoryDAL; _AutoNumber = AutoNumber; _Validation = validation; } public async Task GetItemCategory(int ItemCategoryId, LoginDTO LoginDTO) { try { return await _ItemCategoryDAL.GetItemCategory(ItemCategoryId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListItemCategory(CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { return await _ItemCategoryDAL.GetSelectListItemCategory(criteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task SaveItemCategory(ItemCategoryDTO ItemCategoryDTO, LoginDTO LoginDTO) { if (ItemCategoryDTO == null) throw new ArgumentNullException(nameof(ItemCategoryDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(ItemCategoryDTO.ItemCategoryCode, nameof(ItemCategoryDTO.ItemCategoryCode)); await _Validation.NotEmpty(ItemCategoryDTO.ItemCategoryName, nameof(ItemCategoryDTO.ItemCategoryName)); ItemCategoryDTO.ItemCategoryModifiedById = LoginDTO.UserId; ItemCategoryDTO.ItemCategoryModifiedOn = DateTime.UtcNow; int initialId = ItemCategoryDTO.ItemCategoryId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "ITEMCATEGORY", LoginDTO); ItemCategoryDTO.ItemCategoryId = AutoNumberDTO.StartNumber; ItemCategoryDTO.ItemCategoryCreatedById = LoginDTO.UserId; ItemCategoryDTO.ItemCategoryCreatedOn = DateTime.UtcNow; await _ItemCategoryDAL.SaveItemCategory(ItemCategoryDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessCode} {ItemCategoryDTO.ItemCategoryCode}"; } else { await _ItemCategoryDAL.UpdateItemCategory(ItemCategoryDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessCode} {ItemCategoryDTO.ItemCategoryCode}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && ItemCategoryDTO.ItemCategoryId > 0) await _AutoNumber.RollbackAutoNumber("ITEMCATEGORY", ItemCategoryDTO.ItemCategoryId, LoginDTO); throw; } } public async Task DeleteItemCategory(int ItemCategoryId, LoginDTO LoginDTO) { try { return await _ItemCategoryDAL.DeleteItemCategory(ItemCategoryId, LoginDTO); } catch (Exception) { throw; } } } }