Beispiel #1
0
        public void EditQuestion(TQuestion model)
        {
            try
            {
                using (var dbContext = new QuestionsDBEntities())
                {
                    var modelToUpdate = dbContext.TQuestions.Include(x => x.TChoices).SingleOrDefault(x => x.ID == model.ID);

                    modelToUpdate.Question     = model.Question;
                    modelToUpdate.Image_url    = model.Image_url;
                    modelToUpdate.Thumb_url    = model.Thumb_url;
                    modelToUpdate.Published_at = model.Published_at;

                    var index = Math.Min(model.TChoices.Count(), modelToUpdate.TChoices.Count());
                    for (int i = 0; i < index; i++)
                    {
                        modelToUpdate.TChoices.ElementAt(i).Choice = model.TChoices.ElementAt(i).Choice;
                        modelToUpdate.TChoices.ElementAt(i).Votes  = model.TChoices.ElementAt(i).Votes;
                    }

                    dbContext.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                log.Error(exception);
            }
        }
Beispiel #2
0
        public int CreateQuestion(TQuestion model)
        {
            try
            {
                using (var dbContext = new QuestionsDBEntities())
                {
                    dbContext.TQuestions.Add(model);
                    dbContext.SaveChanges();

                    return(model.ID);
                }
            }
            catch (Exception exception)
            {
                log.Error(exception);
                return(-1);
            }
        }