public IHttpActionResult GetOfflineQuestion(int id) { OfflineQuestion offlineQuestion = db.OfflineQuestion.Include(x => x.OfflineValues).SingleOrDefault(x => x.id_question == id); if (offlineQuestion == null) { return(NotFound()); } var model = new QuestionDTO { description = offlineQuestion.question_desc, required = offlineQuestion.is_required, title = offlineQuestion.question_text, type = offlineQuestion.question_type, survey_id = offlineQuestion.id_offline_survey, Id = offlineQuestion.id_question }; if (offlineQuestion.question_type != "text" && offlineQuestion.question_type != "textarea") { var listOfValues = db.OfflineValues.Where(x => x.question_id == offlineQuestion.id_question).ToList(); List <string> values = new List <string>(); foreach (var val in listOfValues) { values.Add(val.Value); } model.values = values; } return(Ok(model)); }
public IHttpActionResult DeleteOfflineQuestion(int id) { OfflineQuestion offlineQuestion = db.OfflineQuestion.Include(x => x.OfflineValues).Include(x => x.OfflineAnswer).Where(x => x.id_question == id).Single(); if (offlineQuestion == null) { return(NotFound()); } db.OfflineQuestion.Remove(offlineQuestion); db.SaveChanges(); return(Ok(offlineQuestion)); }
public IHttpActionResult PostOfflineQuestion(QuestionDTO question) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (question == null) { return(NotFound()); } if (question.Id != null) { var offQ = db.OfflineQuestion.Find(question.Id); if (question.type != "text" && question.type != "textarea") { offQ.is_required = question.required; offQ.question_desc = question.description; offQ.question_text = question.title; offQ.question_type = question.type; List <OfflineValues> listToDelete = db.OfflineValues.Where(x => x.question_id == question.Id).ToList(); foreach (var val in listToDelete) { db.OfflineValues.Remove(val); } db.SaveChanges(); foreach (var val in question.values) { var newVal = new OfflineValues { Value = val, OfflineQuestion = offQ }; db.OfflineValues.Add(newVal); } db.SaveChanges(); } else { if (question.values != null) { List <OfflineValues> listToDelete = db.OfflineValues.Where(x => x.question_id == question.Id).ToList(); foreach (var val in listToDelete) { db.OfflineValues.Remove(val); } db.SaveChanges(); } offQ.is_required = question.required; offQ.question_desc = question.description; offQ.question_text = question.title; offQ.question_type = question.type; } db.Entry(offQ).State = EntityState.Modified; db.SaveChanges(); return(Ok(offQ)); } else { if (question.type != "text" && question.type != "textarea") { var newQ = new OfflineQuestion { question_desc = question.description, question_text = question.title, question_type = question.type, is_required = question.required, id_offline_survey = question.survey_id }; db.OfflineQuestion.Add(newQ); db.SaveChanges(); foreach (var val in question.values) { var newVal = new OfflineValues { Value = val, OfflineQuestion = newQ }; db.OfflineValues.Add(newVal); } db.SaveChanges(); } else { var newQ = new OfflineQuestion { question_desc = question.description, question_text = question.title, question_type = question.type, is_required = question.required, id_offline_survey = question.survey_id }; db.OfflineQuestion.Add(newQ); } db.SaveChanges(); return(Ok()); } }