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.ComponentModel.DataAnnotations; using TMSDAL.CustomCode.PaperSection; using TMSDAL.DTO.Assessment; namespace TMSBLL.PaperSection { public class PaperSectionBLL : IPaperSectionBLL { private readonly IPaperSectionDAL _PaperSectionDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public PaperSectionBLL( IPaperSectionDAL paperSectionDAL, AutoNumber autoNumber, IValidation validation) { _PaperSectionDAL = paperSectionDAL; _AutoNumber = autoNumber; _Validation = validation; } // ═══════════════════════════════════════════════════════════════════════ // GET // ═══════════════════════════════════════════════════════════════════════ public async Task GetPaperSection(int paperSectionId, LoginDTO loginDTO) { try { return await _PaperSectionDAL.GetPaperSection(paperSectionId, loginDTO); } catch (Exception) { throw; } } public async Task GetPaperSectionList(int assessmentPaperId, LoginDTO loginDTO) { try { return await _PaperSectionDAL.GetPaperSectionList(assessmentPaperId, loginDTO); } catch (Exception) { throw; } } public async Task GetSelectListPaperSection(CriteriaDTO criteriaDTO, LoginDTO loginDTO) => await _PaperSectionDAL.GetSelectListPaperSection(criteriaDTO, loginDTO); // ═══════════════════════════════════════════════════════════════════════ // SAVE / UPDATE // ═══════════════════════════════════════════════════════════════════════ public async Task SavePaperSection(PaperSectionDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { // ── Validations ────────────────────────────────────────────── await _Validation.NotEmpty(dto.SectionTitle, nameof(dto.SectionTitle)); string message; if (dto.PaperSectionId == 0) { // ── INSERT ─────────────────────────────────────────────── autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "PAPERSECTION", loginDTO); dto.PaperSectionId = autoNumberDTO.StartNumber; await _PaperSectionDAL.SavePaperSection(dto, loginDTO); message = string.Format(SuccessResponse.SaveSuccessWithName, dto.SectionTitle); } else { // ── UPDATE ─────────────────────────────────────────────── await _PaperSectionDAL.UpdatePaperSection(dto, loginDTO); message = string.Format(SuccessResponse.UpdateSuccessWithName, dto.SectionTitle); } return message; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { // ── Rollback AutoNumber on failure ─────────────────────────── if (autoNumberDTO != null && dto.PaperSectionId > 0) await _AutoNumber.RollbackAutoNumber("PAPERSECTION", dto.PaperSectionId, loginDTO); throw; } } // ═══════════════════════════════════════════════════════════════════════ // DELETE // ═══════════════════════════════════════════════════════════════════════ public async Task DeletePaperSection(int paperSectionId, LoginDTO loginDTO) { try { return await _PaperSectionDAL.DeletePaperSection(paperSectionId, loginDTO); } catch (Exception) { throw; } } public async Task DeletePaperSectionsByPaper(int assessmentPaperId, LoginDTO loginDTO) { try { int rows = await _PaperSectionDAL.DeletePaperSectionsByPaper(assessmentPaperId, loginDTO); return rows > 0 ? $"{SuccessResponse.DeleteSuccessMessage} {rows} section(s) removed." : ErrorResponse.DeleteNotFoundMessage; } catch (Exception) { throw; } } } }