Example #1
0
        public async Task <ServiceResponseDto> FollowUserAsync(FollowerDto followerDto)
        {
            ServiceResponseDto followResultDto = new ServiceResponseDto();
            Follower           follower        = DtoToEntityConverter.Convert <Follower, FollowerDto>(followerDto);

            if (!await _applicationUserRepository.ExistsAsync(user => user.Id.Equals(followerDto.UserId)))
            {
                followResultDto.Message = "The user does not exist.";
                return(followResultDto);
            }

            if (!await _applicationUserRepository.ExistsAsync(user => user.Id.Equals(followerDto.FollowerUserId)))
            {
                followResultDto.Message = "The follower user does not exist.";
                return(followResultDto);
            }

            string username = (await _applicationUserRepository.FindSingleByExpressionAsync(user => user.Id.Equals(followerDto.UserId))).UserName;

            if (await _followerRepository.ExistsAsync(follow => follow.UserId.Equals(followerDto.UserId) && follow.FollowerUserId.Equals(followerDto.FollowerUserId)))
            {
                followResultDto.Message = $"You already follow {username}";
                return(followResultDto);
            }

            if (await _followerRepository.CreateAsync(follower))
            {
                followResultDto.Success = true;
                followResultDto.Message = $"Successfully followed {username}";
                return(followResultDto);
            }

            followResultDto.Message = "Something happened try again later..";
            return(followResultDto);
        }
Example #2
0
        public async Task AddMemeToCollectionAsync_Should_Pass()
        {
            // Arrange
            MemeDto expectedMemeDto = new MemeDto
            {
                Id       = "a0Q558q",
                ImageUrl = "https://images-cdn.9gag.com/photo/a0Q558q_700b.jpg",
                VideoUrl = "http://img-9gag-fun.9cache.com/photo/a0Q558q_460sv.mp4",
                PageUrl  = "http://9gag.com/gag/a0Q558q",
                Title    = "Old but Gold"
            };
            Meme entity = DtoToEntityConverter.Convert <Meme, MemeDto>(expectedMemeDto);

            Assert.IsTrue(await MemeRepository.CreateAsync(entity));
            AddMemeToCollectionDto addMemeToCollectionDto = new AddMemeToCollectionDto
            {
                MemeId       = "a0Q558q",
                UserId       = 1,
                CollectionId = 1
            };

            // Act
            ServiceResponseDto addMemeToCollectionResultDto = await CollectionItemDetailService.AddMemeToCollectionAsync(addMemeToCollectionDto);

            // Assert
            Assert.IsTrue(addMemeToCollectionResultDto.Success);
            Assert.AreEqual(addMemeToCollectionResultDto.Message, "Successfully added meme to the collection.");
            CollectionItemDetail actualCollectionItemDetail = (await CollectionItemDetailRepository.GetAllAsync()).First();

            Assert.IsTrue(
                actualCollectionItemDetail.MemeId.Equals("a0Q558q") &&
                actualCollectionItemDetail.AddedByUserId.Equals(1) &&
                actualCollectionItemDetail.CollectionId.Equals(1));
        }
Example #3
0
        public bool InkPen(Pen pen, Ink ink)
        {
            Entities.Pen penEntity = DtoToEntityConverter.Convert(pen);
            Entities.Ink inkEntity = DtoToEntityConverter.Convert(ink);

            penEntity.InkUp(inkEntity);

            pen = EntityToDtoConverter.Convert(penEntity);

            return(Storage.UpdatePen(pen));
        }
Example #4
0
        public async Task <MemeDto> GetRandomMemeAsync()
        {
            MemeDto memeDto = await _memeFetcherService.GetRandomMemeAsync();

            while (await _memeRepository.ExistsAsync(meme => meme.Id.Equals(memeDto.Id)))
            {
                memeDto = await _memeFetcherService.GetRandomMemeAsync();
            }
            Meme actualMeme = DtoToEntityConverter.Convert <Meme, MemeDto>(memeDto);
            await _memeRepository.CreateAsync(actualMeme);

            return(memeDto);
        }
Example #5
0
        public async Task <bool> LikeMemeAsync(MemeLikeDto memeLikeDto)
        {
            MemeLike actualMemeLike = DtoToEntityConverter.Convert <MemeLike, MemeLikeDto>(memeLikeDto);

            if (!await _memeLikeRepository.ExistsAsync(memeLike => memeLike.MemeId.Equals(memeLikeDto.MemeId) && memeLike.UserId.Equals(memeLikeDto.UserId)))
            {
                return(await _memeLikeRepository.CreateAsync(actualMemeLike));
            }

            actualMemeLike = await _memeLikeRepository.FindSingleByExpressionAsync(memeLike => memeLike.MemeId.Equals(memeLikeDto.MemeId) && memeLike.UserId.Equals(memeLikeDto.UserId));

            actualMemeLike.IsLike = true;
            return(await _memeLikeRepository.UpdateAsync(actualMemeLike));
        }
Example #6
0
        public async Task <ServiceResponseDto> ShareMemeToMutualFollowerAsync(SharedMemeDto sharedMemeDto)
        {
            SharedMeme sharedMeme = DtoToEntityConverter.Convert <SharedMeme, SharedMemeDto>(sharedMemeDto);
            string     username   = (await _userRepository.FindSingleByExpressionAsync(user => user.Id.Equals(sharedMeme.ReceiverUserId))).UserName;

            if (await _sharedMemeRepository.CreateAsync(sharedMeme))
            {
                return new ServiceResponseDto {
                           Message = username, Success = true
                }
            }
            ;

            return(new ServiceResponseDto {
                Message = username, Success = false
            });
        }
    }
Example #7
0
        public async Task <ServiceResponseDto> CreateCollectionAsync(CollectionDto collectionDto)
        {
            ServiceResponseDto createCollectionResultDto = new ServiceResponseDto();

            if (await _collectionRepository.ExistsAsync(collection => collection.Name.Equals(collectionDto.Name)))
            {
                createCollectionResultDto.Message = $"A collection with the name {collectionDto.Name} already exists.";
                return(createCollectionResultDto);
            }
            Collection actualCollection = DtoToEntityConverter.Convert <Collection, CollectionDto>(collectionDto);

            if (await _collectionRepository.CreateAsync(actualCollection))
            {
                createCollectionResultDto.Success = true;
                createCollectionResultDto.Message = "The collection is created successfully.";
                return(createCollectionResultDto);
            }
            createCollectionResultDto.Message = "Something happened try again later..";
            return(createCollectionResultDto);
        }
Example #8
0
        public async Task GetMemeAsync_With_Id_a0Q558q_Should_Return_MemeAsync()
        {
            // Arrange
            MemeDto expectedMemeDto = new MemeDto
            {
                Id       = "a0Q558q",
                ImageUrl = "https://images-cdn.9gag.com/photo/a0Q558q_700b.jpg",
                VideoUrl = "http://img-9gag-fun.9cache.com/photo/a0Q558q_460sv.mp4",
                PageUrl  = "http://9gag.com/gag/a0Q558q",
                Title    = "Old but Gold"
            };
            Meme entity = DtoToEntityConverter.Convert <Meme, MemeDto>(expectedMemeDto);

            Assert.IsTrue(await MemeRepository.CreateAsync(entity));

            // Act
            MemeDto actualMemeDto = await MemeService.GetMemeAsync("a0Q558q");

            // Assert
            Assert.AreEqual(expectedMemeDto, actualMemeDto);
        }
Example #9
0
        public async Task RemoveMemeFromCollectionAsync_Should_Throw_ApplicationUserIsNotAuthorizedException()
        {
            // Arrange
            MemeDto expectedMemeDto = new MemeDto
            {
                Id       = "a0Q558q",
                ImageUrl = "https://images-cdn.9gag.com/photo/a0Q558q_700b.jpg",
                VideoUrl = "http://img-9gag-fun.9cache.com/photo/a0Q558q_460sv.mp4",
                PageUrl  = "http://9gag.com/gag/a0Q558q",
                Title    = "Old but Gold"
            };
            Meme entity = DtoToEntityConverter.Convert <Meme, MemeDto>(expectedMemeDto);

            Assert.IsTrue(await MemeRepository.CreateAsync(entity));
            AddMemeToCollectionDto addMemeToCollectionDto = new AddMemeToCollectionDto
            {
                MemeId       = "a0Q558q",
                UserId       = 1,
                CollectionId = 1
            };
            ServiceResponseDto addMemeToCollectionResultDto = await CollectionItemDetailService.AddMemeToCollectionAsync(addMemeToCollectionDto);

            Assert.IsTrue(addMemeToCollectionResultDto.Success);


            RemoveMemeFromCollectionDto removeMemeFromCollectionDto = new RemoveMemeFromCollectionDto
            {
                CollectionId           = 1,
                CollectionItemDetailId = 1,
                UserId = 2
            };

            // Act & Assert
            ServiceResponseDto serviceResponseDto =
                await CollectionItemDetailService.RemoveMemeFromCollectionAsync(removeMemeFromCollectionDto);

            Assert.IsFalse(serviceResponseDto.Success);
            Assert.AreEqual(serviceResponseDto.Message, "Failed to remove meme because user is not authorized.");
        }
Example #10
0
        public async Task RemoveMemeFromCollectionAsync_Should_Pass()
        {
            // Arrange
            MemeDto expectedMemeDto = new MemeDto
            {
                Id       = "a0Q558q",
                ImageUrl = "https://images-cdn.9gag.com/photo/a0Q558q_700b.jpg",
                VideoUrl = "http://img-9gag-fun.9cache.com/photo/a0Q558q_460sv.mp4",
                PageUrl  = "http://9gag.com/gag/a0Q558q",
                Title    = "Old but Gold"
            };
            Meme entity = DtoToEntityConverter.Convert <Meme, MemeDto>(expectedMemeDto);

            Assert.IsTrue(await MemeRepository.CreateAsync(entity));
            AddMemeToCollectionDto addMemeToCollectionDto = new AddMemeToCollectionDto
            {
                MemeId       = "a0Q558q",
                UserId       = 1,
                CollectionId = 1
            };
            ServiceResponseDto addMemeToCollectionResultDto = await CollectionItemDetailService.AddMemeToCollectionAsync(addMemeToCollectionDto);

            Assert.IsTrue(addMemeToCollectionResultDto.Success);
            RemoveMemeFromCollectionDto removeMemeFromCollectionDto = new RemoveMemeFromCollectionDto
            {
                CollectionId           = 1,
                CollectionItemDetailId = 1,
                UserId = 1
            };

            // Act
            ServiceResponseDto serviceResponseDto = await CollectionItemDetailService.RemoveMemeFromCollectionAsync(removeMemeFromCollectionDto);

            // Assert
            Assert.IsTrue(serviceResponseDto.Success);
            List <CollectionItemDetail> collectionItemDetails = (await CollectionItemDetailRepository.GetAllAsync()).ToList();

            Assert.AreEqual(collectionItemDetails.Count, 0);
        }
Example #11
0
        public async Task <ServiceResponseDto> UnFollowUserAsync(FollowerDto followerDto)
        {
            ServiceResponseDto unFollowResultDto = new ServiceResponseDto();
            Follower           follower          = DtoToEntityConverter.Convert <Follower, FollowerDto>(followerDto);

            if (!await _applicationUserRepository.ExistsAsync(user => user.Id.Equals(followerDto.UserId)))
            {
                unFollowResultDto.Message = "The user does not exist.";
                return(unFollowResultDto);
            }

            if (!await _applicationUserRepository.ExistsAsync(user => user.Id.Equals(followerDto.FollowerUserId)))
            {
                unFollowResultDto.Message = "The follower user does not exist.";
                return(unFollowResultDto);
            }

            string username = (await _applicationUserRepository.FindSingleByExpressionAsync(user => user.Id.Equals(followerDto.UserId))).UserName;

            if (!await _followerRepository.ExistsAsync(follow => follow.UserId.Equals(followerDto.UserId) && follow.FollowerUserId.Equals(followerDto.FollowerUserId)))
            {
                unFollowResultDto.Message = $"you do not have {username} followed";
                return(unFollowResultDto);
            }

            Follower actualFollower = await _followerRepository.FindSingleByExpressionAsync(follwr => follwr.UserId.Equals(follower.UserId) && follwr.FollowerUserId.Equals(follower.FollowerUserId));

            if (await _followerRepository.DeleteAsync(actualFollower))
            {
                unFollowResultDto.Success = true;
                unFollowResultDto.Message = $"Successfully unfollowed {username}";
                return(unFollowResultDto);
            }

            unFollowResultDto.Message = "Something happened try again later..";
            return(unFollowResultDto);
        }
        public async Task GetAllCollectionsAsync_Should_Return_3_Collections()
        {
            // Arrange
            CollectionDto expectedCollectionDto1 = new CollectionDto
            {
                UserId = 1,
                Name   = "Dank Memes1"
            };
            Collection entity1 = DtoToEntityConverter.Convert <Collection, CollectionDto>(expectedCollectionDto1);
            await CollectionRepository.CreateAsync(entity1);

            CollectionDto expectedCollectionDto2 = new CollectionDto
            {
                UserId = 1,
                Name   = "Dank Memes2"
            };
            Collection entity2 = DtoToEntityConverter.Convert <Collection, CollectionDto>(expectedCollectionDto2);
            await CollectionRepository.CreateAsync(entity2);

            CollectionDto expectedCollectionDto3 = new CollectionDto
            {
                UserId = 1,
                Name   = "Dank Memes3"
            };
            Collection entity3 = DtoToEntityConverter.Convert <Collection, CollectionDto>(expectedCollectionDto3);
            await CollectionRepository.CreateAsync(entity3);

            // Act
            CollectionDto actualCollectionDto1 = (await CollectionService.GetAllCollectionsAsync()).First();
            CollectionDto actualCollectionDto2 = (await CollectionService.GetAllCollectionsAsync()).Skip(1).First();
            CollectionDto actualCollectionDto3 = (await CollectionService.GetAllCollectionsAsync()).Skip(2).First();

            // Assert
            Assert.AreEqual(expectedCollectionDto1, actualCollectionDto1);
            Assert.AreEqual(expectedCollectionDto2, actualCollectionDto2);
            Assert.AreEqual(expectedCollectionDto3, actualCollectionDto3);
        }
Example #13
0
        public async Task <ServiceResponseDto> CreateCommentAsync(CommentDto commentDto)
        {
            ServiceResponseDto commentResultDto = new ServiceResponseDto
            {
                Message = "Something happened try again later."
            };

            if (!await _memeRepository.ExistsAsync(meme => meme.Id.Equals(commentDto.MemeId)))
            {
                return(commentResultDto);
            }

            Comment comment = DtoToEntityConverter.Convert <Comment, CommentDto>(commentDto);

            if (!await _commentRepository.CreateAsync(comment))
            {
                return(commentResultDto);
            }

            commentResultDto.Message = "Successfully added the comment.";
            commentResultDto.Success = true;

            return(commentResultDto);
        }