예제 #1
0
        public HttpResponseMessage Add(Question questionToAdd)
        {
            try
            {
                if (questionToAdd != null && ModelState.IsValid)
                {
                    QuestionDTO questionDTO = QuestionMapper.ToDTO(questionToAdd);
                    questionDTO.Tags = SetTags(questionToAdd.Tags);
                    var     email  = CurrentEmail();
                    UserDTO author = userLogic.Find(email);
                    questionDTO.Author = author;
                    var question = questionLogic.Add(questionDTO);
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, question);
                    return(response);
                }
                else
                {
                    var validationErrors = new List <string>();
                    foreach (var state in ModelState)
                    {
                        foreach (var error in state.Value.Errors)
                        {
                            validationErrors.Add((error.ErrorMessage));
                        }
                    }

                    var jsonerrors = JsonConvert.SerializeObject(new
                    {
                        errors = validationErrors
                    });
                    return(Request.CreateResponse(HttpStatusCode.Forbidden, JsonConvert.DeserializeObject(jsonerrors)));
                }
            }
            catch (Exception e)
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, e.Message);
                return(response);
            }
        }