コード例 #1
0
        public ActionResult <QuestionShallowDTO> Put(long id, [FromBody] CreateQuestionForm question)
        {
            if (string.IsNullOrEmpty(question.Content))
            {
                return(BadRequest());
            }

            var quest = _context.Questions.FirstOrDefault(x => x.Id == id);

            if (quest != null)
            {
                var category = _context.Categories.FirstOrDefault(x => x.Id == question.CategoryId);

                if (category == null)
                {
                    return(BadRequest());
                }

                quest.Category = category;
                quest.Content  = question.Content;
                _context.Questions.Update(quest);
                _context.SaveChanges();

                return(Ok(new QuestionShallowDTO(quest)));
            }
            else
            {
                return(NoContent());
            }
        }
コード例 #2
0
        public ActionResult <QuestionShallowDTO> Post([FromBody] CreateQuestionForm question)
        {
            if (string.IsNullOrEmpty(question.Content))
            {
                return(BadRequest());
            }

            var category = _context.Categories.FirstOrDefault(x => x.Id == question.CategoryId);

            if (category == null)
            {
                return(BadRequest());
            }

            var quest = new Question
            {
                Category = category,
                Content  = question.Content
            };

            _context.Questions.Add(quest);
            _context.SaveChanges();

            return(Ok(new QuestionShallowDTO(quest)));
        }