Beispiel #1
0
        public HttpResponseMessage PostAnswerQuestion([FromBody] AnswersQuestion answerquestion)
        {
            string jsonResponseText;

            try
            {
                if (ModelState.IsValid)
                {
                    var existingItem = _db.AnswersQuestions
                                       .FirstOrDefault(x => x.QuestionID == answerquestion.QuestionID &&
                                                       x.AnswerID == answerquestion.AnswerID);

                    if (existingItem != null)
                    {
                        //item already exists we will simply update it
                        existingItem.Value            = answerquestion.Value;
                        existingItem.Status           = answerquestion.Status;
                        _db.Entry(existingItem).State = EntityState.Modified;
                        _db.SaveChanges();
                        jsonResponseText = JsonConvert.SerializeObject(existingItem);
                    }
                    else
                    {
                        //new choice
                        _db.AnswersQuestions.Add(answerquestion);
                        _db.SaveChanges();
                        jsonResponseText = JsonConvert.SerializeObject(answerquestion);
                    }
                }
                else
                {
                    jsonResponseText =
                        "{\"status\":0,\"error\":\"Model is not valid\",\"message\":\"Model is not valid\"}";
                }
                var response = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StringContent(jsonResponseText, Encoding.UTF8, "application/json");
                return(response);
            }
            catch (Exception ex)
            {
                jsonResponseText =
                    "{\"status\":0,\"error\":\"Error trying to create the new answerQuestion\",\"message\":\"" +
                    ex.Message + "\"}";
                var response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.Content = new StringContent(jsonResponseText, Encoding.UTF8, "application/json");
                return(response);
            }
        }
Beispiel #2
0
        public HttpResponseMessage DeleteAnswerQuestion([FromBody] AnswersQuestion answerquestion)
        {
            var jsonResponseText = "";

            try
            {
                if (ModelState.IsValid)
                {
                    var existingItem = _db.AnswersQuestions
                                       .FirstOrDefault(x => x.QuestionID == answerquestion.QuestionID &&
                                                       x.AnswerID == answerquestion.AnswerID);

                    if (existingItem != null)
                    {
                        _db.AnswersQuestions.Remove(existingItem);
                        _db.SaveChanges();
                        jsonResponseText = "{\"status\":1,\"message\":\"Item deleted successfully\"}";
                    }
                }
                else
                {
                    jsonResponseText =
                        "{\"status\":0,\"error\":\"Model is not valid\",\"message\":\"Model is not valid\"}";
                }
                var response = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StringContent(jsonResponseText, Encoding.UTF8, "application/json");
                return(response);
            }
            catch (Exception ex)
            {
                jsonResponseText =
                    "{\"status\":0,\"error\":\"Error trying to create the new answerQuestion\",\"message\":\"" +
                    ex.Message + "\"}";
                var response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.Content = new StringContent(jsonResponseText, Encoding.UTF8, "application/json");
                return(response);
            }
        }