| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 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<Command, Result<Response>>
- {
- public async Task<Result<Response>> 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<Response>(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);
- }
- }
|