public void GetUserFavourites()
        {
            // Arrange
            var userId        = 1;
            var pageParams    = new PageParams();
            var postForSearch = new PostForSearchDto()
            {
                SearchParams = "",
                OrderBy      = "",
                UserId       = ""
            };
            var favouritesFromRepo    = PostsFavouritedByNick().ToList();
            var favouritesToPagedList = new PagedList <Post>(favouritesFromRepo, 4, 1, 10);

            _repoMock.Setup(x => x.GetFavourites(userId, pageParams, postForSearch)).ReturnsAsync(favouritesToPagedList);

            foreach (var favourite in favouritesFromRepo)
            {
                _repoMock.Setup(x => x.GetMainPhotoForUser(favourite.UserId)).ReturnsAsync(new UserPhoto {
                    UserPhotoId = 2,
                    Url         = "some-img.jpg"
                });
            }

            // Act
            var result = _favouritesController.GetFavourites(userId, pageParams, postForSearch).Result;

            // Assert
            var okResult = Assert.IsType <OkObjectResult>(result);
        }
        public void GetPosts_WhenCalled_ReturnsListOfPosts_WithoutRatings()
        {
            // Arrange
            var posts = GetFakePostList().ToList();

            foreach (var post in posts)
            {
                post.Ratings = new List <Rating>();
            }

            var pageParams       = new PageParams();
            var postsToPagedList = new PagedList <Post>(posts, 4, 1, 10);
            var postForSearchDto = new PostForSearchDto()
            {
                SearchParams = " ",
                OrderBy      = "",
                UserId       = ""
            };

            _repoMock.Setup(x => x.GetPosts(pageParams, postForSearchDto)).ReturnsAsync(postsToPagedList);


            foreach (var post in posts)
            {
                _repoMock.Setup(x => x.GetMainPhotoForUser(post.UserId)).ReturnsAsync(new UserPhoto {
                    Url = "https://mk0agrivalleycohteqm.kinstacdn.com/wp-content/uploads/2017/12/blank-avi-sales-fit.jpg",
                });
                _repoMock.Setup(x => x.GetRatings(post.PostId)).ReturnsAsync(post.Ratings);
            }

            _repoMock.Setup(x => x.SaveAll()).ReturnsAsync(true);

            // Act
            var result = _postsController.GetPosts(pageParams, postForSearchDto).Result;

            // Assert
            var okResult    = Assert.IsType <OkObjectResult>(result);
            var returnValue = Assert.IsType <List <PostsForListDto> >(okResult.Value);

            Assert.Equal(posts.Count, returnValue.Count);
        }
Exemplo n.º 3
0
        // public async Task<PagedList<Post>> GetPosts(int userId, PageParams pageParams, PostForSearchDto postForSearchDto)
        // {
        //     var posts = _context.Posts.Where(x => x.UserId == userId).Include(c => c.Comments).Include(r => r.Ratings).Include(p => p.PostPhoto).OrderByDescending(x => x.Created);

        //     if (postForSearchDto.OrderBy == "most discussed")
        //         posts = posts.OrderByDescending(x => x.Comments.Count);

        //     if (postForSearchDto.OrderBy == "oldest")
        //         posts = posts.OrderBy(x => x.Created);

        //     if (postForSearchDto.OrderBy == "newest")
        //         posts = posts.OrderByDescending(x => x.Created);

        //     if (postForSearchDto.OrderBy == "highest rated")
        //         posts = posts.OrderByDescending(x => x.AverageRating);


        //     return await PagedList<Post>.CreateAsync(posts, pageParams.PageNumber, pageParams.PageSize);
        // }

        public async Task <PagedList <Post> > GetPosts(PageParams pageParams, PostForSearchDto postForSearchDto)
        {
            var searchQuery = postForSearchDto.SearchParams.Trim().ToLower();

            var posts = _context.Posts.Include(c => c.Comments).Include(r => r.Ratings).Include(p => p.PostPhoto).OrderByDescending(x => x.Created);

            var filteredPosts = posts.Where(x =>
                                            x.NameOfDish.ToLower().Contains(searchQuery) ||
                                            x.Cuisine.ToLower().Contains(searchQuery) ||
                                            x.Author.ToLower().Contains(searchQuery) ||
                                            x.Ingredients.ToLower().Contains(searchQuery));

            if (postForSearchDto.UserId.Count() > 0)
            {
                filteredPosts = filteredPosts.Where(x => x.UserId == int.Parse(postForSearchDto.UserId));
            }

            if (postForSearchDto.OrderBy == "most discussed")
            {
                filteredPosts = filteredPosts.OrderByDescending(x => x.Comments.Count);
            }

            if (postForSearchDto.OrderBy == "oldest")
            {
                filteredPosts = filteredPosts.OrderBy(x => x.Created);
            }

            if (postForSearchDto.OrderBy == "newest")
            {
                filteredPosts = filteredPosts.OrderByDescending(x => x.Created);
            }

            if (postForSearchDto.OrderBy == "highest rated")
            {
                filteredPosts = filteredPosts.OrderByDescending(x => x.AverageRating);
            }


            return(await PagedList <Post> .CreateAsync(filteredPosts, pageParams.PageNumber, pageParams.PageSize));
        }
Exemplo n.º 4
0
        public async Task <PagedList <Post> > GetFavourites(int userId, PageParams pageParams, PostForSearchDto postForSearchDto)
        {
            var posts = _context.Posts.Include(c => c.Comments).Include(r => r.Ratings).Include(p => p.PostPhoto).OrderByDescending(x => x.Created);

            var favourites = _context.Favourites.Where(u => u.FavouriterId == userId);

            var filteredPosts = posts.Where(x => favourites.Any(y => y.PostId == x.PostId));

            if (postForSearchDto.OrderBy == "most discussed")
            {
                filteredPosts = filteredPosts.OrderByDescending(x => x.Comments.Count);
            }

            if (postForSearchDto.OrderBy == "oldest")
            {
                filteredPosts = filteredPosts.OrderBy(x => x.Created);
            }

            if (postForSearchDto.OrderBy == "newest")
            {
                filteredPosts = filteredPosts.OrderByDescending(x => x.Created);
            }

            if (postForSearchDto.OrderBy == "highest rated")
            {
                filteredPosts = filteredPosts.OrderByDescending(x => x.AverageRating);
            }


            return(await PagedList <Post> .CreateAsync(filteredPosts, pageParams.PageNumber, pageParams.PageSize));
        }
        public async Task <IActionResult> GetPosts([FromQuery] PageParams pageParams, PostForSearchDto postForSearchDto)
        {
            var calculateAverageRatings = new CalculateAverageRatings(_recipeRepo);
            var posts = await _recipeRepo.GetPosts(pageParams, postForSearchDto);


            foreach (var post in posts)
            {
                // Assign users avatar to the post
                var authorAvatar = await _recipeRepo.GetMainPhotoForUser(post.UserId);

                if (authorAvatar != null)
                {
                    post.UserPhotoUrl = authorAvatar.Url;
                }

                // Calculate average rating of post
                var average = calculateAverageRatings.GetAverageRating(post.PostId).Result;
                if (post.Ratings.Count() == 0)
                {
                    post.AverageRating = 0;
                }
                else
                {
                    post.AverageRating = average;
                }
            }

            var postsFromRepo = _mapper.Map <IEnumerable <PostsForListDto> >(posts);

            Response.AddPagination(posts.CurrentPage, posts.PageSize, posts.TotalCount, posts.TotalPages);

            // If update to average rating, save to the database and return the newly updated posts
            if (await _recipeRepo.SaveAll())
            {
                return(Ok(postsFromRepo));
            }

            return(Ok(postsFromRepo));
        }
        public async Task <IActionResult> GetFavourites(int userId, [FromQuery] PageParams pageParams, PostForSearchDto postForSearchDto)
        {
            var favourites = await _recipeRepo.GetFavourites(userId, pageParams, postForSearchDto);

            foreach (var favourite in favourites)
            {
                var authorAvatar = await _recipeRepo.GetMainPhotoForUser(favourite.UserId);

                if (authorAvatar != null)
                {
                    favourite.UserPhotoUrl = authorAvatar.Url;
                }
            }

            var favouritesFromRepo = _mapper.Map <IEnumerable <PostsForListDto> >(favourites);

            Response.AddPagination(favourites.CurrentPage, favourites.PageSize, favourites.TotalCount, favourites.TotalPages);

            return(Ok(favouritesFromRepo));
        }