Ejemplo n.º 1
0
        public async Task <JsonNetResult> ByVideo(GetVideoCommentsViewModel model)
        {
            VideoComments result = await _comments.GetVideoComments(new GetVideoComments
            {
                VideoId              = model.VideoId,
                PageSize             = model.PageSize,
                FirstCommentIdOnPage = model.FirstCommentIdOnPage
            });

            // For the ViewModel, we also want to include the information about a user who made the comments on the video, so
            // get the user profile 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)
            IEnumerable <UserProfile> userProfiles = await _userManagement.GetUserProfiles(result.Comments.Select(c => c.UserId).ToHashSet());

            var returnModel = new VideoCommentsViewModel
            {
                VideoId  = result.VideoId,
                Comments = result.Comments.Join(userProfiles, c => c.UserId, up => up.UserId, (c, up) => new VideoCommentViewModel
                {
                    CommentId            = c.CommentId,
                    Comment              = c.Comment,
                    CommentTimestamp     = c.CommentTimestamp,
                    UserProfileUrl       = Url.Action("Info", "Account", new { userId = c.UserId }),
                    UserFirstName        = up.FirstName,
                    UserLastName         = up.LastName,
                    UserGravatarImageUrl = GravatarHasher.GetImageUrlForEmailAddress(up.EmailAddress)
                }).ToList()
            };

            return(JsonSuccess(returnModel));
        }
Ejemplo n.º 2
0
        public async Task <JsonNetResult> Add(AddCommentViewModel model)
        {
            // Shouldn't throw because of the Authorize attribute
            Guid userId = User.GetCurrentUserId().Value;

            var commentTimestamp = DateTimeOffset.UtcNow;

            // Add the new comment
            var commentOnVideo = new CommentOnVideo
            {
                UserId    = userId,
                VideoId   = model.VideoId,
                CommentId = TimeUuid.NewId(commentTimestamp),
                Comment   = model.Comment
            };

            await _comments.CommentOnVideo(commentOnVideo);

            // Lookup the current user's information to include in the return data
            UserProfile userInfo = await _userManagement.GetUserProfile(userId);

            return(JsonSuccess(new VideoCommentViewModel
            {
                CommentId = commentOnVideo.CommentId,
                Comment = commentOnVideo.Comment,
                CommentTimestamp = commentTimestamp,
                UserProfileUrl = Url.Action("Info", "Account", new { userId }),
                UserFirstName = userInfo.FirstName,
                UserLastName = userInfo.LastName,
                UserGravatarImageUrl = GravatarHasher.GetImageUrlForEmailAddress(userInfo.EmailAddress)
            }));
        }
        /// <summary>
        /// Creates a ViewModel instance from the common user profile model returned from the data layer.
        /// </summary>
        public static UserProfileViewModel FromDataModel(UserProfile data)
        {
            if (data == null)
            {
                return(null);
            }

            return(new UserProfileViewModel
            {
                UserId = data.UserId,
                EmailAddress = data.EmailAddress,
                FirstName = data.FirstName,
                LastName = data.LastName,
                ProfileImageUrl = GravatarHasher.GetImageUrlForEmailAddress(data.EmailAddress)
            });
        }