コード例 #1
0
        public async Task<JsonNetResult> ByUser(GetUserCommentsViewModel model)
        {
            // If no user was specified, default to the current logged in user
            Guid? userId = model.UserId ?? User.GetCurrentUserId();
            if (userId == null)
            {
                ModelState.AddModelError(string.Empty, "No user specified and no user currently logged in.");
                return JsonFailure();
            }

            // Get a page of comments for the user, then look up video details for those videos
            UserComments result = await _comments.GetUserComments(new GetUserComments
            {
                UserId = userId.Value,
                PageSize = model.PageSize,
                FirstCommentIdOnPage = model.FirstCommentIdOnPage
            });

            // For the ViewModel, we want to add information about the video to each comment as well, so get the video preview 
            // information for the comments and then use a LINQ to objects Join to merge the two together (this should be OK since
            // the dataset should be small since we're doing a page at a time)
            IEnumerable<VideoPreview> videoPreviews = await _videoCatalog.GetVideoPreviews(result.Comments.Select(c => c.VideoId).ToHashSet());
            
            var returnModel = new UserCommentsViewModel
            {
                UserId = result.UserId,
                Comments = result.Comments.Join(videoPreviews, c => c.VideoId, vp => vp.VideoId, (c, vp) => new UserCommentViewModel
                {
                    CommentId = c.CommentId,
                    Comment = c.Comment,
                    CommentTimestamp = c.CommentTimestamp,
                    VideoViewUrl = Url.Action("View", "Videos", new { videoId = c.VideoId }),
                    VideoName = vp.Name,
                    VideoPreviewImageLocation = vp.PreviewImageLocation
                }).ToList()
            };

            return JsonSuccess(returnModel);
        }