Esempio n. 1
0
        public async Task <IActionResult> IndexWork(ReviewWorkRequest reviewRequest)
        {
            var reviews = await _reviewsService.GetReviewsByWorkAsync(reviewRequest);

            if (reviews == null)
            {
                return(this.ErrorObject(404, $"Work (id: {reviewRequest.WorkId}) doesn't contain reviews"));
            }

            return(this.PageObject(reviews.Values, reviews.TotalRows, reviewRequest.Limit, reviewRequest.Offset));
        }
Esempio n. 2
0
        // TODO: rework when EF7 will support GroupBy and Navigation Properties
        public async Task <ApiObject <ReviewResponse> > GetReviewsByWorkAsync(ReviewWorkRequest reviewRequest)
        {
            var reviews = await(from r in _bookContext.Reviews
                                join w in _bookContext.Works on r.WorkId equals w.Id
                                where r.WorkId == reviewRequest.WorkId
                                select new ReviewResponse
            {
                ReviewId    = r.Id,
                Text        = r.Text,
                WorkId      = r.WorkId,
                WorkName    = w.Name,
                WorkRusName = w.RusName,
                DateCreated = r.DateCreated,
                UserId      = r.UserId,
                UserName    = "******"                   // TODO: not implemented yet
            }).ToListAsync();

            // get response votes
            var reviewIds   = reviews.Select(c => c.ReviewId).ToList();
            var reviewVotes = await _bookContext.ReviewVotes
                              .Where(c => reviewIds.Contains(c.ReviewId))
                              .GroupBy(c => c.ReviewId)
                              .Select(g => new
            {
                ReviewId     = g.Key,
                ReviewRating = g.Sum(c => c.Vote)
            }).ToDictionaryAsync(c => c.ReviewId, c => c.ReviewRating);

            foreach (var review in reviews)
            {
                int ratingValue;
                reviewVotes.TryGetValue(review.ReviewId, out ratingValue);
                review.ReviewRating = ratingValue;
            }

            switch (reviewRequest.Sort)
            {
            case ReviewSort.Date:
                reviews = reviews.OrderByDescending(c => c.DateCreated).ToList();
                break;

            case ReviewSort.Rating:
                reviews = reviews.OrderBy(c => c.ReviewRating).ThenByDescending(c => c.DateCreated).ToList();
                break;
            }

            if (reviewRequest.Offset > 0)
            {
                reviews = reviews.Skip(reviewRequest.Offset).ToList();
            }

            if (reviewRequest.Limit > 0)
            {
                reviews = reviews.Take(reviewRequest.Limit).ToList();
            }

            var apiObject = new ApiObject <ReviewResponse>();

            apiObject.Values    = reviews;
            apiObject.TotalRows = apiObject.Values.Count;

            return(apiObject);
        }