Beispiel #1
0
        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));
        }
Beispiel #2
0
        public async Task <ActionResult <List <Difficulty_DTO> > > Get()
        {
            var model = await quizRepo.GetAllDifficultiesAsync();

            List <Difficulty_DTO> model_DTO = new List <Difficulty_DTO>();

            foreach (Difficulty difficulty in model)
            {
                var result = new Difficulty_DTO();
                model_DTO.Add(QuizMapper.ConvertDifficultyTo_DTO(difficulty, ref result));
            }
            return(Ok(model_DTO));
        }