Handler.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Microsoft.EntityFrameworkCore;
  4. using SharedKernel.Results;
  5. namespace Application.Features.Api.Member.Follow.IsFollowing;
  6. public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Result<Response>>
  7. {
  8. public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
  9. {
  10. if (request.FolloweeSIDs.Count == 0)
  11. {
  12. return Result.Success(new Response { Map = new Dictionary<string, bool>() });
  13. }
  14. var sids = request.FolloweeSIDs.Distinct().ToArray();
  15. var targets = await db.Member.AsNoTracking().Where(x => sids.Contains(x.SID)).Select(x => new { x.ID, x.SID }).ToListAsync(ct);
  16. if (targets.Count == 0)
  17. {
  18. return Result.Success(new Response { Map = sids.ToDictionary(s => s, _ => false) });
  19. }
  20. var targetIDs = targets.Select(x => x.ID).ToArray();
  21. var followingIDs = await db.MemberFollow.AsNoTracking()
  22. .Where(x => x.FollowerMemberID == request.FollowerMemberID && targetIDs.Contains(x.FolloweeMemberID))
  23. .Select(x => x.FolloweeMemberID)
  24. .ToListAsync(ct);
  25. var followingSet = followingIDs.ToHashSet();
  26. var sidToId = targets.ToDictionary(x => x.SID, x => x.ID);
  27. var map = sids.ToDictionary(
  28. sid => sid,
  29. sid => sidToId.TryGetValue(sid, out var id) && followingSet.Contains(id)
  30. );
  31. return Result.Success(new Response { Map = map });
  32. }
  33. }