public async Task UnsubscribeMailingEmails(MailingEmail mailingEmail, Guid userId) { var mailedGroupEmails = mailingEmail.MailingEmailGroups.Where(meg => meg.MailingGroup.UserId == userId).ToList(); foreach (var mailedGroupEmail in mailedGroupEmails) { await _mailingEmailGroupRepository.Remove(mailedGroupEmail); } }
public async Task DeleteMailingEmailRelatedToMailingGroup(Guid mailingGroupId) { var mailingGroupEmailsRelatedToMailingGroup = await _mailingEmailGroupRepository.GetAll() .Where(meg => meg.MailingGroupId == mailingGroupId) .ToListAsync(); foreach (var mailingGroupEmail in mailingGroupEmailsRelatedToMailingGroup) { await _mailingEmailGroupRepository.Remove(mailingGroupEmail); } }
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)); }