public ActionResult <QuestionFormDTO> CreateNewQuestionForm(QuestionFormDTO questionFormDTO) { // Handle error if no data is sent. if (questionFormDTO == null) { return(BadRequest("QuestionForm data must be set!")); } try { // Map the DTO to entity and save the entity QuestionForm createdEntity = _service.SaveQuestionForm(questionFormDTO.ToEntity()); // According to the conventions, we have to return a HTTP 201 created repsonse, with field // "Location" in the header pointing to the created object return(CreatedAtAction( nameof(GetQuestionForm), new { id = createdEntity.Id }, new QuestionFormDTO(createdEntity))); } catch (QuestionFormExistsException) { return(Conflict("The desired ID for the QuestionForm is already taken!")); } }
public IHttpActionResult AskQusetion(QuestionFormDTO questionForm) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Question question = new Question { Title = questionForm.Title, Body = questionForm.Body, CreateDate = DateTime.Now }; db.Questions.Add(question); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = question.Id }, question)); }
public ActionResult UpdateQuestionForm(long id, QuestionFormDTO questionFormDTO) { // Handle error if no data is sent. if (questionFormDTO == null) { return(BadRequest("QuestionForm data must be set!")); } try { // Map the DTO to entity and save it _service.UpdateQuestionForm(id, questionFormDTO.ToEntity()); // According to the conventions, we have to return HTTP 204 No Content. return(NoContent()); } catch (QuestionFormDoesntExistsException) { // Handle error if the question form to update doesn't exists. return(BadRequest("No QuestionForm exists with the given ID!")); } }