예제 #1
0
        public IHttpActionResult Get(int postId)
        {
            var userId = this.User.Identity.GetUserId();

            if (userId == null)
            {
                return(this.BadRequest("Invalid session token."));
            }

            var user = this.SocialNetworkData.Users.GetById(userId);

            var existingPost = this.SocialNetworkData.Posts.All()
                               .FirstOrDefault(p => p.Id == postId);

            if (existingPost == null)
            {
                return(this.NotFound());
            }

            var comments = existingPost.Comments
                           .OrderByDescending(c => c.Date)
                           .Select(c => CommentViewModel.Create(c, user));

            return(this.Ok(comments));
        }
예제 #2
0
        public ActionResult AddComment(CommentDto commentDto)
        {
            var list = new List <CommentViewModel>();

            try
            {
                var userId  = User.Identity.GetUserId();
                var comment = _repository.PostRepository.AddComment(commentDto.PostId, userId, commentDto.Text);
                list.Add(CommentViewModel.Create(comment, userId));
                _repository.Complete();
                list[0].Id = comment.Id;
                CommentsHub.Notify(commentDto.PostId, comment.Id);
            }
            catch (DbEntityValidationException exception)
            {
                list[0].Error = DbEntityValidationExceptionHandler.GetExceptionMessage(exception);
                return(PartialView("_CommentsList", list));
            }
            catch (Exception exception)
            {
                list[0].Error = exception.Message;
                return(PartialView("_CommentsList", list));
            }

            return(PartialView("_CommentsList", list));
        }
예제 #3
0
        public async Task <IActionResult> Comment(Guid id, [FromBody] Comment comment)
        {
            var modItem = _ctx.ModerationItems.FirstOrDefault(x => x.Id == id);

            if (modItem == null)
            {
                return(NoContent());
            }

            var regex = new Regex(@"\B(?<tag>@[a-zA-Z0-9-_]+)");

            comment.HtmlContent = regex.Matches(comment.Content)
                                  .Aggregate(comment.Content,
                                             (content, match) =>
            {
                var tag = match.Groups["tag"].Value;
                return(content
                       .Replace(tag, $"<a href=\"{tag}-user-link\">{tag}</a>"));
            });

            modItem.Comments.Add(comment);
            await _ctx.SaveChangesAsync();

            return(Ok(CommentViewModel.Create(comment)));
        }
예제 #4
0
        public static Expression <Func <Photo, PhotoViewModel> > Create(ApplicationUser currentUser)
        {
            string currentUserId = currentUser.Id;

            return(photo => new PhotoViewModel
            {
                Id = photo.Id,
                Image = photo.Source,
                Description = photo.Description,
                PostedOn = photo.PostedOn,
                LikesCount = photo.Likes.Count,
                Liked = photo.Likes.Any(l => l.UserId == currentUserId),
                CommentsCount = photo.Comments.Count,
                Owner = new UserViewModelMinified
                {
                    Id = photo.PhotoOwner.Id,
                    Name = photo.PhotoOwner.Name,
                    Username = photo.PhotoOwner.UserName,
                    IsFriend = photo.PhotoOwner.Friends.Any(f => f.Id == currentUserId),
                    Gender = photo.PhotoOwner.Gender,
                    ProfileImageData = photo.PhotoOwner.ProfileImageData,
                },
                Comments = photo.Comments
                           .AsQueryable()
                           .Select(CommentViewModel.Create(currentUser))
            });
        }
예제 #5
0
        public ActionResult GetComment(int commentId)
        {
            var user = _repository.ApplicationUserRepository.GetUserById(User.Identity.GetUserId());

            if (user == null)
            {
                return(HttpNotFound());
            }

            return(PartialView("_Comment", CommentViewModel.Create(_repository.PostRepository.GetComment(commentId), user.Id)));
        }
예제 #6
0
        public ActionResult GetComments(int postId)
        {
            var user = _repository.ApplicationUserRepository.GetUserById(User.Identity.GetUserId());

            if (user == null)
            {
                return(HttpNotFound());
            }

            var comments = _repository.PostRepository.GetComments(postId);
            var list     = comments.Select(c => CommentViewModel.Create(c, user.Id)).ToList();

            return(PartialView("_CommentsList", list));
        }
        public async Task <IActionResult> Create(
            [FromBody] CommentForm commentForm,
            [FromServices] CommentCreationContext commentCreationContext)
        {
            try
            {
                var comment = await commentCreationContext
                              .Setup(UserId)
                              .CreateAsync(commentForm);

                return(Ok(CommentViewModel.Create(comment)));
            }
            catch (CommentCreationContext.ParentNotFoundException e)
            {
                return(BadRequest(e.Message));
            }
        }
예제 #8
0
        public IHttpActionResult Post(int postId, PostCommentBindingModel commentBindingModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var post = this.SocialNetworkData.Posts.All()
                       .FirstOrDefault(p => p.Id == postId);

            if (post == null)
            {
                return(this.NotFound());
            }

            var userId = this.User.Identity.GetUserId();

            if (userId == null)
            {
                return(this.BadRequest("Invalid session token."));
            }

            var user = this.SocialNetworkData.Users.All()
                       .First(u => u.Id == userId);

            if (!this.HasAccessToPost(user, post))
            {
                return(this.BadRequest("Post must be by friend or on friend's wall."));
            }

            var comment = new Comment()
            {
                AuthorId = userId,
                PostId   = postId,
                Content  = commentBindingModel.CommentContent,
                Date     = DateTime.Now
            };

            this.SocialNetworkData.Comments.Add(comment);
            this.SocialNetworkData.Comments.SaveChanges();

            return(this.Ok(CommentViewModel.Create(comment, user)));
        }
 public static PostViewModel Create(Post p, ApplicationUser currentUser)
 {
     return(new PostViewModel()
     {
         Id = p.Id,
         Author = UserViewModelPreview.Create(p.Author, currentUser),
         WallOwner = UserViewModelPreview.Create(p.WallOwner, currentUser),
         PostContent = p.Content,
         Date = p.Date,
         LikesCount = p.Likes.Count,
         Liked = p.Likes
                 .Any(l => l.UserId == currentUser.Id),
         TotalCommentsCount = p.Comments.Count,
         Comments = p.Comments
                    .Reverse()
                    .Take(3)
                    .Select(c => CommentViewModel.Create(c, currentUser))
     });
 }
        public static AddPostViewModel ConvertTo(Post post, ApplicationUser currentUser)
        {
            AddPostViewModel postViewModel = new AddPostViewModel
            {
                Id     = post.Id,
                Author = new UserViewModelMinified
                {
                    Id               = post.AuthorId,
                    Name             = post.Author.Name,
                    Username         = post.Author.UserName,
                    IsFriend         = post.Author.Friends.Any(f => f.Id == currentUser.Id),
                    Gender           = post.Author.Gender,
                    ProfileImageData = post.Author.ProfileImageData,
                },
                WallOwner = new UserViewModelMinified
                {
                    Id               = post.WallOwnerId,
                    Name             = post.WallOwner.Name,
                    Username         = post.WallOwner.UserName,
                    IsFriend         = post.WallOwner.Friends.Any(f => f.Id == currentUser.Id),
                    Gender           = post.WallOwner.Gender,
                    ProfileImageData = post.WallOwner.ProfileImageData,
                },
                PostContent        = post.Content,
                PostedOn           = post.PostedOn,
                LikesCount         = post.Likes.Count,
                Liked              = post.Likes.Any(l => l.UserId == currentUser.Id),
                TotalCommentsCount = post.Comments.Count,
                Comments           = post.Comments
                                     .Reverse()
                                     .Take(4)
                                     .AsQueryable()
                                     .Select(CommentViewModel.Create(currentUser))
            };

            return(postViewModel);
        }
        public static Expression <Func <GroupPost, GroupPostViewModel> > Create(ApplicationUser currentUser)
        {
            string currentUserId = currentUser.Id;

            return(post => new GroupPostViewModel
            {
                Id = post.Id,
                Author = new UserViewModelMinified
                {
                    Id = post.AuthorId,
                    Name = post.Author.Name,
                    Username = post.Author.UserName,
                    IsFriend = post.Author.Friends.Any(f => f.Id == currentUserId),
                    Gender = post.Author.Gender,
                    ProfileImageData = post.Author.ProfileImageData,
                },
                Group = new GroupViewModelMinified
                {
                    Id = post.GroupId,
                    Name = post.Group.Name,
                    CoverImageData = post.Group.CoverImageData,
                    CreatedOn = post.Group.CreatedOn,
                    Description = post.Group.Description,
                },
                PostContent = post.Content,
                Date = post.PostedOn,
                LikesCount = post.Likes.Count,
                Liked = post.Likes.Any(l => l.UserId == currentUserId),
                TotalCommentsCount = post.Comments.Count,
                Comments = post.Comments
                           .OrderByDescending(c => c.PostedOn)
                           .Take(4)
                           .AsQueryable()
                           .Select(CommentViewModel.Create(currentUser))
            });
        }