Esempio n. 1
0
        public async Task CreateQuestion(QuestionDTO questionDTO, List <string> tagNames)
        {
            var question = _mapper.Map <Question>(questionDTO);

            question.QuestionsTags = new List <QuestionTag>();
            question.Author        = await _userRepository.GetByIdAsync(question.Author.Id);

            await _questionRepository.CreateAsync(question);

            var questionTags = new List <QuestionTag>();

            tagNames.ForEach(x => questionTags
                             .Add(new QuestionTag()
            {
                TagId    = _tagRepository.GetByName(x).Id,
                Question = question
            }));

            questionTags.ForEach(x => _questionTagRepository.Create(x));

            await _unitOfWork.Commit();
        }
        public async Task <CreateQuestionResult> CreateQuestionAsync(CreateQuestionCommand command)
        {
            ThrowIfDisposed();

            string         questionToken  = GenerateToken();
            QuestionEntity questionEntity = new QuestionEntity()
            {
                Description       = command.Description,
                Name              = command.Name,
                EmailAddress      = command.Email,
                IsClosed          = false,
                IsVerified        = false,
                CreateDateTimeUTC = DateTime.UtcNow,
                Token             = questionToken
            };

            using (var transaction = QuestionRepository.Context.Database.BeginTransaction())
            {
                try
                {
                    await QuestionRepository.CreateAsync(questionEntity, CancellationToken);

                    var emailEntity = EmailGeneratorService.GenerateVerifyQuestionEmail(questionEntity);
                    questionEntity.Email = emailEntity;
                    await QuestionRepository.UpdateAsync(questionEntity, CancellationToken);

                    transaction.Commit();
                }
                catch (Exception exception)
                {
                    string errorMesssage = "Error while saving question";
                    Logger.LogError(exception, errorMesssage);
                    return(new CreateQuestionResult(new Error(errorMesssage, errorMesssage)));
                }
            }
            return(new CreateQuestionResult(questionEntity.QuestionId));
        }