コード例 #1
0
        public static async Task <string> CreateNewGroupAsync(
            GroupUploadModel groupUploadModel,
            Guid userId,
            IRepositoryManager repositoryManager)
        {
            try
            {
                var newGroupIdentifier = await GenerateNewGroupIdentifierAsync(repositoryManager.GroupRepository).ConfigureAwait(false);

                var newGroup = new Domain.Entities.Group
                {
                    Identifier    = newGroupIdentifier,
                    Name          = groupUploadModel.Name,
                    IsPrivate     = groupUploadModel.IsPrivate,
                    CreatedOn     = DateTime.Now,
                    CreatedBy     = userId,
                    LastUpdatedOn = DateTime.Now,
                    LastUpatedBy  = userId
                };

                await repositoryManager.GroupRepository.AddAsync(newGroup);

                await CreateNewGroupMemberAsync(
                    newGroup.Id,
                    userId,
                    repositoryManager).ConfigureAwait(false);

                return(newGroupIdentifier);
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #2
0
        public async Task Validate_when_user_is_admin_of_group()
        {
            // Arrange
            var sut = new UpdateGroupValidator(_dbContext);

            string userSub = Guid.NewGuid().ToString();

            var admin = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = admin,
                Name  = "my group"
            };

            await _dbContext.Users.AddAsync(admin);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.UsersToGroups.AddAsync(new UserToGroup { Group = group, User = admin });

            await _dbContext.SaveChangesAsync();

            // Act
            bool isValid = await sut.IsValidAsync(new UpdateGroup(userSub, group.Id, new GroupRequest()));

            // Assert
            isValid.ShouldBeTrue();
        }
コード例 #3
0
        public async Task Validate_when_user_is_admin_of_group()
        {
            // Arrange
            string userSub = Guid.NewGuid().ToString();
            var    user    = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var group = new Domain.Entities.Group {
                Admin = user, Name = "A group"
            };

            var userToGroup = new UserToGroup {
                User = user, Group = group
            };

            await _dbContext.Users.AddAsync(user);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.UsersToGroups.AddAsync(userToGroup);

            await _dbContext.SaveChangesAsync();

            var sut = new GetGroupMembersValidator(_dbContext);

            // Act
            bool isValid = await sut.IsValidAsync(new GetGroupMembers(userSub, group.Id));

            // Assert
            isValid.ShouldBeTrue();
        }
コード例 #4
0
        public async Task Invalidate_when_user_not_part_of_group()
        {
            // Arrange
            var    sut     = new LeaveGroupValidator(_dbContext);
            string userSub = Guid.NewGuid().ToString();

            var user = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = user,
                Name  = "my group"
            };

            await _dbContext.Users.AddAsync(user);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.SaveChangesAsync();

            // Act
            bool isValid = await sut.IsValidAsync(new LeaveGroup(userSub, group.Id));

            // Assert
            isValid.ShouldBeFalse();
        }
コード例 #5
0
        public async Task Invalidate_when_target_user_does_not_exists()
        {
            // Arrange
            var sut = new RemoveFromGroupValidator(_dbContext);

            string userSub = Guid.NewGuid().ToString();

            var admin = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = admin,
                Name  = "my group"
            };

            await _dbContext.Users.AddAsync(admin);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.UsersToGroups.AddAsync(new UserToGroup { Group = group, User = admin });

            await _dbContext.SaveChangesAsync();

            // Act
            bool isValid = await sut.IsValidAsync(new RemoveFromGroup(userSub, group.Id, Guid.NewGuid()));

            // Assert
            isValid.ShouldBeFalse();
        }
コード例 #6
0
        public async Task Invalidate_when_user_is_already_invited()
        {
            // Arrange
            var sut = new InviteToGroupValidator(_dbContext);

            string userSub         = Guid.NewGuid().ToString();
            string nonGroupUserSub = Guid.NewGuid().ToString();

            var admin = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var invitedGroupUser = new User
            {
                FirstName = "Integration2",
                LastName  = "Test2",
                Sub       = nonGroupUserSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = admin,
                Name  = "my group"
            };

            var userToGroups = new UserToGroup {
                Group = group, User = admin
            };
            var groupInvite = new GroupInvite
            {
                Group = group,
                InvitationReceiver = invitedGroupUser,
                InvitationSender   = admin
            };

            await _dbContext.Users.AddAsync(admin);

            await _dbContext.Users.AddAsync(invitedGroupUser);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.UsersToGroups.AddAsync(userToGroups);

            await _dbContext.GroupInvites.AddAsync(groupInvite);

            await _dbContext.SaveChangesAsync();

            // Act
            bool isValid = await sut.IsValidAsync(new InviteToGroup(userSub, group.Id, invitedGroupUser.Id));

            // Assert
            isValid.ShouldBeFalse();
        }
コード例 #7
0
        public async Task Validate_request_when_is_part_of_group()
        {
            // Arrange
            string userSub  = Guid.NewGuid().ToString();
            string adminSub = Guid.NewGuid().ToString();

            var user = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var admin = new User
            {
                FirstName = "Admin",
                LastName  = "User",
                Sub       = adminSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = admin,
                Name  = "A group"
            };

            var userToGroup = new UserToGroup {
                Group = group, User = user
            };
            var adminToGroup = new UserToGroup {
                Group = group, User = admin
            };

            await _dbContext.Users.AddAsync(user);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.UsersToGroups.AddAsync(userToGroup);

            await _dbContext.UsersToGroups.AddAsync(adminToGroup);

            await _dbContext.SaveChangesAsync();

            var sut = new GetGroupSafeLocationsValidator(_dbContext);

            // Act
            bool isValid = await sut.IsValidAsync(new GetGroupSafeLocations(userSub, group.Id));

            // Assert
            isValid.ShouldBeTrue();
        }
コード例 #8
0
        public async Task Invalidate_request_when_user_is_not_admin_of_group()
        {
            // Arrange
            string userSub  = Guid.NewGuid().ToString();
            string adminSub = Guid.NewGuid().ToString();

            var user = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var adminUser = new User
            {
                FirstName = "Admin",
                LastName  = "User",
                Sub       = adminSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = adminUser,
                Name  = "a group"
            };

            var groupSafeLocation = new GroupSafeLocation
            {
                Group = group,
                Name  = "A safe location"
            };

            await _dbContext.Users.AddAsync(user);

            await _dbContext.Users.AddAsync(adminUser);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.GroupsSafeLocations.AddAsync(groupSafeLocation);

            await _dbContext.SaveChangesAsync();

            var sut = new UpdateSafeLocationValidator(_dbContext);

            // Act
            bool isValid = await sut.IsValidAsync(new UpdateSafeLocation(userSub, groupSafeLocation.Id, new SafeLocationRequest()));

            // Assert
            isValid.ShouldBeFalse();
        }
コード例 #9
0
        public async Task <Result <GroupModel> > Handle(AddGroup request, CancellationToken cancellationToken)
        {
            var isValid = await _validator.IsValidAsync(request);

            if (!isValid)
            {
                return(Result.Failure <GroupModel>("Validation failed"));
            }

            var user = await _context.Users.FirstAsync(u => u.Sub == request.UserSub, cancellationToken);

            var newBackpack = new Backpack
            {
                Name = $"Backpack for {request.Group.Name}"
            };

            var group = new Domain.Entities.Group
            {
                Admin    = user,
                Name     = request.Group.Name,
                Backpack = newBackpack
            };

            var newGroup = await _context.Groups.AddAsync(group, cancellationToken);

            await _context.UsersToGroups.AddAsync(
                new UserToGroup
            {
                User  = user,
                Group = group
            }, cancellationToken);

            await _context.BackpacksToUsers.AddAsync(new BackpackToUser
            {
                Backpack = newBackpack,
                User     = user
            }, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(new GroupModel
            {
                Id = newGroup.Entity.Id,
                Name = newGroup.Entity.Name,
                NumberOfMembers = newGroup.Entity.GroupMembers.Count,
                AdminId = newGroup.Entity.AdminId,
                AdminFirstName = newGroup.Entity.Admin.FirstName,
                AdminLastName = newGroup.Entity.Admin.LastName
            });
        }
コード例 #10
0
            public async Task <int> Handle(CreateGroupCommand request, CancellationToken cancellationToken)
            {
                Domain.Entities.Group group;

                group = new Domain.Entities.Group
                {
                    Name = request.Name
                };

                _context.Group.Add(group);

                await _context.SaveChangesAsync(cancellationToken);

                return(group.Id);
            }
コード例 #11
0
        public async Task Validate_when_user_is_part_of_group_and_invited_existing_user()
        {
            // Arrange
            var    sut             = new InviteToGroupValidator(_dbContext);
            string userSub         = Guid.NewGuid().ToString();
            string nonGroupUserSub = Guid.NewGuid().ToString();

            var user = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var nonGroupUser = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = nonGroupUserSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = user,
                Name  = "my group"
            };

            var userToGroups = new UserToGroup {
                Group = group, User = user
            };

            await _dbContext.Users.AddAsync(user);

            await _dbContext.Users.AddAsync(nonGroupUser);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.UsersToGroups.AddAsync(userToGroups);

            await _dbContext.SaveChangesAsync();

            // Act
            bool isValid = await sut.IsValidAsync(new InviteToGroup(userSub, group.Id, nonGroupUser.Id));

            // Assert
            isValid.ShouldBeTrue();
        }
コード例 #12
0
        public async Task Validate_when_user_is_admin_of_requested_group()
        {
            // Arrange
            var sut = new DeleteSafeLocationValidator(_dbContext);

            string userSub = Guid.NewGuid().ToString();
            var    user    = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = user,
                Name  = "my group"
            };

            var groupSafeLocation = new GroupSafeLocation
            {
                Group = group,
                Name  = "A safe location"
            };

            await _dbContext.Users.AddAsync(user);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.GroupsSafeLocations.AddAsync(groupSafeLocation);

            await _dbContext.SaveChangesAsync();

            // Act
            bool isValid = await sut.IsValidAsync(new DeleteSafeLocation(userSub, groupSafeLocation.Id));

            // Assert
            isValid.ShouldBeTrue();
        }
コード例 #13
0
        public async Task Invalidate_request_when_user_adds_himself_to_group()
        {
            // Arrange
            var    sut     = new InviteToGroupValidator(_dbContext);
            string userSub = Guid.NewGuid().ToString();

            var user = new User
            {
                FirstName = "Integration",
                LastName  = "Test",
                Sub       = userSub
            };

            var group = new Domain.Entities.Group
            {
                Admin = user,
                Name  = "my group"
            };

            var userToGroups = new UserToGroup {
                Group = group, User = user
            };

            await _dbContext.Users.AddAsync(user);

            await _dbContext.Groups.AddAsync(group);

            await _dbContext.UsersToGroups.AddAsync(userToGroups);

            await _dbContext.SaveChangesAsync();

            // Act
            bool isValid = await sut.IsValidAsync(new InviteToGroup(userSub, group.Id, user.Id));

            // Assert
            isValid.ShouldBeFalse();
        }
コード例 #14
0
ファイル: GroupDto.cs プロジェクト: matthofmarch/MyRecipe
 public GroupDto(Domain.Entities.Group group)
 {
     Name    = group.Name;
     Members = group.Members.Select(m => new MemberDto(m)).ToList();
 }