using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using System.ComponentModel.DataAnnotations; using TMSDAL.CustomCode.AttemptResponse; using TMSDAL.DTO.AttemptResponse; namespace TMSBLL.AttemptResponse { public class AttemptResponseBLL : IAttemptResponseBLL { private readonly IAttemptResponseDAL _AttemptResponseDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public AttemptResponseBLL( IAttemptResponseDAL attemptResponseDAL, AutoNumber autoNumber, IValidation validation) { _AttemptResponseDAL = attemptResponseDAL; _AutoNumber = autoNumber; _Validation = validation; } // ── GET SINGLE ──────────────────────────────────────── public async Task GetAttemptResponse(int attemptResponseId, LoginDTO loginDTO) { try { return await _AttemptResponseDAL.GetAttemptResponse(attemptResponseId, loginDTO); } catch (Exception) { throw; } } // ── GET LIST BY ATTEMPT ─────────────────────────────── public async Task GetAttemptResponseList(int assessmentAttemptId, LoginDTO loginDTO) { try { return await _AttemptResponseDAL.GetAttemptResponseList(assessmentAttemptId, loginDTO); } catch (Exception) { throw; } } // ── SAVE SINGLE RESPONSE ────────────────────────────── public async Task SaveAttemptResponse(AttemptResponseDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(dto.AssessmentAttemptId.ToString(), nameof(dto.AssessmentAttemptId)); autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "ATTEMPTRESPONSE", loginDTO); dto.AttemptResponseId = autoNumberDTO.StartNumber; await _AttemptResponseDAL.SaveAttemptResponse(dto, loginDTO); return string.Format(SuccessResponse.SaveSuccessWithName, dto.ResponseGiven); } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && dto.AttemptResponseId > 0) await _AutoNumber.RollbackAutoNumber("ATTEMPTRESPONSE", dto.AttemptResponseId, loginDTO); throw; } } // ── UPDATE RESPONSE (Evaluator) ─────────────────────── public async Task UpdateAttemptResponse(AttemptResponseDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); try { await _Validation.NotEmpty(dto.AttemptResponseId.ToString(), nameof(dto.AttemptResponseId)); await _AttemptResponseDAL.UpdateAttemptResponse(dto, loginDTO); return string.Format(SuccessResponse.UpdateSuccessWithName, dto.ResponseGiven); } catch (Exception) { throw; } } } }