Example #1
0
        public async Task <IActionResult> Add([FromBody] ExamAddDto model)
        {
            var result = await _examService.Add(model);

            if (result.Message != ApiResultMessages.Ok)
            {
                return(BadRequest(result));
            }


            return(Ok(result));
        }
Example #2
0
        public async Task <ApiResults> Add(ExamAddDto model)
        {
            var entityExam = new Exam
            {
                ExamId    = Guid.NewGuid(),
                IsCreated = DateTime.Now
            };

            entityExam.Name = model.Name;
            await _context.Exam.AddAsync(entityExam);

            foreach (var question in model.Questions)
            {
                var entityQuestion = new Question
                {
                    QuestionId = Guid.NewGuid(),
                    IsCreated  = DateTime.Now
                };
                entityQuestion.QuestionSentence = question.QuestionSentence;
                entityQuestion.AnswerA          = question.AnswerA;
                entityQuestion.AnswerB          = question.AnswerB;
                entityQuestion.AnswerC          = question.AnswerC;
                entityQuestion.AnswerD          = question.AnswerD;
                entityQuestion.CorrectAnswer    = question.CorrectAnswer;
                entityQuestion.Exam             = entityExam;

                await _context.Question.AddAsync(entityQuestion);
            }


            await _context.SaveChangesAsync();

            return(new ApiResults {
                Data = entityExam, Message = ApiResultMessages.Ok
            });
        }