Exemplo n.º 1
0
        public async Task <ActionResult <AnswerResponse> > PostTextAnswer(DTO.Answer <string> input)
        {
            var question = await _db.Questions.FindAsync(input.QuestionId);

            if (question == null)
            {
                return(NotFound(new ErrorModel
                {
                    Reason = "Question with the provided QuestionId does not exist",
                    Error = "Question Not Found"
                }));
            }

            var answer = new Data.Answer
            {
                AnswerContent   = input.AnswerContent,
                QuestionId      = input.QuestionId,
                IsAnswerCorrect = input.IsAnswerCorrect,
            };

            _db.Answers.Add(answer);

            question.QuestionAnswers = new List <QuestionAnswer>();

            question.QuestionAnswers.Add(new QuestionAnswer
            {
                Question = question,
                Answer   = answer
            });
            var result = answer.MapAnswerResponse();
            await _db.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { Id = result.Id }, result));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <AnswerResponse> > PostImageAnswer(DTO.Answer <IFormFile> input)
        {
            var question = await _db.Questions.FindAsync(input.QuestionId);

            if (question == null)
            {
                return(NotFound(new ErrorModel
                {
                    Reason = "Question with the provided QuestionId does not exist",
                    Error = "Question Not Found"
                }));
            }

            var imageResponse = await _imageUploader.DataLoaderAsync(input.AnswerContent, "answers");

            if (!imageResponse.IsSuccess)
            {
                return(BadRequest(new ErrorModel
                {
                    Error = imageResponse.Data,
                    Reason = imageResponse.Message
                }));
            }

            var answer = new Data.Answer
            {
                AnswerContent   = imageResponse.Data,
                QuestionId      = input.QuestionId,
                IsAnswerCorrect = input.IsAnswerCorrect,
            };

            _db.Answers.Add(answer);

            question.QuestionAnswers = new List <QuestionAnswer>();

            question.QuestionAnswers.Add(new QuestionAnswer
            {
                Question = question,
                Answer   = answer
            });
            var result = answer.MapAnswerResponse();
            await _db.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { Id = result.Id }, result));
        }