Beispiel #1
0
        public async Task <object> UpdateComment(int id, CommentForUpdate comment)
        {
            // Get the post content in DB
            var existingComment = await _context.Comments
                                  .Where(c => c.Id == id)
                                  .Include(c => c.User)
                                  .ThenInclude(u => u.Posts)
                                  .FirstOrDefaultAsync();

            if (existingComment == null)
            {
                return(new { status = "Failure" });
            }
            else
            {
                existingComment.content = comment.content;
                existingComment.edited  = true;

                await _context.SaveChangesAsync();

                return(new
                {
                    Id = existingComment.Id,
                    content = existingComment.content,
                    edited = existingComment.edited,
                    createdAt = existingComment.createdAt,
                    UserId = existingComment.UserId,
                    PostId = existingComment.PostId,
                    User = new
                    {
                        Id = existingComment.User.Id,
                        name = existingComment.User.name,
                        username = existingComment.User.username,
                        avatarBackground = existingComment.User.avatarBackground,
                        postCount = existingComment.User.Posts.Count()
                    }
                });
            }
        }
        public async Task <object> UpdateComment(int id, [FromBody] CommentForUpdate commentForUpdate)
        {
            var update = await _commentService.UpdateComment(id, commentForUpdate);

            return(Ok(update));
        }