private void UpdateSurveyMessageDetails(NotificationMessage message) { if (message.SurveyMessage == null) { return; } SurveyMessage survey = null; if (message.SurveyId.GetValueOrDefault(0) == 0) // create NEW { survey = new SurveyMessage(); _notificationMessageService.InsertSurveyMessage(survey); _unitOfWorkAsync.SaveChanges(); message.SurveyId = survey.SurveyId; message.SurveyMessage.SurveyId = survey.SurveyId; message.SurveyMessage.Questions.ToList().ForEach(q => { q.SurveyId = survey.SurveyId; var answers = new List <SurveyQuestionAnswer>(); if (q.Answers != null) { answers = new List <SurveyQuestionAnswer>(q.Answers); q.Answers = null; } _notificationMessageService.InsertSurveyQuestion(q); answers.ForEach(a => a.QuestionId = q.QuestionId); _notificationMessageService.InsertSurveyAnswers(answers.Where(a => a.Answer != null)); q.Answers = answers; survey.Questions.Add(q); }); survey.MessageHeader = message.SurveyMessage.MessageHeader; survey.MessageShortDescription = message.SurveyMessage.MessageShortDescription; survey.MessageDescription = message.SurveyMessage.MessageDescription; message.SurveyMessage = survey; } else // edit existing { survey = _notificationMessageService.FindSurveyMessage(message.SurveyId.GetValueOrDefault(0)); survey.MessageHeader = message.SurveyMessage.MessageHeader; survey.MessageShortDescription = message.SurveyMessage.MessageShortDescription; survey.MessageDescription = message.SurveyMessage.MessageDescription; foreach (var question in message.SurveyMessage.Questions) { var originalQuestion = survey.Questions .SingleOrDefault(c => c.QuestionId == question.QuestionId && c.QuestionId != 0); // Is original child item with same ID in DB? if (originalQuestion != null) { // Yes -> Update scalar properties of child item // lets first update answers if (question.Answers != null) { foreach (var answer in question.Answers) { var originalAnswer = originalQuestion.Answers .SingleOrDefault(c => c.AnswerId == answer.AnswerId && c.AnswerId != 0); // Is original child item with same ID in DB? if (originalAnswer != null) { // Yes -> Update scalar properties of child item originalAnswer.ModifiedDate = DateTime.UtcNow; originalAnswer.Answer = answer.Answer; originalAnswer.ObjectState = ObjectState.Modified; _notificationMessageService.UpdateSurveyAnswer(originalAnswer); } else { // No -> It's a new child item -> Insert answer.QuestionId = question.QuestionId; answer.CreatedDate = DateTime.UtcNow; answer.ObjectState = ObjectState.Added; _notificationMessageService.InsertSurveyAnswer(answer); } } //removing answers wich is not present in request (was deleter by user) var originalAnswersIds = originalQuestion.Answers.Select(a => a.AnswerId).ToList(); foreach (var originalAnswerId in originalAnswersIds) { if (!question.Answers.Select(a => a.AnswerId).Contains(originalAnswerId)) { var answer = originalQuestion.Answers.First(a => a.AnswerId == originalAnswerId); answer.ObjectState = ObjectState.Deleted; _notificationMessageService.DeleteSurveyAnswer(answer); } } } originalQuestion.ModifiedDate = DateTime.UtcNow; originalQuestion.ObjectState = ObjectState.Modified; originalQuestion.Question = question.Question; originalQuestion.QuestionType = question.QuestionType; //TODO: think about it _notificationMessageService.UpdateSurveyQuestion(originalQuestion); } else { // No -> It's a new child item -> Insert question.ObjectState = ObjectState.Added; question.SurveyId = survey.SurveyId; var answers = new List <SurveyQuestionAnswer>(); if (question.Answers != null) { answers = new List <SurveyQuestionAnswer>(question.Answers); question.Answers = null; } _notificationMessageService.InsertSurveyQuestion(question); answers.ForEach(a => a.QuestionId = question.QuestionId); _notificationMessageService.InsertSurveyAnswers(answers.Where(a => a.Answer != null)); question.Answers = answers; } } //removing questions with is not present in request var questionList = new List <SurveyQuestion>(survey.Questions); foreach (var question in questionList) { if (!message.SurveyMessage.Questions.Select(q => q.QuestionId).Contains(question.QuestionId)) { var answersList = new List <SurveyQuestionAnswer>(question.Answers); answersList.ForEach(a => { a.ObjectState = ObjectState.Deleted; _notificationMessageService.DeleteSurveyAnswer(a); }); var userAnswersList = new List <SurveyUserAnswer>(question.UserAnswers); userAnswersList.ForEach(ua => { ua.ObjectState = ObjectState.Deleted; _notificationMessageService.DeleteSurveyUserAnswer(ua); }); question.ObjectState = ObjectState.Deleted; _notificationMessageService.DeleteSurveyQuestion(question); } } message.SurveyMessage = survey; } _notificationMessageService.UpdateSurveyMessage(survey); _unitOfWorkAsync.SaveChanges(); }