using System.Collections.Generic; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using TMSDAL.DTO.Assessment; using TMSDAL.DTO.Quiz; using TMSDAL.Query.Quiz; namespace TMSDAL.CustomCode.Quiz { public class QuizSectionDAL : IQuizSectionDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public QuizSectionDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public async Task GetQuizSection(int quizSectionId, LoginDTO loginDTO) { try { var parameters = new { QuizSectionId = quizSectionId }; var result = await _QueryExecutor.QuerySingleAsync(loginDTO, QuizSectionQB.GET_QUIZSECTION, parameters); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } public async Task GetQuizSectionList(int quizId, LoginDTO loginDTO) { try { var parameters = new { QuizId = quizId }; var result = await _QueryExecutor.QueryAsync(loginDTO, QuizSectionQB.GET_QUIZSECTION_LIST, parameters); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } public async Task SaveQuizSection(QuizSectionDTO dto, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync(loginDTO, QuizSectionQB.SAVE_QUIZSECTION, dto); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } public async Task UpdateQuizSection(QuizSectionDTO dto, LoginDTO loginDTO) { try { return await _QueryExecutor.ExecuteAsync(loginDTO, QuizSectionQB.UPDATE_QUIZSECTION, dto); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task DeleteQuizSection(int quizSectionId, LoginDTO loginDTO) { try { var parameters = new { QuizSectionId = quizSectionId }; int rowsAffected = await _QueryExecutor.ExecuteAsync(loginDTO, QuizSectionQB.DELETE_QUIZSECTION, parameters); return rowsAffected > 0 ? SuccessResponse.DeleteSuccessMessage : ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } public async Task DeleteQuizSectionsByQuiz(int quizId, LoginDTO loginDTO) { try { var parameters = new { QuizId = quizId }; int rowsAffected = await _QueryExecutor.ExecuteAsync(loginDTO, QuizSectionQB.DELETE_QUIZSECTIONS_BY_QUIZ, parameters); return rowsAffected > 0 ? SuccessResponse.DeleteSuccessMessage : ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } public async Task GetQuestionsForQuizSection(int quizSectionId, LoginDTO loginDTO) { try { var sectionParams = new { QuizSectionId = quizSectionId }; var section = await _QueryExecutor.QuerySingleAsync(loginDTO, QuizSectionQB.GET_QUIZSECTION, sectionParams); if (section == null) return JsonConvert.SerializeObject(new List()); var questionParams = new { TenantId = loginDTO.ClientId, QuestionCount = (int)section.QuestionCount, QuestionTypeFilter = section.QuestionTypeFilter, DifficultyFilter = section.DifficultyFilter, IndustryTagFilter = section.IndustryTagFilter, TopicIdFilter = section.TopicIdFilter }; var questions = await _QueryExecutor.QueryAsync(loginDTO, QuizSectionQB.GET_QUESTIONS_FOR_QUIZSECTION, questionParams); return JsonConvert.SerializeObject(questions); } catch (Exception) { throw; } } } }