コード例 #1
0
ファイル: PostsController.cs プロジェクト: FightCore/Backend
        public async Task <IActionResult> DeletePost(long id)
        {
            var userId = GetUserIdFromClaims(User);
            var post   = await _postService.GetByIdAsync(id);

            if (post.AuthorId != userId)
            {
                return(Unauthorized(new UnauthorizedErrorViewModel()));
            }

            var likes = await _likeService.FindRangeAsync(like => like.PostId == id);

            foreach (var like in likes)
            {
                _likeService.Remove(like);
            }

            _postService.Remove(post);
            await _context.SaveChangesAsync();

            await _cachingService.RemoveAsync($"{nameof(Post)}{post.Id}");

            return(Ok());
        }