using Application.Abstractions.Data; using Application.Abstractions.Messaging; using Domain.Entities.Forum.ValueObject; using Microsoft.EntityFrameworkCore; namespace Application.Features.Admin.Forum.PostReaction.Delete; public sealed class Handler(IAppDbContext db) : ICommandHandler { public async Task Handle(Command request, CancellationToken ct) { if (request.IDs is null || request.IDs.Length == 0) { return; } var reactions = await db.PostReaction.Where(c => request.IDs.Contains(c.ID)).ToListAsync(ct); var postIDs = reactions.Select(c => c.PostID).Distinct().ToList(); var posts = await db.Post.Where(c => postIDs.Contains(c.ID)).ToListAsync(ct); foreach (var post in posts) { var likes = reactions.Count(c => c.PostID == post.ID && c.Reaction == Reaction.Like); var dislikes = reactions.Count(c => c.PostID == post.ID && c.Reaction == Reaction.Dislike); post.Likes -= likes; post.Dislikes -= dislikes; post.UpdatedAt = DateTime.UtcNow; } db.PostReaction.RemoveRange(reactions); await db.SaveChangesAsync(ct); } }