public IHttpActionResult GetOnlineQuestion(int id) { OnlineQuestion onlineQuestion = db.OnlineQuestion.SingleOrDefault(x => x.id_question == id); if (onlineQuestion == null) { return(NotFound()); } var model = new QuestionDTO { description = onlineQuestion.question_desc, required = onlineQuestion.is_required, title = onlineQuestion.question_text, type = onlineQuestion.question_type, survey_id = onlineQuestion.id_online_survey, Id = onlineQuestion.id_question }; if (onlineQuestion.question_type != "text" && onlineQuestion.question_type != "textarea") { var listOfValues = db.OnlineValues.Where(x => x.question_id == onlineQuestion.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 DeleteOnlineQuestion(int id) { OnlineQuestion onlineQuestion = db.OnlineQuestion.Include(x => x.OnlineValues).Include(x => x.OnlineAnswer).Where(x => x.id_question == id).Single(); if (onlineQuestion == null) { return(NotFound()); } db.OnlineQuestion.Remove(onlineQuestion); db.SaveChanges(); return(Ok(onlineQuestion)); }
public IHttpActionResult PostOnlineQuestion(QuestionDTO question) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (question == null) { return(NotFound()); } if (question.Id != null) { var onQ = db.OnlineQuestion.Find(question.Id); if (question.type != "text" && question.type != "textarea") { onQ.is_required = question.required; onQ.question_desc = question.description; onQ.question_text = question.title; onQ.question_type = question.type; List <OnlineValues> listToDelete = db.OnlineValues.Where(x => x.question_id == question.Id).ToList(); foreach (var val in listToDelete) { db.OnlineValues.Remove(val); } db.SaveChanges(); foreach (var val in question.values) { var newVal = new OnlineValues { Value = val, OnlineQuestion = onQ }; db.OnlineValues.Add(newVal); } db.SaveChanges(); } else { if (question.values != null) { List <OnlineValues> listToDelete = db.OnlineValues.Where(x => x.question_id == question.Id).ToList(); foreach (var val in listToDelete) { db.OnlineValues.Remove(val); } db.SaveChanges(); } onQ.is_required = question.required; onQ.question_desc = question.description; onQ.question_text = question.title; onQ.question_type = question.type; } db.Entry(onQ).State = EntityState.Modified; db.SaveChanges(); return(Ok(onQ)); } else { if (question.type != "text" && question.type != "textarea") { var newQ = new OnlineQuestion { question_desc = question.description, question_text = question.title, question_type = question.type, is_required = question.required, id_online_survey = question.survey_id }; db.OnlineQuestion.Add(newQ); db.SaveChanges(); //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 OnlineValues { Value = val, OnlineQuestion = newQ }; db.OnlineValues.Add(newVal); } db.SaveChanges(); } else { var newQ = new OnlineQuestion { question_desc = question.description, question_text = question.title, question_type = question.type, is_required = question.required, id_online_survey = question.survey_id }; db.OnlineQuestion.Add(newQ); } db.SaveChanges(); return(Ok()); } }