| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- 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<Query, Result<Response>>
- {
- public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
- {
- if (request.FolloweeSIDs.Count == 0)
- {
- return Result.Success(new Response { Map = new Dictionary<string, bool>() });
- }
- 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 });
- }
- }
|