Esempio n. 1
0
        public async Task <JsonNetResult> GetRatings(GetRatingsViewModel model)
        {
            // We definitely want the overall rating info, so start there
            Task <VideoRating> ratingTask = _ratings.GetRating(model.VideoId);

            // If a user is logged in, we also want their rating
            Guid?userId = User.GetCurrentUserId();
            Task <UserVideoRating> userRatingTask = null;

            if (userId.HasValue)
            {
                userRatingTask = _ratings.GetRatingFromUser(model.VideoId, userId.Value);
            }

            // Await data appropriately
            VideoRating     ratingData = await ratingTask;
            UserVideoRating userRating = null;

            if (userRatingTask != null)
            {
                userRating = await userRatingTask;
            }

            return(JsonSuccess(new RatingsViewModel
            {
                VideoId = ratingData.VideoId,
                CurrentUserLoggedIn = userId.HasValue,
                CurrentUserRating = userRating == null ? 0 : userRating.Rating,
                RatingsCount = ratingData.RatingsCount,
                RatingsSum = ratingData.RatingsTotal
            }));
        }