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.ItemModel; using MMDAL.DTO.Model; using MMDAL.Query.ItemModel; namespace MMBLL.ItemModel { public class ItemModelBLL : IItemModelBLL { private readonly IItemModelDAL _ItemModelDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public ItemModelBLL(IItemModelDAL IItemModelDAL, AutoNumber AutoNumber, IValidation Validation) { _ItemModelDAL = IItemModelDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetItemModel(int ModelId, LoginDTO LoginDTO) { try { return await _ItemModelDAL.GetItemModel(ModelId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListItemModel(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _ItemModelDAL.GetSelectListItemModel(CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task SaveItemModel(ModelDTO ItemModelDTO, LoginDTO LoginDTO) { if (ItemModelDTO == null) throw new ArgumentNullException(nameof(ItemModelDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(ItemModelDTO.ModelCode, nameof(ItemModelDTO.ModelCode)); await _Validation.NotEmpty(ItemModelDTO.ModelName, nameof(ItemModelDTO.ModelName)); await _Validation.NotEmpty(ItemModelDTO.ModelShortName, nameof(ItemModelDTO.ModelShortName)); ItemModelDTO.ModelModifiedById = LoginDTO.UserId; ItemModelDTO.ModelModifiedOn = DateTime.UtcNow; int count = 1; if (ItemModelDTO.ModelId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "MODEL", LoginDTO); ItemModelDTO.ModelId = autoNumberDTO.StartNumber; ItemModelDTO.ModelCreatedById = LoginDTO.UserId; ItemModelDTO.ModelCreatedOn = DateTime.UtcNow; await _ItemModelDAL.SaveItemModel(ItemModelDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {ItemModelDTO.ModelId}"; } else { await _ItemModelDAL.UpdateItemModel(ItemModelDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {ItemModelDTO.ModelId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && ItemModelDTO.ModelId > 0) await _AutoNumber.RollbackAutoNumber("MODEL", ItemModelDTO.ModelId, LoginDTO); throw; } } public async Task DeleteItemModel(int ModelId, LoginDTO LoginDTO) { try { return await _ItemModelDAL.DeleteItemModel(ModelId, LoginDTO); } catch (Exception) { throw; } } } }