public static CommentLikeViewModel Create(CommentLike commentLike) { return new CommentLikeViewModel() { CommentId = commentLike.CommentId, User = UserViewModelMinified.Create(commentLike.User) }; }
public IHttpActionResult LikeComment(int postId, int commentId) { 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(); } var comment = this.Data.Comments.Find(commentId); if (comment == null) { return this.NotFound(); } if (this.HasAuthorization(currentUser, post)) { return this.BadRequest("Unable to like that comment. You can like comments of your friends post or on their wall posts."); } if (comment.Likes.Any(c => c.UserId == currentUserId)) { return this.BadRequest("You have already like this comment."); } CommentLike commentLike = new CommentLike { CommentId = comment.Id, UserId = currentUserId }; this.Data.CommentLikes.Add(commentLike); this.Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult LikeComment([FromUri] int postId, [FromUri] int commentId) { var post = this.Data.Posts.Find(postId); if (post == null) { return this.NotFound(); } var comment = post.Comments.FirstOrDefault(c => c.Id == commentId); if (comment == 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 postCommentLikes = this.Data.CommentLikes .Where(cl => cl.Comment.Id == comment.Id); if (!comment.Author.Friends.Contains(currentUser) && !post.Owner.Friends.Contains(currentUser) && comment.Author != currentUser) { return this.BadRequest("Not allowed. Either wall-owner or author must be friends."); } if (postCommentLikes.Any(l => l.Author.Id == currentUserId)) { return this.BadRequest("Comment is already liked."); } var newLikeToAdd = new CommentLike() { Author = currentUser }; comment.Likes.Add(newLikeToAdd); this.Data.SaveChanges(); var commentLikeViewModel = this.Data.Comments.Where(c => c.Id == commentId) .Select(LikedCommentViewModel.Create); return this.Ok(commentLikeViewModel); }
public async Task<HttpResponseMessage> LikeComment([FromUri] int postId, [FromUri] int commentId) { var post = this.Data.GroupPosts.Find(postId); if (post == null) { return await this.NotFound().ExecuteAsync(new CancellationToken()); } var comment = this.Data.Comments.Find(commentId); if (comment == null) { return await this.NotFound().ExecuteAsync(new CancellationToken()); } if (!post.Comments.Contains(comment)) { 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 (comment.Likes.Any(l => l.Author == currentUser)) { return await this.BadRequest("You have already liked this comment.") .ExecuteAsync(new CancellationToken()); } if (!post.Owner.Members.Contains(currentUser) && currentUser != post.Owner.Owner) { return await this.BadRequest("Not allowed. You must be member of the group.") .ExecuteAsync(new CancellationToken()); } var like = new CommentLike() { Author = currentUser }; comment.Likes.Add(like); this.Data.SaveChanges(); var commentPreview = this.Data.Comments .Where(c => c.Id == comment.Id) .Select(CommentViewModel.Create) .FirstOrDefault(); commentPreview.Liked = true; return await this.Ok(commentPreview).ExecuteAsync(new CancellationToken()); }