using Femto.Common.Domain;
using Femto.Modules.Blog.Application.Commands.AddPostReaction;
using Microsoft.EntityFrameworkCore;

namespace Femto.Modules.Blog.Application.Commands.ClearPostReaction;

internal class ClearPostReactionCommandHandler(BlogContext context)
    : ICommandHandler<ClearPostReactionCommand>
{
    public async Task Handle(ClearPostReactionCommand request, CancellationToken cancellationToken)
    {
        var post = await context.Posts.SingleOrDefaultAsync(
            p => p.Id == request.PostId,
            cancellationToken
        );

        if (post is null)
            return;

        post.RemoveReaction(request.ReactorId, request.Emoji);
    }
}
