using Application.Abstractions.Data; using Application.Abstractions.Messaging; using Domain.Entities.Feed; using Microsoft.EntityFrameworkCore; using SharedKernel.Results; namespace Application.Features.Api.Feed.ToggleCommentLike; public sealed class Handler(IAppDbContext db) : ICommandHandler> { public async Task> 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", "댓글을 찾을 수 없습니다.")); } var existing = await db.FeedCommentLike.FirstOrDefaultAsync(l => l.FeedCommentID == request.CommentID && l.MemberID == request.MemberID, ct); bool hasLike; if (existing is null) { await db.FeedCommentLike.AddAsync(new FeedCommentLike { FeedPostID = comment.FeedPostID, FeedCommentID = request.CommentID, MemberID = request.MemberID }, ct); comment.Likes++; comment.Score = comment.Likes; hasLike = true; } else { db.FeedCommentLike.Remove(existing); comment.Likes = comment.Likes > 0 ? comment.Likes - 1 : 0; comment.Score = comment.Likes; hasLike = false; } await db.SaveChangesAsync(ct); return new Response(hasLike, comment.Likes); } }