public async Task <ServiceResponseDto> UnSubscribeFromCollectionAsync(int userId, int collectionId)
        {
            ServiceResponseDto serviceResponseDto = new ServiceResponseDto();

            if (!await _collectionSubscriberRepository.ExistsAsync(collSubscriber =>
                                                                   collSubscriber.CollectionId.Equals(collectionId) && collSubscriber.UserId.Equals(userId)))
            {
                serviceResponseDto.Message = "User is not subscribed to collection.";
                return(serviceResponseDto);
            }

            CollectionSubscriber collectionSubscriber = await _collectionSubscriberRepository
                                                        .FindSingleByExpressionAsync(collectSub => collectSub.CollectionId.Equals(collectionId) && collectSub.UserId.Equals(userId));

            if (await _collectionSubscriberRepository.DeleteAsync(collectionSubscriber))
            {
                serviceResponseDto.Success = true;
                serviceResponseDto.Message = "Successfully unsubscribed from collection.";
            }
            else
            {
                serviceResponseDto.Message = "Failed to unsubscribe from collection.";
            }

            return(serviceResponseDto);
        }
Esempio n. 2
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);
        }
        public async Task <ServiceResponseDto> SubscribeToCollectionAsync(int userId, int collectionId)
        {
            ServiceResponseDto serviceResponseDto = new ServiceResponseDto();

            if (await _collectionSubscriberRepository.ExistsAsync(collSubscriber =>
                                                                  collSubscriber.CollectionId.Equals(collectionId) && collSubscriber.UserId.Equals(userId)))
            {
                serviceResponseDto.Message = "User is already subscribed to collection.";
                return(serviceResponseDto);
            }
            CollectionSubscriber collectionSubscriber = new CollectionSubscriber
            {
                CollectionId = collectionId,
                UserId       = userId
            };

            if (await _collectionSubscriberRepository.CreateAsync(collectionSubscriber))
            {
                serviceResponseDto.Success = true;
                serviceResponseDto.Message = "Successfully subscribed to collection.";
            }
            else
            {
                serviceResponseDto.Message = "Failed to subscribe to collection.";
            }
            return(serviceResponseDto);
        }
Esempio n. 4
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));
        }
Esempio n. 5
0
        public async Task <ServiceResponseDto> MarkSharedMemeAsSeenAsync(SharedMemeDto sharedMeme)
        {
            ServiceResponseDto sharedMemeMarkAsSeenDto = new ServiceResponseDto
            {
                Message = "Something happened try again later.."
            };

            if (!await _sharedMemeRepository.ExistsAsync(sm => sm.Id.Equals(sharedMeme.Id)))
            {
                sharedMemeMarkAsSeenDto.Message = "Something happened try again later..";
                return(sharedMemeMarkAsSeenDto);
            }

            SharedMeme sharedMemeEntity = await _sharedMemeRepository.FindSingleByExpressionAsync(sm => sm.Id.Equals(sharedMeme.Id));

            if (sharedMemeEntity.IsSeen)
            {
                sharedMemeMarkAsSeenDto.Message = "The shared meme is already marked as seen.";
                return(sharedMemeMarkAsSeenDto);
            }

            sharedMemeEntity.IsSeen = true;

            if (!await _sharedMemeRepository.UpdateAsync(sharedMemeEntity))
            {
                return(sharedMemeMarkAsSeenDto);
            }

            sharedMemeMarkAsSeenDto.Success = true;
            sharedMemeMarkAsSeenDto.Message = "Successfully marked the shared meme as seen.";
            return(sharedMemeMarkAsSeenDto);
        }
        public async Task <ServiceResponseDto> RemoveMemeFromCollectionAsync(RemoveMemeFromCollectionDto removeMemeFromCollectionDto)
        {
            ServiceResponseDto serviceResultDto = new ServiceResponseDto();

            bool isOwnerOfCollection = await _collectionRepository.ExistsAsync(collection =>
                                                                               collection.Id.Equals(removeMemeFromCollectionDto.CollectionId) &&
                                                                               collection.UserId.Equals(removeMemeFromCollectionDto.UserId));

            CollectionItemDetail actualCollectionItemDetail = await _collectionItemDetailRepository
                                                              .FindSingleByExpressionAsync(collectionItemDetail => collectionItemDetail.Id.Equals(removeMemeFromCollectionDto.CollectionItemDetailId));

            // TODO: check if user is currently subscribed?

            if (!actualCollectionItemDetail.AddedByUserId.Equals(removeMemeFromCollectionDto.UserId) || !isOwnerOfCollection)
            {
                serviceResultDto.Message = "Failed to remove meme because user is not authorized.";
                return(serviceResultDto);
            }

            if (await _collectionItemDetailRepository.DeleteAsync(actualCollectionItemDetail))
            {
                serviceResultDto.Success = true;
                serviceResultDto.Message = "Successfully removed meme from collection.";
            }
            else
            {
                serviceResultDto.Message = "Failed to remove meme from the collection";
            }
            return(serviceResultDto);
        }
        public async Task DeleteCollectionAsync_Should_Pass()
        {
            // Arrange
            CollectionDto collectionDto = new CollectionDto
            {
                UserId = 1,
                Name   = "Dank Memes"
            };
            await CollectionService.CreateCollectionAsync(collectionDto);

            List <CollectionDto> collections = (await CollectionService.GetAllCollectionsAsync()).ToList();

            Assert.IsTrue(collections.Count.Equals(1));

            // Act
            DeleteCollectionDto deleteCollectionDto = new DeleteCollectionDto {
                Id = 1, UserId = 1
            };
            ServiceResponseDto deleteCollectionResultDto = await CollectionService.DeleteCollectionAsync(deleteCollectionDto);

            // Assert
            Assert.IsTrue(deleteCollectionResultDto.Success);
            collections = (await CollectionService.GetAllCollectionsAsync()).ToList();
            Assert.IsTrue(collections.Count.Equals(0));
        }
Esempio n. 8
0
        public async Task <ServiceResponseDto> DeleteCollectionAsync(DeleteCollectionDto deleteCollectionDto)
        {
            ServiceResponseDto deleteCollectionResultDto = new ServiceResponseDto();

            if (!await _collectionRepository.ExistsAsync(collection => collection.Id.Equals(deleteCollectionDto.Id)))
            {
                deleteCollectionResultDto.Message = "The collection does not exist.";
                return(deleteCollectionResultDto);
            }

            if (!await _collectionRepository.ExistsAsync(collection => collection.Id.Equals(deleteCollectionDto.Id) && collection.UserId.Equals(deleteCollectionDto.UserId)))
            {
                deleteCollectionResultDto.Message = "The collection does not exist or you are not the owner of the collection.";
                return(deleteCollectionResultDto);
            }

            Collection actualCollection = await _collectionRepository.FindSingleByExpressionAsync(collection => collection.Id.Equals(deleteCollectionDto.Id) && collection.UserId.Equals(deleteCollectionDto.UserId));

            if (await _collectionRepository.DeleteAsync(actualCollection))
            {
                deleteCollectionResultDto.Success = true;
                deleteCollectionResultDto.Message = "Successfully deleted the collection.";
                return(deleteCollectionResultDto);
            }

            deleteCollectionResultDto.Message = "Something happened try again later..";
            return(deleteCollectionResultDto);
        }
Esempio n. 9
0
        /// <summary>
        /// Create a result object given the value and errors list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public ServiceResponseDto <T> CreateResult <T>(T value, IEnumerable <IError> errors)
        {
            var result = new ServiceResponseDto <T>()
            {
                Errors       = errors?.ToList(),
                ResultObject = value
            };

            return(result);
        }
        private async Task HandleAuthError()
        {
            var originalStatusCode = Context.Response.StatusCode;
            var originalStatusMsg  = ((HttpStatusCode)originalStatusCode).ToString();

            Context.Response.ContentType = MediaTypeNames.Application.Json;
            Context.Response.StatusCode  = (int)HttpStatusCode.OK;

            var responseBody          = new ServiceResponseDto(originalStatusCode, originalStatusMsg);
            var serializedResponseDto = JsonConvert.SerializeObject(responseBody);
            await Context.Response.WriteAsync(serializedResponseDto);
        }
Esempio n. 11
0
        public async Task <IActionResult> MarkMemeAsSeenAsync([FromBody] MarkMemeAsSeenViewModel markMemeAsSeenViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(Json(ModelState.DefaultInvalidModelStateWithErrorMessages()));
            }

            int                userId                  = Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier));
            SharedMemeDto      sharedMemeDto           = markMemeAsSeenViewModel.GetSharedMemeDto(userId);
            ServiceResponseDto sharedMemeMarkAsSeenDto = await _memeSharingService.MarkSharedMemeAsSeenAsync(sharedMemeDto);

            return(Json(sharedMemeMarkAsSeenDto));
        }
Esempio n. 12
0
        public async Task <IActionResult> CreateCommentAsync([FromBody] CreateCommentViewModel createCommentViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(Json(ModelState.DefaultInvalidModelStateWithErrorMessages()));
            }

            int                userId           = Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier));
            CommentDto         commentDto       = createCommentViewModel.GetCommentDto(userId);
            ServiceResponseDto commentResultDto = await _commentService.CreateCommentAsync(commentDto);

            return(Json(commentResultDto));
        }
Esempio n. 13
0
        public async Task FollowUserAsync_Should_Fail_User_Does_Not_Exist()
        {
            // Arrange
            FollowerDto followerDto = new FollowerDto
            {
                UserId         = 1,
                FollowerUserId = 6
            };

            // Act
            ServiceResponseDto responseDto = await FollowerService.FollowUserAsync(followerDto);

            // Assert
            Assert.IsFalse(responseDto.Success);
        }
Esempio n. 14
0
        public async Task FollowUserAsync_Should_Pass()
        {
            // Arrange
            FollowerDto followerDto = new FollowerDto
            {
                UserId         = 1,
                FollowerUserId = 2
            };

            // Act
            ServiceResponseDto responseDto = await FollowerService.FollowUserAsync(followerDto);

            // Assert
            Assert.IsTrue(responseDto.Success);
        }
Esempio n. 15
0
        public async Task UnFollowUserAsync_Should_Fail_Because_User_Is_Not_Currently_Followed()
        {
            // Arrange
            FollowerDto followerDto = new FollowerDto
            {
                UserId         = 1,
                FollowerUserId = 2
            };

            // Act
            ServiceResponseDto responseDto = await FollowerService.UnFollowUserAsync(followerDto);

            // Assert
            Assert.IsFalse(responseDto.Success);
        }
Esempio n. 16
0
        public async Task <IActionResult> UnFollowAsync([FromBody] UnFollowViewModel unFollowViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(Json(ModelState.DefaultInvalidModelStateWithErrorMessages()));
            }

            int                userId            = Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier));
            FollowerDto        follower          = unFollowViewModel.GetFollowerDto(userId);
            ServiceResponseDto unFollowResultDto = await _followerService.UnFollowUserAsync(follower);

            await _tempDataService.UpdateTempDataAsync(TempData, userId);

            return(Json(unFollowResultDto));
        }
Esempio n. 17
0
        public async Task <IActionResult> ShareMemeToFriendAsync([FromBody] ShareMemeViewModel shareMemeViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(Json(ModelState.DefaultInvalidModelStateWithErrorMessages()));
            }

            int                userId              = Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier));
            SharedMemeDto      sharedMemeDto       = shareMemeViewModel.GetSharedMemeDto(userId);
            ServiceResponseDto sharedMemeResultDto = await _memeSharingService.ShareMemeToMutualFollowerAsync(sharedMemeDto);

            return(sharedMemeResultDto.Success
                ? Json(new { success = true, to = sharedMemeResultDto.Message })
                : Json(new { success = false, message = "Something happened.." }));
        }
Esempio n. 18
0
        public async Task ShareMemeToMutualFollowerAsync_Should_Pass()
        {
            // Arrange
            SharedMemeDto sharedMemeDto = new SharedMemeDto
            {
                SenderUserId   = 1,
                ReceiverUserId = 2,
                MemeId         = "a0Q558q"
            };

            // Act
            ServiceResponseDto serviceResponseDto = await MemeSharingService.ShareMemeToMutualFollowerAsync(sharedMemeDto);

            // Assert
            Assert.IsTrue(serviceResponseDto.Success);
        }
Esempio n. 19
0
        public async Task AddMemeToCollectionAsync_Should_Fail_With_Message_Meme_Does_Not_Exist()
        {
            // Arrange
            AddMemeToCollectionDto addMemeToCollectionDto = new AddMemeToCollectionDto
            {
                MemeId       = "a0Q558q",
                UserId       = 1,
                CollectionId = 1
            };

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

            // Assert
            Assert.IsFalse(serviceResultDto.Success);
            Assert.AreEqual(serviceResultDto.Message, "Meme does not exist.");
        }
Esempio n. 20
0
        public async Task CreateCollectionAsync_Should_Pass()
        {
            // Arrange
            CollectionDto expectedCollectionDto1 = new CollectionDto
            {
                UserId = 1,
                Name   = "Dank"
            };
            ServiceResponseDto createCollectionResultDto = await CollectionService.CreateCollectionAsync(expectedCollectionDto1);

            Assert.IsTrue(createCollectionResultDto.Success);

            // Act
            CollectionDto actualCollectionDto1 = await CollectionService.GetCollectionByIdAsync(1);

            // Assert
            Assert.AreEqual(expectedCollectionDto1, actualCollectionDto1);
        }
        public JsonResult Authenticate(CredentialsViewModel model)
        {
            ServiceResponseDto <AuthenticationResultViewModel> serviceResult =
                SendRequest <AuthenticationResultViewModel, CredentialsViewModel>(new ServiceRequestDto <CredentialsViewModel>
            {
                ServiceUrl        = HomeControllerServiceUrl.AuthenticationService,
                ServiceMethodName = HomeControllerServiceUrl.AuthenticationMethod,
                RequestType       = WebRequestMethods.Http.Post,
                RequestModel      = model
            });

            if (serviceResult.ErrorModel != null)
            {
                Response.StatusCode = serviceResult.ErrorModel.StatusCode;

                return(Json(serviceResult.ErrorModel, JsonRequestBehavior.AllowGet));
            }

            return(Json(serviceResult.ResultViewModel, JsonRequestBehavior.DenyGet));
        }
Esempio n. 22
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);
        }
        public async Task <ServiceResponseDto> DeAuthorizeSubscriberFromCollectionAsync(int userId, int collectionId)
        {
            ServiceResponseDto   serviceResponseDto         = new ServiceResponseDto();
            CollectionSubscriber actualCollectionSubscriber = await _collectionSubscriberRepository
                                                              .FindSingleByExpressionAsync(collectionSubscriber =>
                                                                                           collectionSubscriber.CollectionId.Equals(collectionId) &&
                                                                                           collectionSubscriber.UserId.Equals(userId));

            actualCollectionSubscriber.IsAuthorized = false;
            if (await _collectionSubscriberRepository.UpdateAsync(actualCollectionSubscriber))
            {
                serviceResponseDto.Success = true;
                serviceResponseDto.Message = "Successfully de-authorized user from collection.";
            }
            else
            {
                serviceResponseDto.Message = "Failed to de-authorize user from collection.";
            }
            return(serviceResponseDto);
        }
Esempio n. 24
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.");
        }
Esempio n. 25
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);
        }
        public JsonResult GetTokenTest(string secureToken)
        {
            ServiceResponseDto <TokenTestResultViewModel> serviceResult =
                SendRequest <TokenTestResultViewModel>(new ServiceRequestDto
            {
                ServiceUrl        = HomeControllerServiceUrl.TestService,
                ServiceMethodName = HomeControllerServiceUrl.TestGetWithTokenHeaderMethod,
                RequestType       = WebRequestMethods.Http.Get,
                HeaderParameters  = new Dictionary <string, string>
                {
                    { "Token", secureToken }
                }
            });

            if (serviceResult.ErrorModel != null)
            {
                Response.StatusCode = serviceResult.ErrorModel.StatusCode;

                return(Json(serviceResult.ErrorModel, JsonRequestBehavior.AllowGet));
            }

            return(Json(serviceResult.ResultViewModel, JsonRequestBehavior.AllowGet));
        }
        public JsonResult PostBasicAuthTest(string authorizationString)
        {
            ServiceResponseDto <TokenTestResultViewModel> serviceResult =
                SendRequest <TokenTestResultViewModel>(new ServiceRequestDto
            {
                ServiceUrl        = HomeControllerServiceUrl.TestService,
                ServiceMethodName = HomeControllerServiceUrl.TestPostWithBasicAuthHeaderMethodName,
                RequestType       = WebRequestMethods.Http.Post,
                HeaderParameters  = new Dictionary <string, string>
                {
                    { "Authorization", authorizationString }
                }
            });

            if (serviceResult.ErrorModel != null)
            {
                Response.StatusCode = serviceResult.ErrorModel.StatusCode;

                return(Json(serviceResult.ErrorModel, JsonRequestBehavior.AllowGet));
            }

            return(Json(serviceResult.ResultViewModel, JsonRequestBehavior.AllowGet));
        }
Esempio n. 28
0
        public async Task CreateCommentAsync_Should_Pass()
        {
            // Arrange
            CommentDto commentDto = new CommentDto
            {
                MemeId = "a0Q558q",
                Text   = "Haha, super funny meme!",
                UserId = 1
            };
            // NOTE: the repository is used here instead of CommentService.GetCommentsByMemeIdAsync because relational data will not work in memory.
            List <Comment> commentsBefore = (List <Comment>) await CommentRepository.FindManyByExpressionAsync(comment => comment.MemeId.Equals("a0Q558q"));

            Assert.IsTrue(commentsBefore.Count.Equals(0));

            // Act
            ServiceResponseDto responseDto = await CommentService.CreateCommentAsync(commentDto);

            // Assert
            Assert.IsTrue(responseDto.Success);
            List <Comment> commentsAfter = (List <Comment>) await CommentRepository.FindManyByExpressionAsync(comment => comment.MemeId.Equals("a0Q558q"));

            Assert.IsTrue(commentsAfter.Count.Equals(1));
        }
Esempio n. 29
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);
        }
Esempio n. 30
0
        public async Task Initialize()
        {
            ApplicationDbFactory = new ApplicationDbFactory("InMemoryDatabase");
            await ApplicationDbFactory.Create().Database.EnsureDeletedAsync();

            await ApplicationDbFactory.Create().Database.EnsureCreatedAsync();

            ApplicationDbFactory.Create().ResetValueGenerators();
            // Relational-specific methods can only be used when the context is using a relational database provider..
            // ApplicationDbFactory.Create().Database.Migrate();
            CollectionRepository           = new CollectionRepository(ApplicationDbFactory.Create(), CollectionValidator);
            MemeRepository                 = new MemeRepository(ApplicationDbFactory.Create(), MemeValidator);
            CollectionItemDetailRepository = new CollectionItemDetailRepository(ApplicationDbFactory.Create(), CollectionItemDetailValidator);
            CollectionService              = new CollectionService(CollectionRepository);
            CollectionItemDetailService    = new CollectionItemDetailService(CollectionItemDetailRepository, CollectionRepository, MemeRepository);
            CollectionDto collectionDto = new CollectionDto
            {
                UserId = 1,
                Name   = "Dank Memes"
            };
            ServiceResponseDto createCollectionResultDto = await CollectionService.CreateCollectionAsync(collectionDto);

            Assert.IsTrue(createCollectionResultDto.Success);
        }