示例#1
0
        public async Task <Guid> Handle(CreateMailingEmailCommand request, CancellationToken cancellationToken)
        {
            var mailingEmailId = await _mailingEmailService.GetOrCreateMailingEmail(request.Email);

            var mailingGroup = await _mailingGroupRepository.GetById(request.MailingGroupId);

            if (mailingGroup == null)
            {
                throw new LogicException(LogicErrorCode.CouldNotFindMailingGroup, $"Could not found mailing group with id '{request.MailingGroupId}'");
            }

            if (mailingGroup.UserId != request.UserId)
            {
                throw new LogicException(LogicErrorCode.DisallowToMakeChangesInOtherUserMailingGroup, "Could not update mailing group which is not belong to user");
            }

            var mailingEmailGroup = _mailingEmailGroupRepository.GetAll()
                                    .FirstOrDefault(meg => meg.MailingGroupId == request.MailingGroupId && meg.MailingEmailId == mailingEmailId);

            if (mailingEmailGroup != null)
            {
                throw new LogicException(LogicErrorCode.MailingEmailGrourExist, "Actually there is exist mailing email which is subscribed to mailing group");
            }

            var newMailingGroup = new MailingEmailGroup()
            {
                MailingEmailId = mailingEmailId,
                MailingGroupId = mailingGroup.Id
            };

            await _mailingEmailGroupRepository.Add(newMailingGroup);

            return(newMailingGroup.Id);
        }
示例#2
0
        public async Task <Unit> Handle(UpdateMailingEmailCommand request, CancellationToken cancellationToken)
        {
            var mailingEmail = await _mailingEmailRepository.GetAll()
                               .Include(me => me.MailingEmailGroups)
                               .FirstOrDefaultAsync(me => me.Id == request.MailingEmailId);

            if (mailingEmail == null)
            {
                throw new LogicException(LogicErrorCode.CannotFindMailingEmail, $"Could not find mailing email with id {request.MailingEmailId}");
            }

            if (mailingEmail.Email == request.NewEmailName)
            {
                throw new LogicException(LogicErrorCode.NewNameAndOldNameShouldBeDifferent, "Could not update mailing email to exactly same mailing email");
            }

            var mailingGroup = await _mailingGroupRepository.GetAll().FirstOrDefaultAsync(mg => mg.Id == request.MailingGroupId);

            if (mailingGroup == null)
            {
                throw new LogicException(LogicErrorCode.CannotFindMailingGroup, $"Could not find mailing email group with id {request.MailingGroupId}");
            }

            var newMailingEmailId = await _mailingEmailService.GetOrCreateMailingEmail(request.NewEmailName);

            var mailingGroupEmail = mailingEmail.MailingEmailGroups
                                    .FirstOrDefault(meg => meg.MailingGroup.UserId == request.UserId && meg.MailingGroupId == request.MailingGroupId);

            if (mailingGroupEmail == null)
            {
                throw new LogicException(LogicErrorCode.CannotFindMailingEmail,
                                         $"Could not find mailing email group for user with id {request.UserId}, mailing group with id {request.MailingGroupId} and mailing email with id {mailingEmail.Id}");
            }

            await _mailingEmailGroupRepository.Remove(mailingGroupEmail);

            var newMailingGroup = new MailingEmailGroup()
            {
                MailingEmailId = newMailingEmailId,
                MailingGroupId = mailingGroup.Id
            };

            await _mailingEmailGroupRepository.Add(newMailingGroup);

            return(await Task.FromResult(Unit.Value));
        }