using Application.Abstractions.Data; using Application.Abstractions.Messaging; using Microsoft.EntityFrameworkCore; using SharedKernel.Results; namespace Application.Features.Api.Member.Follow.IsFollowing; public sealed class Handler(IAppDbContext db) : IQueryHandler> { public async Task> Handle(Query request, CancellationToken ct) { if (request.FolloweeSIDs.Count == 0) { return Result.Success(new Response { Map = new Dictionary() }); } var sids = request.FolloweeSIDs.Distinct().ToArray(); var targets = await db.Member.AsNoTracking().Where(x => sids.Contains(x.SID)).Select(x => new { x.ID, x.SID }).ToListAsync(ct); if (targets.Count == 0) { return Result.Success(new Response { Map = sids.ToDictionary(s => s, _ => false) }); } var targetIDs = targets.Select(x => x.ID).ToArray(); var followingIDs = await db.MemberFollow.AsNoTracking() .Where(x => x.FollowerMemberID == request.FollowerMemberID && targetIDs.Contains(x.FolloweeMemberID)) .Select(x => x.FolloweeMemberID) .ToListAsync(ct); var followingSet = followingIDs.ToHashSet(); var sidToId = targets.ToDictionary(x => x.SID, x => x.ID); var map = sids.ToDictionary( sid => sid, sid => sidToId.TryGetValue(sid, out var id) && followingSet.Contains(id) ); return Result.Success(new Response { Map = map }); } }