public IHttpActionResult AddComment([FromUri] int id, [FromBody] AddCommentBindingModel model)
        {
            var post = this.Data.Posts.Find(id);

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

            var currentUserId = User.Identity.GetUserId();
            var currentUser = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId);
            if (currentUser == null)
            {
                return this.BadRequest("Invalid user token! Please login again!");
            }

            var newCommentToAdd = new Comment()
            {
                Author = currentUser,
                Post = post,
                PostedOn = DateTime.Now,
                Content = model.CommentContent
            };

            post.Comments.Add(newCommentToAdd);
            this.Data.SaveChanges();

            var newCommentViewModel = this.Data.Comments
                .Where(c => c.Id == newCommentToAdd.Id)
                .Select(CommentViewModel.Create)
                .FirstOrDefault();

            return this.Ok(newCommentViewModel);
        }
        public IHttpActionResult AddCommentToPost(int postId,
            AddPostBindingModel model)
        {
            //check if the post exists
            var post = this.Context.Posts.Find(postId);
            if (post == null)
            {
                return this.NotFound();
            }

            //if the model is empty
            if (model==null)
            {
                return this.BadRequest("No values are sent!");
            }

            //check for validations
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            //if everything is OK
            var userId = this.User.Identity.GetUserId();
            var comment = new Comment()
            {
                Content = model.Content,
                PostedOn = DateTime.Now,
                AuthorId = userId
            };
            post.Comments.Add(comment);
            this.Context.SaveChanges();

            return this.Ok();
        }
        public static CommentViewModel ConvertTo(Comment comment, ApplicationUser currentUser)
        {
            CommentViewModel commentViewModel = new CommentViewModel
            {
                Id = comment.Id,
                Author = new UserViewModelMinified
                {
                    Id = comment.AuthorId,
                    Name = comment.Author.Name,
                    Username = comment.Author.UserName,
                    IsFriend = comment.Author.Friends.Any(f => f.Id == currentUser.Id),
                    Gender = comment.Author.Gender,
                    ProfileImageData = comment.Author.ProfileImageData,
                },
                Date = comment.PostedOn,
                CommentContent = comment.Content,
                LikesCount = comment.Likes.Count,
                Liked = comment.Likes.Any(l => l.UserId == currentUser.Id),
                CommentReplies = comment.Replies
                    .OrderByDescending(cr => cr.RepliedOn)
                    .AsQueryable()
                    .Select(CommentReplyViewModel.Create(currentUser))
            };

            return commentViewModel;
        }
Ejemplo n.º 4
0
 protected void LinkButtonAddComment_Click(object sender, EventArgs e)
 {
     using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
     {
         if (!string.IsNullOrEmpty(this.TextBoxNewComment.Text))
         {
             try
             {
                 Comment comment = new Comment();
                 context.Comments.Add(comment);
                 comment.PostId = this.postId;
                 comment.DateCreated = DateTime.Now;
                 comment.UserId = User.Identity.GetUserId();
                 comment.Text = Server.HtmlEncode(this.TextBoxNewComment.Text);
                 context.SaveChanges();
                 Response.Redirect("UserPost.aspx?postId=" + comment.PostId, false);
             }
             catch (Exception ex)
             {
                 ErrorSuccessNotifier.AddErrorMessage("Comment failed! Server error!");
             }
         }
         else
         {
             ErrorSuccessNotifier.AddErrorMessage("Cannot post empty comment!");
         }
     }
 }
        public async Task<HttpResponseMessage> AddPostComment([FromUri] int postId, 
            [FromBody] AddCommentBindingModel model)
        {
            if (model == null)
            {
                return await this.BadRequest("Comment cannot be empty.").ExecuteAsync(new CancellationToken());
            }

            if (!ModelState.IsValid)
            {
                return await this.BadRequest(this.ModelState).ExecuteAsync(new CancellationToken());
            }

            var post = this.Data.GroupPosts.Find(postId);

            if (post == null)
            {
                return await this.NotFound().ExecuteAsync(new CancellationToken());
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId);
            if (currentUser == null)
            {
                return await this.BadRequest("Invalid user token! Please login again!")
                    .ExecuteAsync(new CancellationToken());
            }

            if (!post.Owner.Members.Contains(currentUser) && currentUser != post.Owner.Owner)
            {
                return await this.BadRequest("Not allowed. You must be a member of the group.")
                    .ExecuteAsync(new CancellationToken());
            }

            var newComment = new Comment()
            {
                Content = model.CommentContent,
                Author = currentUser,
                PostedOn = DateTime.Now
            };

            post.Comments.Add(newComment);
            this.Data.SaveChanges();

            var commentPreview = this.Data.Comments
                .Where(p => p.Id == newComment.Id)
                .Select(AddCommentViewModel.Create)
                .FirstOrDefault();
            commentPreview.PostId = post.Id;

            return await this.Ok(commentPreview).ExecuteAsync(new CancellationToken());
        }
 public static CommentViewModel Create(Comment c, ApplicationUser currentUser)
 {
     return new CommentViewModel()
     {
         Id = c.Id,
         Author = UserViewModelMinified.Create(c.Author),
         LikesCount = c.Likes.Count,
         CommentContent = c.Content,
         Date = c.Date,
         Liked = c.Likes
             .Any(l => l.UserId == currentUser.Id)
     };
 }
 public static object CreatePreview(ApplicationUser user, Comment comment)
 {
     return new
     {
         Id = comment.Id,
         AuthorId = comment.Author.Id,
         AuthorUsername = comment.Author.UserName,
         AuthorProfileImage = comment.Author.ProfilePicture,
         CommentContent = comment.Content,
         Date = comment.PostedOn,
         LikesCount = comment.Likes.Count,
         Liked = comment.Likes.Any(l => l.Author.Id == user.Id)
     };
 }
        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 IHttpActionResult AddNewComment(AddCommentBindingModel bindingModel, int postId)
        {
            if (bindingModel == null)
            {
                return this.BadRequest("Invalid data!");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var currentUserId = this.UserIdProvider.GetUserId();
            var currentUser = this.Data.Users.Find(currentUserId);

            var post = this.Data.Posts.Find(postId);
            if (post == null)
            {
                return this.NotFound();
            }

            if (this.HasAuthorization(currentUser, post))
            {
                return this.BadRequest("Unable to post comment. You can comment only on your friend's posts or post made on their wall.");
            }

            Comment comment = new Comment
            {
                Content = bindingModel.CommentContent,
                PostedOn = DateTime.Now,
                PostId = postId,
                AuthorId = currentUserId
            };

            post.Comments.Add(comment);
            this.Data.SaveChanges();

            CommentViewModel commentViewModel = CommentViewModel.ConvertTo(comment, currentUser);

            return this.Ok(commentViewModel);
        }