using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using TMSDAL.DTO.Assessment; using TMSDAL.Query.PaperSection; using TMSDAL.Query.QuestionBank; namespace TMSDAL.CustomCode.PaperSection { public class PaperSectionDAL : IPaperSectionDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public PaperSectionDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } // ===================================================== // GET SINGLE // ===================================================== public async Task GetPaperSection(int paperSectionId, LoginDTO loginDTO) { try { // Step 1: Get section header var section = await _QueryExecutor.QuerySingleAsync( loginDTO, PaperSectionQB.GET_PAPERSECTION, new { PaperSectionId = paperSectionId }); if (section == null) return JsonConvert.SerializeObject(null); // Step 2: Pull matching questions using section filters section.Questions = (await _QueryExecutor.QueryAsync( loginDTO, QuestionBankQB.GET_QUESTIONS_FOR_SECTION, new { TenantId = loginDTO.ClientId, QuestionTypeFilter = section.QuestionTypeFilter, DifficultyFilter = section.DifficultyFilter, IndustryTagFilter = section.IndustryTagFilter, QuestionCount = section.QuestionCount })).ToList(); return JsonConvert.SerializeObject(section); } catch (Exception) { throw; } } // ===================================================== // GET LIST BY PAPER // ===================================================== public async Task GetPaperSectionList(int assessmentPaperId, LoginDTO loginDTO) { try { // Step 1: Get all sections for this paper var sections = (await _QueryExecutor.QueryAsync( loginDTO, PaperSectionQB.GET_PAPERSECTION_LIST, new { AssessmentPaperId = assessmentPaperId })).ToList(); if (!sections.Any()) return JsonConvert.SerializeObject(sections); // Step 2: For each section fetch matching questions foreach (var section in sections) { section.Questions = (await _QueryExecutor.QueryAsync( loginDTO, QuestionBankQB.GET_QUESTIONS_FOR_SECTION, new { TenantId = loginDTO.ClientId, QuestionTypeFilter = section.QuestionTypeFilter, DifficultyFilter = section.DifficultyFilter, IndustryTagFilter = section.IndustryTagFilter, QuestionCount = section.QuestionCount })).ToList(); } return JsonConvert.SerializeObject(sections); } catch (Exception) { throw; } } // ===================================================== // GET SELECTLIST — Criteria Based // ===================================================== public async Task GetSelectListPaperSection(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { var result = await _QueryExecutor.QueryWithCriteriaAsync( loginDTO, PaperSectionQB.GET_SELECTLIST_PAPERSECTION, new { TenantId = loginDTO.ClientId }, criteriaDTO); return JsonConvert.SerializeObject(result); } // ===================================================== // SAVE // ===================================================== public async Task SavePaperSection(PaperSectionDTO dto, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync( loginDTO, PaperSectionQB.SAVE_PAPERSECTION, dto); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } // ===================================================== // UPDATE // ===================================================== public async Task UpdatePaperSection(PaperSectionDTO dto, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync( loginDTO, PaperSectionQB.UPDATE_PAPERSECTION, dto); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } // ===================================================== // DELETE SINGLE // ===================================================== public async Task DeletePaperSection(int paperSectionId, LoginDTO loginDTO) { try { var parameters = new { PaperSectionId = paperSectionId }; int rowsAffected = await _QueryExecutor.ExecuteAsync( loginDTO, PaperSectionQB.DELETE_PAPERSECTION, parameters); return rowsAffected > 0 ? SuccessResponse.DeleteSuccessMessage : ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } // ===================================================== // DELETE ALL BY PAPER // ===================================================== public async Task DeletePaperSectionsByPaper(int assessmentPaperId, LoginDTO loginDTO) { try { var parameters = new { AssessmentPaperId = assessmentPaperId }; return await _QueryExecutor.ExecuteAsync( loginDTO, PaperSectionQB.DELETE_PAPERSECTIONS_BY_PAPER, parameters); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } } }