public async Task <IActionResult> AddOpenAnswer(OpenAnswerForCreationDto answerDto) { var user = await _userRepository.GetUserByUsernameAsync(answerDto.Username); var answer = _mapper.Map <OpenAnswer>(answerDto); answer.UserId = user.Id; var option = await _optionRepository.GetOptionById(answerDto.OptionId); var isExerciseSolved = _openAnswerRepository.CheckIfUserAddedAnswerForOption(option.Id, user.Id); if (isExerciseSolved) { throw new Exception("Zadanie zostało już rozwiązane"); } else { if (user != null) { _openAnswerRepository.Add <OpenAnswer>(answer); await _openAnswerRepository.SaveAll(); user.OpenAnswers.Add(answer); await _userRepository.SaveAll(); return(Ok(_mapper.Map <OpenAnswerToReturnDto>(answer))); } } throw new Exception("Nie udało się dodać rozwiązania"); }
//[Authorize] public async Task <IActionResult> DeleteOption(int id) { var option = await _optionRepository.GetOptionById(id); _optionRepository.Delete <Option>(option); if (await _optionRepository.SaveAll()) { return(Ok()); } return(BadRequest("Nie udało się usunąć opcji")); }
public async Task <IActionResult> AddAnswer(OptionAnswerForCreationDto answerDto) { var option = await _optionRepository.GetOptionById(answerDto.OptionId); var exerciseOptions = await _optionRepository.GetOptionsForExercise(option.ExerciseId); // _optionAnswerRepository. var user = await _userRepository.GetUserByUsernameAsync(answerDto.Username); var answer = _mapper.Map <OptionAnswer>(answerDto); answer.UserId = user.Id; var isExerciseSolved = _optionAnswerRepository.CheckIfUserSolvedExercise(option.ExerciseId, user.Id); if (isExerciseSolved) { throw new Exception("Zadanie zostało już rozwiązane"); } else { if (user != null) { foreach (var eo in exerciseOptions) { if (eo.Id != answer.OptionId) { var defaultAnswer = new OptionAnswer(); // todo set null by default for true-false option, for rest options - false defaultAnswer.IsSelected = null; defaultAnswer.OptionId = eo.Id; defaultAnswer.UserId = user.Id; _optionAnswerRepository.Add <OptionAnswer>(defaultAnswer); await _optionAnswerRepository.SaveAll(); user.OptionAnswers.Add(defaultAnswer); await _userRepository.SaveAll(); } } _optionAnswerRepository.Add <OptionAnswer>(answer); await _optionAnswerRepository.SaveAll(); user.OptionAnswers.Add(answer); await _userRepository.SaveAll(); // check if selected option is correct and add points if (option.SubexerciseId != null) { var subexercise = await _subexerciseRepository.GetSubxerciseByIdAsync((int)option.SubexerciseId); var correctOptions = await _optionRepository.GetCorrectOptionsForSubexercise(option.SubexerciseId); var userOptions = await _optionAnswerRepository.GetSelectedOptionsForSubexerciseAsync(option.SubexerciseId, user.Id); // all answers must be correct to get a point var isSolvedCorrect = correctOptions.Count() == userOptions.Count() && userOptions.All(o => correctOptions.Contains(o)); var point = new UserExercisePoint(); point.UserId = user.Id; point.ExerciseId = (int)option.ExerciseId; point.SubexerciseId = option.SubexerciseId; if (isSolvedCorrect) { point.Points = (int)subexercise.Points; } else { point.Points = 0; } _userExercisePointRepository.Add(point); } else { var exercise = await _exerciseRepository.GetExerciseByIdAsync((int)option.ExerciseId); var correctOptions = await _optionRepository.GetCorrectOptionsForExercise(option.ExerciseId); var userOptions = await _optionAnswerRepository.GetSelectedOptionsForExerciseAsync(option.ExerciseId, user.Id); // all answers must be correct to get a point var isSolvedCorrect = correctOptions.Count() == userOptions.Count() && userOptions.All(o => correctOptions.Contains(o)); var point = new UserExercisePoint(); point.UserId = user.Id; point.ExerciseId = exercise.Id; if (isSolvedCorrect) { point.Points = exercise.Points; } else { point.Points = 0; } _userExercisePointRepository.Add(point); } await _userExercisePointRepository.SaveAll(); return(Ok(_mapper.Map <OptionAnswerToReturnDto>(answer))); } } throw new Exception("Nie udało się dodać rozwiązania"); }