Пример #1
0
        public async Task RemoveFollowerAsync_WithValidData_ShouldRemoveFollowerFromDatabase()
        {
            // Arrange
            this.InitilaizeMapper();
            var context                = InMemoryDbContext.Initiliaze();
            var userRepository         = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userFollowerRepository = new EfRepository <UserFollower>(context);
            var profileService         = new ProfilesService(userRepository, userFollowerRepository);

            await this.SeedUsers(context);

            await userFollowerRepository.AddAsync(new UserFollower { UserId = "userOneId", FollowerId = "userTwoId" });

            await userFollowerRepository.SaveChangesAsync();

            var model = new AddFollowerModel
            {
                UserId     = "userOneId",
                FollowerId = "userTwoId",
            };

            // Act
            int expectedCount = userFollowerRepository.All().Count() - 1;
            await profileService.RemoveFollowerAsync(model);

            int actualCount = userFollowerRepository.All().Count();

            // Assert
            Assert.Equal(expectedCount, actualCount);
        }
Пример #2
0
        /*
         * A method that allows you to unfollow somebody through their profile.
         */
        public async Task <string> RemoveFollowerAsync(AddFollowerModel model)
        {
            var follower = this.userFollowerRepository.All()
                           .FirstOrDefault(uf => uf.UserId == model.UserId && uf.FollowerId == model.FollowerId);

            this.userFollowerRepository.Delete(follower);

            await this.userFollowerRepository.SaveChangesAsync();

            return("Unfollowed user successfully");
        }
Пример #3
0
        public async Task <IActionResult> Unfollow([FromBody] AddFollowerModel model)
        {
            bool followerExists = await this.service.FollowerExistsAsync(model.UserId, model.FollowerId);

            if (!followerExists)
            {
                return(BadRequest(new BadRequestViewModel {
                    Message = "You cannot unfollow this user, because you don't follow him."
                }));
            }

            var result = await this.service.RemoveFollowerAsync(model);

            return(Ok(result));
        }
Пример #4
0
        public async Task <IActionResult> Follow([FromBody] AddFollowerModel model)
        {
            bool followerExists = await this.service.FollowerExistsAsync(model.UserId, model.FollowerId);

            if (followerExists)
            {
                return(BadRequest(new BadRequestViewModel {
                    Message = "You are already following this user."
                }));
            }

            // Follow the user
            await this.service.AddFollowerAsync(model);

            return(Ok());
        }
Пример #5
0
        public async Task AddFollowerAsync_WithValidData_ShouldReturnTrue()
        {
            // Arrange
            var context                = InMemoryDbContext.Initiliaze();
            var userRepository         = new EfDeletableEntityRepository <ApplicationUser>(context);
            var userFollowerRepository = new EfRepository <UserFollower>(context);
            var profileService         = new ProfilesService(userRepository, userFollowerRepository);

            await this.SeedUsers(context);

            var model = new AddFollowerModel
            {
                UserId     = "userOneId",
                FollowerId = "userTwoId",
            };

            // Act
            bool actualResult = await profileService.AddFollowerAsync(model);

            // Assert
            Assert.True(actualResult);
        }
Пример #6
0
        /*
         * A method that allows you to follow somebody through their profile, by clicking the follow button.
         */
        public async Task <bool> AddFollowerAsync(AddFollowerModel model)
        {
            var userToFollow = this.userRepository.All().FirstOrDefault(x => x.Id == model.UserId);

            var isApproved = false;

            if (userToFollow.IsPrivate == false)
            {
                isApproved = true;
            }

            var userFollower = new UserFollower
            {
                UserId     = model.UserId,
                FollowerId = model.FollowerId,
                IsApproved = isApproved,
            };

            await this.userFollowerRepository.AddAsync(userFollower);

            var result = await this.userFollowerRepository.SaveChangesAsync() > 0;

            return(result);
        }