using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using TMSDAL.DTO.AttemptResponse; using TMSDAL.Query.AttemptResponse; namespace TMSDAL.CustomCode.AttemptResponse { public class AttemptResponseDAL : IAttemptResponseDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public AttemptResponseDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } // ── GET SINGLE ──────────────────────────────────────── public async Task GetAttemptResponse(int attemptResponseId, LoginDTO loginDTO) { try { var result = await _QueryExecutor.QuerySingleAsync( loginDTO, AttemptResponseQB.GET_ATTEMPTRESPONSE, new { AttemptResponseId = attemptResponseId }); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } // ── GET LIST BY ATTEMPT ─────────────────────────────── public async Task GetAttemptResponseList(int assessmentAttemptId, LoginDTO loginDTO) { try { var result = await _QueryExecutor.QueryAsync( loginDTO, AttemptResponseQB.GET_ATTEMPTRESPONSE_LIST, new { AssessmentAttemptId = assessmentAttemptId }); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } // ── SAVE ────────────────────────────────────────────── public async Task SaveAttemptResponse(AttemptResponseDTO dto, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync( loginDTO, AttemptResponseQB.SAVE_ATTEMPTRESPONSE, dto); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } // ── UPDATE (Evaluator) ──────────────────────────────── public async Task UpdateAttemptResponse(AttemptResponseDTO dto, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync( loginDTO, AttemptResponseQB.UPDATE_ATTEMPTRESPONSE, dto); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } // ── UPDATE BY QUESTION (Auto-evaluation on Submit) ─── public async Task UpdateAttemptResponseByQuestion(AttemptResponseDTO dto, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync( loginDTO, AttemptResponseQB.UPDATE_ATTEMPTRESPONSE_BY_QUESTION, dto); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } // ── UPSERT BY QUESTION (Submit flow — INSERT or UPDATE) ─ public async Task UpsertAttemptResponseByQuestion(AttemptResponseDTO dto, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync( loginDTO, AttemptResponseQB.UPSERT_ATTEMPTRESPONSE_BY_QUESTION, dto); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } // ── DELETE BY ATTEMPT ───────────────────────────────── public async Task DeleteAttemptResponsesByAttempt(int assessmentAttemptId, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync(loginDTO, AttemptResponseQB.DELETE_ATTEMPTRESPONSES_BY_ATTEMPT, new { AssessmentAttemptId = assessmentAttemptId }); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } } }