コード例 #1
0
        public IActionResult ReviewsListApi(ReviewSearchModel reviewSearchModel)
        {
            //check if the product is valid
            var product = _productService.Get(reviewSearchModel.ProductId);

            if (!product.IsPublic(CurrentStore.Id))
            {
                return(NotFound());
            }

            IList <Review> reviews;
            var            ratings = reviewSearchModel.Rating.HasValue
                ? new List <int>()
            {
                reviewSearchModel.Rating.Value
            }
                : new List <int>()
            {
                1, 2, 3, 4, 5
            };
            int totalMatches;

            if (reviewSearchModel.VerifiedPurchase)
            {
                reviews = _reviewService.Get(out totalMatches,
                                             x => x.ProductId == reviewSearchModel.ProductId && ratings.Contains(x.Rating) &&
                                             x.VerifiedPurchase == true && x.Published, page: reviewSearchModel.Page,
                                             count: reviewSearchModel.Count).ToList();
            }
            else
            {
                reviews = _reviewService.Get(out totalMatches,
                                             x => x.ProductId == reviewSearchModel.ProductId && ratings.Contains(x.Rating) && x.Published,
                                             page: reviewSearchModel.Page,
                                             count: reviewSearchModel.Count).ToList();
            }

            var reviewModels = reviews.Select(_reviewModelFactory.Create).ToList();

            var reviewsSummaryModel = new AllReviewsSummaryModel()
            {
                TotalReviews = totalMatches
            };
            //find best and worst review
            ReviewModel bestReview = null, worstReview = null;

            if (reviews.Count > 1 && reviews.Count < reviewSearchModel.Count)
            {
                bestReview  = reviewModels.OrderByDescending(x => x.Rating).First();
                worstReview = reviewModels.OrderBy(x => x.Rating).First();
                reviewsSummaryModel.FiveStarCount  = reviews.Count(x => x.Rating == 5);
                reviewsSummaryModel.FourStarCount  = reviews.Count(x => x.Rating == 4);
                reviewsSummaryModel.ThreeStarCount = reviews.Count(x => x.Rating == 3);
                reviewsSummaryModel.TwoStarCount   = reviews.Count(x => x.Rating == 2);
                reviewsSummaryModel.OneStarCount   = reviews.Count(x => x.Rating == 1);
            }
            else
            {
                if (reviews.Count > 1)
                {
                    bestReview  = _reviewModelFactory.Create(_reviewService.GetBestReview(product.Id));
                    worstReview = _reviewModelFactory.Create(_reviewService.GetWorstReview(product.Id));
                    reviewsSummaryModel.FiveStarCount  = _reviewService.Count(x => x.Rating == 5 && x.ProductId == product.Id && x.Published);
                    reviewsSummaryModel.FourStarCount  = _reviewService.Count(x => x.Rating == 4 && x.ProductId == product.Id && x.Published);
                    reviewsSummaryModel.ThreeStarCount = _reviewService.Count(x => x.Rating == 3 && x.ProductId == product.Id && x.Published);
                    reviewsSummaryModel.TwoStarCount   = _reviewService.Count(x => x.Rating == 2 && x.ProductId == product.Id && x.Published);
                    reviewsSummaryModel.OneStarCount   = _reviewService.Count(x => x.Rating == 1 && x.ProductId == product.Id && x.Published);
                }
            }

            var productModel = _productModelFactory.Create(product);

            //breadcrumbs
            //set breadcrumb nodes
            SetBreadcrumbToRoute(product.Name, RouteNames.SingleProduct,
                                 new { seName = product.SeoMeta.Slug, id = product.Id }, localize: false);
            SetBreadcrumbToUrl("Reviews", "");

            return(R.Success
                   .With("product", productModel)
                   .With("reviews", reviewModels)
                   .With("bestReview", bestReview)
                   .With("worstReview", worstReview)
                   .WithGridResponse(totalMatches, reviewSearchModel.Page, reviewSearchModel.Count)
                   .With("summary", reviewsSummaryModel).Result);
        }