using CMSDAL.CustomCode.ContentCategory; using CMSDAL.DTO.ContentCategory; 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 System; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; namespace CMSBLL.ContentCategory { public class ContentCategoryBLL : IContentCategoryBLL { private readonly IContentCategoryDAL _contentCategoryDAL; private readonly AutoNumber _autoNumber; private readonly IValidation _validation; public ContentCategoryBLL( IContentCategoryDAL contentCategoryDAL, AutoNumber autoNumber, IValidation validation) { _contentCategoryDAL = contentCategoryDAL; _autoNumber = autoNumber; _validation = validation; } public async Task GetContentCategory(int contentCategoryId, LoginDTO loginDTO) { try { return await _contentCategoryDAL.GetContentCategory(contentCategoryId, loginDTO); } catch (Exception) { throw; } } public async Task SaveContentCategory(ContentCategoryDTO contentCategoryDTO, LoginDTO loginDTO) { if (contentCategoryDTO == null) throw new ArgumentNullException(nameof(contentCategoryDTO)); AutoNumberDTO? autoNumberDTO = null; try { // 🔹 Validation (based on your table) await _validation.NotEmpty(contentCategoryDTO.ContentCategoryName, nameof(contentCategoryDTO.ContentCategoryName)); await _validation.NotEmpty(contentCategoryDTO.ContentCategoryCode, nameof(contentCategoryDTO.ContentCategoryCode)); contentCategoryDTO.ModifiedById = loginDTO.UserId; contentCategoryDTO.ModifiedOn = DateTime.UtcNow; int count = 1; if (contentCategoryDTO.ContentCategoryId == 0) { // 🔹 AutoNumber autoNumberDTO = await _autoNumber.GetAutoNumber(count, "ContentCategory", loginDTO); contentCategoryDTO.ContentCategoryId = autoNumberDTO.StartNumber; contentCategoryDTO.CreatedById = loginDTO.UserId; contentCategoryDTO.CreatedOn = DateTime.UtcNow; await _contentCategoryDAL.SaveContentCategory(contentCategoryDTO, loginDTO); return $"{SuccessResponse.SaveSuccessMessage} {contentCategoryDTO.ContentCategoryId}"; } else { await _contentCategoryDAL.UpdateContentCategory(contentCategoryDTO, loginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {contentCategoryDTO.ContentCategoryId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && contentCategoryDTO.ContentCategoryId > 0) { await _autoNumber.RollbackAutoNumber("ContentCategory", contentCategoryDTO.ContentCategoryId, loginDTO); } throw; } } public async Task GetSelectListContentCategory(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _contentCategoryDAL.GetSelectListContentCategory(firstNumber, maxResult, criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task DeleteContentCategory(int contentCategoryId, LoginDTO loginDTO) { try { return await _contentCategoryDAL.DeleteContentCategory(contentCategoryId, loginDTO); } catch (Exception) { throw; } } } }