public async Task <Result> HandleAsync(DeleteExamCommand command) { var exam = await _examsRepository.GetByIdAsync(_examId); if (exam == null) { throw new NullReferenceException($"Exam with ID '{_examId}' was not found."); } await _examsRepository.RemoveAsync(exam); return(Result.Success()); }
public async Task <Result <ExamWithQuestionsResult> > HandleAsync(GetExamByIdQuery query) { var exam = await _examsRepository.GetByIdAsync(query.Id); if (exam == null) { return(Result.Failure(query, "Exam with given Id does not exists.")); } var result = _mapper.Map <ExamWithQuestionsResult>(exam); result.Questions = result.Questions.OrderBy(x => x.Order).ToList(); return(Result.Success(result)); }
public async Task <Result> HandleAsync(EditExamCommand command) { var exam = await _examsRepository.GetByIdAsync(_examId); if (exam == null) { return(Result.Failure($"Exam with ID '{_examId}' was not found.")); } exam.SetName(command.Name); exam.ChangeType(command.Type); exam.SetCategory(command.Category); var commandQuestionIds = command.Questions .Where(x => x.Id.HasValue) .Select(p => p.Id.Value) .ToList(); exam.Questions .Select(x => x.Id) .Except(commandQuestionIds) .ToList() .ForEach(id => exam.RemoveQuestion(id)); foreach (var q in command.Questions) { if (q.Id.HasValue) { var existedQuestion = exam.Questions.SingleOrDefault(x => x.Id == q.Id); if (existedQuestion != null) { existedQuestion.SetOrder(q.Order); existedQuestion.SetText(q.Text); existedQuestion.SetAnswers(q.A, q.B, q.C, q.D); existedQuestion.SetExplantation(q.Explanation); existedQuestion.ChangeCorrectAnswer(q.CorrectAnswer); continue; } } exam.AddQuestion(q.Order, q.Text, q.A, q.B, q.C, q.D, q.CorrectAnswer, q.Explanation); } await _examsRepository.UpdateAsync(exam); return(Result.Success()); }
public async Task <Exam> GetExamByIdAsync(Guid id) { return(await _examsRepository.GetByIdAsync(id)); }