Ejemplo n.º 1
0
        public Question EditItem(Question item, List <Answer> answers)
        {
            using (var context = new Context())
            {
                var questionToEdit = context.QuestionsDB.Include("AnswersDB").FirstOrDefault(q => q.Id == item.Id);
                questionToEdit.IsOK = cast.BoolToInt(IsOK(item, answers));
                List <AnswerDB> toDelete = new List <AnswerDB>();
                foreach (AnswerDB answer in questionToEdit.AnswersDB)
                {
                    if (answers.FirstOrDefault(a => a.Id == answer.Id) == null)
                    {
                        toDelete.Add(answer);
                    }
                }
                questionToEdit.AnswersDB.RemoveAll(a => toDelete.Contains(a));
                questionToEdit.ReplyTime = DateTime.Now.ToString();
                questionToEdit.State     = (int)State.Answered;
                context.SaveChanges();

                foreach (var ans in toDelete)
                {
                    context.AnswersDB.Remove(context.AnswersDB.FirstOrDefault(a => a.Id == ans.Id));
                }
                context.SaveChanges();

                _items.Remove(item);
                item = cast.DBToQuestion(questionToEdit);
                _items.Add(item);

                return(item);
            }
        }