public void EditTest(EditTestDto testDto) { Guard.WhenArgument(testDto, "testDto").IsNull().Throw(); var entity = testRepo.All .Where(t => t.Id.ToString() == testDto.Id) .Include(t => t.Questions) .ThenInclude(q => q.Answers) .FirstOrDefault(); foreach (var question in entity.Questions) { if (testDto.Questions.Any(x => x.Id == question.Id.ToString())) { continue; } this.questionService.DeleteQuestion(question); } foreach (var question in testDto.Questions) { this.questionService.EditQuestion(question, entity.Id); } entity.TestName = testDto.TestName; entity.Duration = TimeSpan.FromMinutes(testDto.Duration); entity.CategoryId = this.categoryRepo.All.Where(x => x.Name == testDto.Category).FirstOrDefault().Id; entity.IsPusblished = testDto.IsPusblished; entity.ModifiedOn = DateTime.Now; this.testRepo.Update(entity); this.Saver.SaveChanges(); }
public async Task <IActionResult> EditTest(EditTestDto editTestDto) { if (!await testService.EditTest(Int32.Parse(editTestDto.Id), editTestDto)) { return(BadRequest(resourceManager.GetString("Null"))); } return(Ok()); }
public Result ValidateEditTestDto(EditTestDto editTestDto) { if (string.IsNullOrWhiteSpace(editTestDto.Title)) { return(Result.Fail(-1)); } return(questionValidator.ValidateEditQuestionDtos(editTestDto.Questions)); }
public async Task <bool> EditTest(int id, EditTestDto editTestDto) { var test = await testRepository.GetTest(id); if (test == null) { return(false); } if (editTestDto.AutomaticCountTime == true) { var questions = await testQuestionRepository.GetAllTestQuestions(test.Id); test.TimeOfTest = 0; foreach (var question in questions) { if ((int)question.Complexity == 3) { test.TimeOfTest += 5; } else if ((int)question.Complexity == 2) { test.TimeOfTest += 3; } else if ((int)question.Complexity == 1) { test.TimeOfTest += 1; } } test.Name = editTestDto.Name; test.AdditionalInfo = editTestDto.AdditionalInfo; test.TypeOfTest = (TypeOfTest)Int32.Parse(editTestDto.TypeOfTest); test.AutomaticTime = editTestDto.AutomaticCountTime; } else { test.Name = editTestDto.Name; test.AdditionalInfo = editTestDto.AdditionalInfo; test.TimeOfTest = Int32.Parse(editTestDto.TimeOfTest); test.TypeOfTest = (TypeOfTest)Int32.Parse(editTestDto.TypeOfTest); } testRepository.Update(test); await testRepository.SaveChangesAsync(); return(true); }
public async Task <Result> UpdateTest(EditTestDto editTestDto) { var validationResult = testValidator.ValidateEditTestDto(editTestDto); if (validationResult.Failure) { return(validationResult); } var questions = mapper.Map <IEnumerable <Question> >(editTestDto.Questions); var storedTest = await dataStorage.Tests.GetAsync(editTestDto.Id); var except = storedTest.Questions.Where(st => questions.FirstOrDefault(q => q.Id == st.Id) is null); foreach (var question in except) { dataStorage.Questions.Delete(question); } var enumerable = storedTest.Questions.SelectMany( sq => sq.Answers.Where(st => questions.SelectMany(q => q.Answers).FirstOrDefault(a => a.Id == st.Id) is null) ); foreach (var answer in enumerable) { dataStorage.Answers.Delete(answer); } storedTest.Questions = questions as ICollection <Question>; storedTest.Title = editTestDto.Title; storedTest.Time = TimeSpan.FromMilliseconds(editTestDto.Time); storedTest.Description = editTestDto.Description; dataStorage.Tests.Update(storedTest); await dataStorage.SaveChangesAsync(); return(Result.Ok()); }