public Guid Handle(SaveSurveyAnswerPageCommand command)
        {
            var surveyAnswer = dbContext.SurveyAnswers.Single(x => x.Id == command.Id);

            var questionAnswers = dbContext.QuestionAnswers
                                  .Include(a => a.QuestionPartAnswers)
                                  .Where(a => a.SurveyAnswer.Id == command.Id && a.Question.SurveyPage.PageNumber == command.PageNumber)
                                  .ToList();

            // first remove current answers for this page
            foreach (var answer in questionAnswers)
            {
                foreach (var answerPart in answer.QuestionPartAnswers)
                {
                    dbContext.QuestionPartAnswers.Remove(answerPart);
                }
                dbContext.QuestionAnswers.Remove(answer);
            }
            dbContext.SaveChanges();

            // then insert the new answers
            foreach (var questionWithAnswerDto in command.QuestionsWithAnswers)
            {
                var question       = dbContext.Questions.Single(x => x.Id == questionWithAnswerDto.QuestionId);
                var questionAnswer = new QuestionAnswer
                {
                    Id           = Guid.NewGuid(),
                    AnswerText   = questionWithAnswerDto.Answer.Text,
                    Question     = question,
                    SurveyAnswer = surveyAnswer
                };
                dbContext.QuestionAnswers.Add(questionAnswer);

                // insert the answer parts if any
                if (questionWithAnswerDto.Answer.Parts != null)
                {
                    foreach (var questionPartId in questionWithAnswerDto.Answer.Parts)
                    {
                        var questionPart = dbContext.QuestionParts.Single(x => x.Id == questionPartId);
                        var answerPart   = new QuestionPartAnswer
                        {
                            Id             = Guid.NewGuid(),
                            Text           = questionWithAnswerDto.Answer.Text, // obsolete ? text on questionAnswer maybe enough ?
                            QuestionAnswer = questionAnswer,
                            QuestionPart   = questionPart
                        };
                        dbContext.QuestionPartAnswers.Add(answerPart);
                    }
                }
            }
            dbContext.SaveChanges();

            return(command.Id);
        }
Ejemplo n.º 2
0
        public Guid Dispatch <TCommand>(TCommand command)
            where TCommand : ICommand
        {
            var handler = (IHandleCommand <TCommand>)serviceProvider.GetService(typeof(IHandleCommand <TCommand>));

            if (handler == null)
            {
                throw new HandlerNotFoundException(typeof(IHandleCommand <TCommand>));
            }

            // would be better to put transaction in interceptor
            // but as of now I don't know how to with the DI of Dotnet Core
            var myTransaction = false;

            try
            {
                if (dbContext.Database.CurrentTransaction == null)
                {
                    myTransaction = true;
                    dbContext.Database.BeginTransaction();
                }

                var result = handler.Handle(command);
                dbContext.SaveChanges();

                if (myTransaction)
                {
                    dbContext.Database.CommitTransaction();
                }

                return(result);
            }
            catch (Exception)
            {
                dbContext.Database.CurrentTransaction?.Rollback();
                throw;
            }
        }