| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Microsoft.EntityFrameworkCore;
- using SharedKernel.Results;
- namespace Application.Features.Api.Feed.DeleteComment;
- public sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
- {
- public async Task<Result> Handle(Command request, CancellationToken ct)
- {
- var comment = await db.FeedComment.FirstOrDefaultAsync(c => c.ID == request.CommentID && !c.IsDeleted, ct);
- if (comment is null)
- {
- return Result.Failure(Error.NotFound("Feed.CommentNotFound", "댓글을 찾을 수 없습니다."));
- }
- if (comment.MemberID != request.MemberID)
- {
- return Result.Failure(Error.Forbidden("Feed.NotOwner", "삭제 권한이 없습니다."));
- }
- comment.IsDeleted = true;
- comment.DeletedAt = DateTime.UtcNow;
- // 포스트 댓글 수 차감
- var post = await db.FeedPost.FirstOrDefaultAsync(p => p.ID == comment.FeedPostID, ct);
- if (post is not null)
- {
- post.Comments = post.Comments > 0 ? post.Comments - 1 : 0;
- }
- // 부모 댓글 Replies 차감
- if (comment.ParentID is int pid)
- {
- var parent = await db.FeedComment.FirstOrDefaultAsync(p => p.ID == pid, ct);
- if (parent is not null)
- {
- parent.Replies = parent.Replies > 0 ? parent.Replies - 1 : 0;
- }
- }
- await db.SaveChangesAsync(ct);
- return Result.Success();
- }
- }
|