update getpairedclients to use only one query instead of one per entry

This commit is contained in:
Stanley Dimant
2022-08-19 12:18:16 +02:00
parent 7abe4f9f2e
commit 78543d0fc2

View File

@@ -93,21 +93,36 @@ namespace MareSynchronosServer.Hubs
public async Task<List<ClientPairDto>> GetPairedClients() public async Task<List<ClientPairDto>> GetPairedClients()
{ {
string userid = AuthenticatedUserId; string userid = AuthenticatedUserId;
var pairs = await _dbContext.ClientPairs.AsNoTracking() var query =
.Include(u => u.OtherUser) from userToOther in _dbContext.ClientPairs
.Include(u => u.User) join otherToUser in _dbContext.ClientPairs
.Where(w => w.User.UID == userid) on new
.ToListAsync();
return pairs.Select(w =>
{ {
var otherEntry = OppositeEntry(w.OtherUser.UID); user = userToOther.UserUID,
return new ClientPairDto other = userToOther.OtherUserUID
} equals new
{ {
IsPaused = w.IsPaused, user = otherToUser.OtherUserUID,
OtherUID = w.OtherUser.UID, other = otherToUser.UserUID
IsSynced = otherEntry != null, } into leftJoin
IsPausedFromOthers = otherEntry?.IsPaused ?? false, from otherEntry in leftJoin.DefaultIfEmpty()
where
userToOther.UserUID == userid
select new
{
userToOther.IsPaused,
OtherIsPaused = otherEntry != null && otherEntry.IsPaused,
userToOther.OtherUserUID,
IsSynced = otherEntry != null
}; };
return (await query.ToListAsync()).Select(f => new ClientPairDto()
{
IsPaused = f.IsPaused,
OtherUID = f.OtherUserUID,
IsSynced = f.IsSynced,
IsPausedFromOthers = f.OtherIsPaused
}).ToList(); }).ToList();
} }