private static async Task TestUserProjectRepositoryManyToMany()
        {
            IUserRepository userRepo = new UserRepository(_tableName);

            var u1 = new User {
                Id = "U1", Name = "User 1", FirstName = "User", LastName = "A", Email = "*****@*****.**"
            };
            await userRepo.AddUser(u1);


            IUserProjectRepository repo = new UserProjectRepository(_tableName);

            var u1p1 = new UserProject {
                UserId = u1.Id, ProjectId = "P1", Role = "owner"
            };
            await repo.AddProjectToUser(u1p1);

            var u1p2 = new UserProject {
                UserId = u1.Id, ProjectId = "P2", Role = "member"
            };
            await repo.AddProjectToUser(u1p2);

            Console.WriteLine("Getting projects by user U1");
            var allUPU1 = await repo.GetProjectsByUserAsync(u1.Id);

            foreach (var item in allUPU1)
            {
                Console.WriteLine(JsonSerializer.Serialize(item));
            }

            Console.WriteLine("Getting users by project P1");
            var usersP1 = await repo.GetUsersByProjectAsync("P1");

            foreach (var item in usersP1)
            {
                Console.WriteLine(JsonSerializer.Serialize(item));
            }
            Console.WriteLine("Getting users by project P2");
            var usersP2 = await repo.GetUsersByProjectAsync("P2");

            foreach (var item in usersP2)
            {
                Console.WriteLine(JsonSerializer.Serialize(item));
            }

            Console.WriteLine("Deleting projects P1 and P2 for user U1");
            await repo.RemoveProjectFromUser(u1p1.UserId, u1p1.ProjectId);

            await repo.RemoveProjectFromUser(u1p2.UserId, u1p2.ProjectId);

            Console.WriteLine("Getting projects by user U1 - should be empty");
            var deletedUPU1 = await repo.GetProjectsByUserAsync(u1.Id);

            foreach (var item in deletedUPU1)
            {
                Console.WriteLine(JsonSerializer.Serialize(item));
            }
        }
Exemple #2
0
 public UnitOfWork(ApplicationDbContext context, UserManager <ApplicationUser> userManager)
 {
     _context          = context;
     Projects          = new ProjectRepository(_context);
     Users             = new UserRepository(_context, userManager);
     Categories        = new CategoryRepository(_context);
     Materials         = new MaterialRepository(_context);
     MaterialTypes     = new MaterialTypeRepository(_context);
     Roles             = new RoleRepository(_context);
     UserProjects      = new UserProjectRepository(_context);
     ProjectCategories = new ProjectCategoryRepository(_context);
 }
        public async void TestRepo_Batch_UserProjectRepository()
        {
            var u1 = new User {
                Id = "U1", Name = "User 1", FirstName = "User", LastName = "A", Email = "*****@*****.**"
            };

            IUserProjectRepository repo = new UserProjectRepository(_tableName, _serviceUrl);

            var itemsToCreate = new List <UserProject>();

            for (int i = 50; i < 60; i++)
            {
                var up = new UserProject {
                    UserId = u1.Id, ProjectId = "P" + i, Role = "member"
                };
                itemsToCreate.Add(up);
            }
            await repo.BatchAddProjectsToUser(u1.Id, itemsToCreate);

            var list = await repo.GetProjectsByUserAsync(u1.Id);

            Assert.Equal(10, list.Count);

            for (int i = 0; i < 10; i++)
            {
                var item = list[i];
                var id   = i + 50;
                Assert.NotNull(item);
                Assert.Equal(u1.Id, item.UserId);
                Assert.Equal("P" + id, item.ProjectId);
                Assert.Equal("member", item.Role);
            }

            var itemsToDelete = new List <UserProject>();

            for (int i = 50; i < 60; i++)
            {
                var up = new UserProject {
                    UserId = u1.Id, ProjectId = "P" + i
                };
                itemsToDelete.Add(up);
            }
            await repo.BatchRemoveProjectsFromUser(u1.Id, itemsToDelete);

            var emptyList = await repo.GetProjectsByUserAsync(u1.Id);

            Assert.Empty(emptyList);
        }
Exemple #4
0
 public UserProjectService()
 {
     _userProjectRepository = new UserProjectRepository(new DatabaseContext());
 }
        public async void TestRepo_UserProjectRepository()
        {
            var u1 = new User {
                Id = "U1", Name = "User 1", FirstName = "User", LastName = "A", Email = "*****@*****.**"
            };

            IUserProjectRepository repo = new UserProjectRepository(_tableName, _serviceUrl);

            var allUPU1 = await repo.GetProjectsByUserAsync(u1.Id);

            Assert.Equal(0, allUPU1.Count);

            var u1p1 = new UserProject {
                UserId = u1.Id, ProjectId = "P1", Role = "owner"
            };
            await repo.AddProjectToUser(u1p1);

            allUPU1 = await repo.GetProjectsByUserAsync(u1.Id);

            Assert.Equal(1, allUPU1.Count);

            var u1p2 = new UserProject {
                UserId = u1.Id, ProjectId = "P2", Role = "member"
            };
            await repo.AddProjectToUser(u1p2);

            allUPU1 = await repo.GetProjectsByUserAsync(u1.Id);

            Assert.Equal(2, allUPU1.Count);
            Assert.Equal(u1.Id, allUPU1[0].UserId);
            Assert.Equal("P1", allUPU1[0].ProjectId);
            Assert.Equal("owner", allUPU1[0].Role);
            Assert.Equal(u1.Id, allUPU1[1].UserId);
            Assert.Equal("P2", allUPU1[1].ProjectId);
            Assert.Equal("member", allUPU1[1].Role);

            var usersP1 = await repo.GetUsersByProjectAsync("P1");

            Assert.Equal(1, usersP1.Count);
            Assert.Equal(u1.Id, usersP1[0].UserId);
            Assert.Equal("P1", usersP1[0].ProjectId);
            Assert.Equal("owner", usersP1[0].Role);

            var usersP2 = await repo.GetUsersByProjectAsync("P2");

            Assert.Equal(1, usersP2.Count);
            Assert.Equal(u1.Id, usersP2[0].UserId);
            Assert.Equal("P2", usersP2[0].ProjectId);
            Assert.Equal("member", usersP2[0].Role);

            await repo.RemoveProjectFromUser(u1p1.UserId, u1p1.ProjectId);

            allUPU1 = await repo.GetProjectsByUserAsync(u1.Id);

            Assert.Equal(1, allUPU1.Count);

            await repo.RemoveProjectFromUser(u1p2.UserId, u1p2.ProjectId);

            allUPU1 = await repo.GetProjectsByUserAsync(u1.Id);

            Assert.Equal(0, allUPU1.Count);
        }