Beispiel #1
0
        public ActionResult DeleteConfirmed(int id)
        {
            tbl_question tbl_question = db.tbl_question.Find(id);

            db.tbl_question.Remove(tbl_question);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Beispiel #2
0
 public ActionResult Edit([Bind(Include = "id,question,maxRate,personTypeId,active")] tbl_question tbl_question)
 {
     if (ModelState.IsValid)
     {
         db.Entry(tbl_question).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(tbl_question));
 }
Beispiel #3
0
        public ActionResult Create([Bind(Include = "id,question,maxRate,personTypeId,active")] tbl_question tbl_question)
        {
            if (ModelState.IsValid)
            {
                db.tbl_question.Add(tbl_question);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(tbl_question));
        }
Beispiel #4
0
        // GET: question/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tbl_question tbl_question = db.tbl_question.Find(id);

            if (tbl_question == null)
            {
                return(HttpNotFound());
            }
            return(View(tbl_question));
        }
Beispiel #5
0
        public IHttpActionResult Put(questionModel questionParameter)
        {
            try
            {
                int          ChoiceID;
                tbl_question DBquestion = db.tbl_question.Find(questionParameter.ID);
                DBquestion.question  = questionParameter.question;
                DBquestion.question  = questionParameter.question;
                DBquestion.image_url = questionParameter.image_url;
                DBquestion.thumb_url = questionParameter.thumb_url;
                //Assuming field published_at is updated when the record is edited
                DBquestion.published_at = DateTime.Now;
                db.SaveChanges();

                //Since this approach allows to add N answers to the question it is preferable to delete the old ones and insert the new ones.
                DBquestion.rel_question_choices.Clear();
                //for all the answers/choices...
                foreach (choices choices in questionParameter.Choices)
                {
                    //if the choice doesn't exist on the answers table, it is added
                    if (!db.tbl_choices.Any(m => m.choice == choices.choice))
                    {
                        tbl_choices DBchoices = new tbl_choices();
                        DBchoices.choice = choices.choice;
                        db.tbl_choices.Add(DBchoices);
                        db.SaveChanges();
                        ChoiceID = DBchoices.ID;
                    }
                    //otherwise get the id
                    else
                    {
                        ChoiceID = db.tbl_choices.Where(m => m.choice == choices.choice).Select(m => m.ID).FirstOrDefault();
                    }

                    //and the answer and its votation is added in the relational table related to the question
                    db.rel_question_choices.Add(new rel_question_choices {
                        questionID = DBquestion.ID, choiceID = ChoiceID, votes = choices.votes
                    });
                    db.SaveChanges();
                }
                return(Ok());
            }
            catch (Exception e)
            {
                return(InternalServerError(new Exception(e.Message)));
            }
        }
Beispiel #6
0
        public IHttpActionResult questions(questionModel questionParameter)
        {
            try
            {
                int          ChoiceID;
                tbl_question DBquestion = new tbl_question();

                DBquestion.question     = questionParameter.question;
                DBquestion.image_url    = questionParameter.image_url;
                DBquestion.thumb_url    = questionParameter.thumb_url;
                DBquestion.published_at = DateTime.Now;

                //The question is added to the DB
                db.tbl_question.Add(DBquestion);
                db.SaveChanges();

                //for all the answers/choices...
                foreach (choices choices in questionParameter.Choices)
                {
                    //if the choice doesn't exist on the answers table, it is added
                    if (!db.tbl_choices.Any(m => m.choice == choices.choice))
                    {
                        tbl_choices DBchoices = new tbl_choices();
                        DBchoices.choice = choices.choice;
                        db.tbl_choices.Add(DBchoices);
                        db.SaveChanges();
                        ChoiceID = DBchoices.ID;
                    }
                    //otherwise get the id
                    else
                    {
                        ChoiceID = db.tbl_choices.Where(m => m.choice == choices.choice).Select(m => m.ID).FirstOrDefault();
                    }

                    //and the answer and its votation is added in the relational table related to the question
                    db.rel_question_choices.Add(new rel_question_choices {
                        questionID = DBquestion.ID, choiceID = ChoiceID, votes = choices.votes
                    });
                    db.SaveChanges();
                }
                return(Ok());
            }
            catch (Exception e)
            {
                return(InternalServerError(new Exception(e.Message)));
            }
        }