public async Task <ActionResult> CreateQuiz(IFormCollection collection, Quiz quiz) { try { if (!ModelState.IsValid) { return(BadRequest()); } Person person = await quizRepo.GetPersonForIdAsync(userManager.GetUserId(User)); quiz.Person = person; var created = await quizRepo.AddQuiz(quiz); if (created == null) { return(RedirectToAction(nameof(Quizzes), new { exc = "Quiz did not create" })); } return(RedirectToAction(nameof(Quizzes))); } catch (Exception ex) { return(RedirectToAction(nameof(Quizzes), new { exc = ex.Message })); } }
public async Task <IActionResult> Post([FromForm] Quiz_DTO quiz_DTO) { var confirmedModel = new Quiz(); //te returnen DTO try { var obj = new Difficulty_DTO(); quiz_DTO.Difficulty = QuizMapper.ConvertDifficultyTo_DTO(await quizRepo.GetDifficultyForIdAsync(quiz_DTO.DifficultyId), ref obj); //1. Validatie if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //2.Entity (model) via de mapper ophalen var model = new Quiz() { }; QuizMapper.ConvertQuizTo_Entity(quiz_DTO, ref model); //3. Entity (model) toevoegen via het repo confirmedModel = await quizRepo.AddQuiz(model); //4. Een bevestiging v foutieve actie teruggeven if (confirmedModel == null) { return(NotFound(model.Subject + " did not get saved. ")); } } catch (Exception exc) { return(BadRequest("Add quiz failed")); } //5. DTO terugsturen als bevestiging return(CreatedAtAction("Get", new { id = confirmedModel.Id }, quiz_DTO)); }
public void AddQuiz(AddQuizRequest request) { quizRepo.AddQuiz(new Quiz { Name = request.Name, Description = request.Description }); }
public async Task <ActionResult> CreateQuiz(IFormCollection collection, AddQuiz_VM vm) { try { if (ModelState.IsValid) { if (vm.ImgUrl == null) { vm.ImgUrl = "https://wallpaperaccess.com/full/2384075.jpg"; } Quiz quiz = new Quiz() { SubjectId = vm.SubjectId, QuizName = vm.QuizName, QuizID = Guid.NewGuid(), ImgUrl = vm.ImgUrl, Difficulty = vm.Difficulty, Description = vm.Description }; Quiz result = await quizRepo.AddQuiz(quiz); if (result == null) { throw new Exception("Invalid Entry"); } return(RedirectToAction(nameof(Quizzes))); } return(View(vm)); } catch (Exception ex) { Debug.WriteLine("unable to create" + ex.Message); ModelState.AddModelError("", "Create mislukt." + ex.Message); return(View(vm)); } }
public async Task <ActionResult> CreateAsync(IFormCollection collection, Quiz quiz) { try { if (!ModelState.IsValid) { throw new Exception("Validation Error"); } Quiz result = await quizRepo.AddQuiz(quiz); if (result == null) { throw new Exception("Invalid Entry"); } return(RedirectToAction(nameof(Index))); } catch (Exception ex) { Debug.WriteLine("unable to create" + ex.Message); ModelState.AddModelError("", "Create mislukt." + ex.Message); return(View(quiz)); } }