Example #1
0
        public async Task <object> UpdatePost(int id, PostForUpdate postForEdit)
        {
            var userLikes = await _context.Likes
                            .Where(like => like.UserId == postForEdit.UserId)
                            .Select(like => like.PostId)
                            .ToListAsync();

            var userComments = await _context.Comments
                               .Where(comment => comment.UserId == postForEdit.UserId)
                               .Select(comment => comment.PostId)
                               .ToListAsync();

            // Get the post content in DB
            var existingPost = await _context.Posts
                               .Where(p => p.Id == id)
                               .Include(p => p.Likes)
                               .Include(p => p.User)
                               .Include(p => p.Comments).ThenInclude(comment => comment.User).ThenInclude(user => user.Posts)
                               .FirstOrDefaultAsync();

            if (existingPost == null)
            {
                return(new { status = "Failure" });
            }
            else
            {
                existingPost.content = postForEdit.content;
                existingPost.hidden  = postForEdit.hidden;
                existingPost.edited  = postForEdit.edited;

                await _context.SaveChangesAsync();

                return(new
                {
                    Id = existingPost.Id,
                    content = existingPost.content,
                    imageURL = existingPost.imageURL,
                    edited = existingPost.edited,
                    createdAt = existingPost.createdAt,
                    UserId = existingPost.UserId,
                    User = new
                    {
                        Id = existingPost.User.Id,
                        name = existingPost.User.name,
                        email = existingPost.User.email,
                        username = existingPost.User.username,
                        avatarBackground = existingPost.User.avatarBackground,
                        createdAt = existingPost.User.createdAt
                    },
                    likeData = new
                    {
                        quanity = existingPost.Likes.Count(),
                        likedByUser = CheckUserPostLikeArray(userLikes, existingPost.Id),
                        likeId = GetLikeId(existingPost.Likes, existingPost.Id)
                    },
                    commentData = new
                    {
                        quanity = existingPost.Comments.Count(),
                        commentedByUser = CheckUserPostCommentArray(userComments, existingPost.Id),
                        comments = FormatCommentCollection(existingPost.Comments)
                    }
                });
            }
        }
Example #2
0
        public async Task <IActionResult> UpdatePost(PostForUpdate model, CancellationToken ct)
        {
            await _mediator.Send(new UpdatePostRequest { PostToUpdate = _mapper.Map <Post>(model) }, ct);

            return(Ok());
        }
Example #3
0
        public async Task <object> UpdatePost(int id, [FromBody] PostForUpdate postForEdit)
        {
            var update = await _postService.UpdatePost(id, postForEdit);

            return(Ok(update));
        }