Ejemplo n.º 1
0
        public async Task ExecuteShouldReturnSuccess()
        {
            //Arrange
            MailingGroupContext mailingGroupContext = ContextBuilder.BuildClean();
            AppUser             user = new AppUserBuilder(mailingGroupContext).WithName("test").BuildAndSave();

            Command cmd = new Command
            {
                UserId = user.Id,
                Name   = "New group"
            };
            //Act
            ApiResult <MailingGroupItemListDto> result = await new Handler(mailingGroupContext).Handle(cmd, CancellationToken.None);

            //Assert
            result.IsSuccess.Should().BeTrue();
            result.Message.Should().BeNullOrEmpty();

            mailingGroupContext.MailingGroups.Should().HaveCount(1);
            Model.Entity.MailingGroup mailingGroup = mailingGroupContext.MailingGroups.First();
            mailingGroup.Name.Should().Be(cmd.Name);
            mailingGroup.UserId.Should().Be(cmd.UserId);
            mailingGroup.Id.Should().Be(1);

            result.Data.Should().NotBeNull();
            result.Data.Id.Should().Be(mailingGroup.Id);
        }
Ejemplo n.º 2
0
        public async Task ExecuteShouldReturnSuccess()
        {
            //Arrange
            MailingGroupContext mailingGroupContext = ContextBuilder.BuildClean();
            AppUser             user = new AppUser
            {
                UserName = "******",
                Email    = "*****@*****.**"
            };

            AppUser otherUser = new AppUser
            {
                UserName = "******",
                Email    = "*****@*****.**"
            };

            mailingGroupContext.Add(user);
            mailingGroupContext.Add(otherUser);
            Model.Entity.MailingGroup oldGroup = new Model.Entity.MailingGroup
            {
                Name = "Old group",
                User = user
            };
            Model.Entity.MailingGroup groupOtherUser = new Model.Entity.MailingGroup
            {
                Name = "New group",
                User = otherUser
            };
            mailingGroupContext.Add(oldGroup);
            mailingGroupContext.Add(groupOtherUser);
            mailingGroupContext.SaveChanges();

            Command cmd = new Command
            {
                UserId = user.Id,
                Id     = oldGroup.Id,
                Name   = "New group"
            };
            //Act
            ApiResult <MailingGroupDto> result = await new Handler(mailingGroupContext).Handle(cmd, CancellationToken.None);

            //Assert
            result.IsSuccess.Should().BeTrue();
            result.Message.Should().BeNullOrEmpty();

            mailingGroupContext.MailingGroups.Should().HaveCount(2);
            Model.Entity.MailingGroup mailingGroup = mailingGroupContext.MailingGroups.First();
            mailingGroup.Name.Should().Be(cmd.Name);
            mailingGroup.UserId.Should().Be(cmd.UserId);
            mailingGroup.Id.Should().Be(1);

            result.Data.Should().NotBeNull();
            result.Data.Id.Should().Be(mailingGroup.Id);
        }
Ejemplo n.º 3
0
 private IEnumerable <Model.Entity.MailingGroup> GenerateGroups(int numberOfGroup, AppUser user)
 {
     for (int i = 0; i < numberOfGroup; i++)
     {
         MailingGroup mailingGroup = new Model.Entity.MailingGroup()
         {
             Name = $"Lista {i}",
             User = user
         };
         mailingGroup.Emails = GenerateEmails(i, mailingGroup).ToList();
         yield return(mailingGroup);
     }
 }
Ejemplo n.º 4
0
        public async Task ExecuteShouldReturnNameIsTaken()
        {
            //Arrange
            MailingGroupContext mailingGroupContext = ContextBuilder.BuildClean();
            AppUser             user = new AppUser
            {
                UserName = "******",
                Email    = "*****@*****.**"
            };

            mailingGroupContext.Add(user);
            Model.Entity.MailingGroup oldGroup = new Model.Entity.MailingGroup
            {
                Name = "Old group",
                User = user
            };
            Model.Entity.MailingGroup otherGroup = new Model.Entity.MailingGroup
            {
                Name = "New group",
                User = user
            };
            mailingGroupContext.Add(oldGroup);
            mailingGroupContext.SaveChanges();
            mailingGroupContext.Add(otherGroup);
            mailingGroupContext.SaveChanges();

            Command cmd = new Command
            {
                UserId = user.Id,
                Id     = oldGroup.Id,
                Name   = "New group"
            };
            //Act
            ApiResult <MailingGroupDto> result = await new Handler(mailingGroupContext).Handle(cmd, CancellationToken.None);

            //Assert
            result.IsSuccess.Should().BeFalse();
            result.Message.Should().Be("This mailing group name is already exists");
            result.StatusCode.Should().Be(401);
            result.Data.Should().BeNull();
        }
Ejemplo n.º 5
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));
            }

            if (mailingGroup.Emails.Any(x => x.Name == newEmail))
            {
                return(ApiResult <EmailDto> .Failed("Email is already exists", 409));
            }

            Email email = new Email
            {
                Name           = newEmail,
                MailingGroup   = mailingGroup,
                MailingGroupId = mailingGroup.Id
            };

            await _context.AddAsync(email, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(ApiResult <EmailDto> .Success(new EmailDto(email)));
        }
Ejemplo n.º 6
0
        public async Task <ApiResult <MailingGroupItemListDto> > Handle(Command request, CancellationToken cancellationToken)
        {
            AppUser user = await _context.Users.FirstOrDefaultAsync(x => x.Id == request.UserId, cancellationToken);

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

            string newName = request.Name.Trim();

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

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

            if (isExist)
            {
                return(ApiResult <MailingGroupItemListDto> .Failed("This mailing group is already exists", 409));
            }

            Model.Entity.MailingGroup newEntity = new Model.Entity.MailingGroup
            {
                Name   = newName,
                UserId = request.UserId,
                User   = user
            };

            await _context.AddAsync(newEntity, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(ApiResult <MailingGroupItemListDto> .Success(new MailingGroupItemListDto(newEntity)));
        }