Пример #1
0
        public async Task <IActionResult> Reviews(ReviewsResourceParameters parameters)
        {
            var response = await _reviewData.GetReviews(parameters);

            if (!response.IsSuccessful)
            {
                return(View("Error", new ErrorViewModel(response.ErrorMessage,
                                                        response.ErrorException.ToString())));
            }

            ViewBag.PaginationMetadata = JsonConvert.DeserializeObject(response.Headers
                                                                       .FirstOrDefault(h => h.Name == "X-Pagination").Value.ToString());

            ViewBag.MovieGenres = new MovieGenres(parameters.MovieGenre);

            return(View(response.Data));
        }
Пример #2
0
        public async Task <IActionResult> GetReviews([FromQuery] ReviewsResourceParameters reviewsResourceParameters)
        {
            if (!_typeHelperService.TypeHasProperties <ReviewReturnModel>(
                    reviewsResourceParameters.Fields))
            {
                return(BadRequest());
            }

            var reviews = await _reviewData.GetReviews(reviewsResourceParameters);

            var returnModel = Mapper.Map <List <ReviewReturnModel> >(reviews);

            var pagedReturnModel = await PagedList <ReviewReturnModel> .Create(returnModel,
                                                                               reviewsResourceParameters.PageNumber,
                                                                               reviewsResourceParameters.PageSize);

            var previousPageLink = pagedReturnModel.HasPrevious ?
                                   CreateReviewsResourceUri(reviewsResourceParameters,
                                                            ResourceUriType.PreviousPage) : null;

            var nextPageLink = pagedReturnModel.HasNext ?
                               CreateReviewsResourceUri(reviewsResourceParameters,
                                                        ResourceUriType.NextPage) : null;

            var paginationMetadata = new
            {
                totalCount       = pagedReturnModel.TotalCount,
                pageSize         = pagedReturnModel.PageSize,
                currentPage      = pagedReturnModel.CurrentPage,
                totalPages       = pagedReturnModel.TotalPages,
                previousPageLink = previousPageLink,
                nextPageLink     = nextPageLink
            };

            Response.Headers.Add("X-Pagination",
                                 Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata));

            var links = CreateLinksForReviews(reviewsResourceParameters,
                                              pagedReturnModel.HasNext, pagedReturnModel.HasPrevious);

            var shapedReturnModel = pagedReturnModel.AsEnumerable().ShapeData(reviewsResourceParameters.Fields);

            var shapedReturnModelWithLinks = shapedReturnModel.Select(review =>
            {
                var reviewAsDictionary = review as IDictionary <string, object>;
                var reviewLinks        = CreateLinksForReview(
                    (int)reviewAsDictionary["Id"], reviewsResourceParameters.Fields);
                reviewAsDictionary.Add("links", reviewLinks);

                return(reviewAsDictionary);
            });

            return(Ok(shapedReturnModelWithLinks));
        }
Пример #3
0
        public async Task <IActionResult> GetMovie(int id)
        {
            var movie = await _movieData.GetMovie(id);

            if (movie == null)
            {
                return(NotFound());
            }

            var returnModel = Mapper.Map <MovieReturnModel>(movie);

            var movieReviews = await _reviewData.GetReviews(new ReviewsResourceParameters { MovieId = id });

            returnModel.MovieReviews = Mapper.Map <List <ReviewReturnModel> >(movieReviews);

            return(Ok(returnModel));
        }