Exemple #1
0
        public async Task <ApiResult <MailingGroupDto> > Handle(Command request, CancellationToken cancellationToken)
        {
            IdentityUser user = await _context.Users.FirstOrDefaultAsync(x => x.Id == request.UserId, cancellationToken);

            if (user is null)
            {
                return(ApiResult <MailingGroupDto> .Failed("User not found", 404));
            }

            string newName = request.Name.Trim();

            if (string.IsNullOrEmpty(newName))
            {
                return(ApiResult <MailingGroupDto> .Failed("Name is required", 400));
            }

            var updatedGroup = await _context.MailingGroups
                               .Where(x => x.UserId == request.UserId)
                               .Where(x => x.Id == request.Id)
                               .FirstOrDefaultAsync(cancellationToken);

            if (updatedGroup is null)
            {
                return(ApiResult <MailingGroupDto> .Failed("This mailing group is not exist", 404));
            }

            bool nameIsTaken = await _context.MailingGroups
                               .Where(x => x.UserId == request.UserId)
                               .Where(x => x.Id != request.Id)
                               .Where(x => x.Name == newName).AnyAsync(cancellationToken);

            if (nameIsTaken)
            {
                return(ApiResult <MailingGroupDto> .Failed("This mailing group name is already exists", 401));
            }


            updatedGroup.Name = newName;

            _context.Update(updatedGroup);
            await _context.SaveChangesAsync(cancellationToken);

            return(ApiResult <MailingGroupDto> .Success(request));
        }
Exemple #2
0
        public async Task <ApiResult <EmailDto> > Handle(Command request, CancellationToken cancellationToken)
        {
            Model.Entity.MailingGroup mailingGroup = await _context.MailingGroups
                                                     .Include(x => x.Emails)
                                                     .Where(x => x.UserId == request.UserId)
                                                     .Where(x => x.Id == request.GroupId)
                                                     .FirstOrDefaultAsync(cancellationToken);

            if (mailingGroup is null)
            {
                return(ApiResult <EmailDto> .Failed("Mailing group is not exists", 404));
            }

            string newEmail     = request.Email.Trim();
            bool   isValidEmail = EmailValidator.ValidateEmail(newEmail);

            if (!isValidEmail)
            {
                return(ApiResult <EmailDto> .Failed("Email is wrong format", 400));
            }
            Email updatedEmail = mailingGroup.Emails.FirstOrDefault(x => x.Id == request.EmailId);

            if (updatedEmail is null)
            {
                return(ApiResult <EmailDto> .Failed("Email is not exists", 404));
            }

            if (mailingGroup.Emails.Where(x => x.Id != request.EmailId).Any(x => x.Name == request.Email))
            {
                return(ApiResult <EmailDto> .Failed("New email is already exists in this group", 409));
            }

            updatedEmail.Name = newEmail;
            _context.Update(updatedEmail);
            await _context.SaveChangesAsync(cancellationToken);

            return(ApiResult <EmailDto> .Success(new EmailDto(updatedEmail)));
        }