💄 Optimize realm discovery
This commit is contained in:
@ -9,9 +9,16 @@ namespace DysonNetwork.Sphere.Discovery;
|
||||
|
||||
public class DiscoveryService(AppDatabase appDatabase)
|
||||
{
|
||||
public Task<List<Realm.Realm>> GetPublicRealmsAsync(string? query, List<string>? tags)
|
||||
public Task<List<Realm.Realm>> GetPublicRealmsAsync(string? query,
|
||||
List<string>? tags,
|
||||
int take = 10,
|
||||
int offset = 0,
|
||||
bool randomizer = false
|
||||
)
|
||||
{
|
||||
var realmsQuery = appDatabase.Realms
|
||||
.Take(take)
|
||||
.Skip(offset)
|
||||
.Where(r => r.IsPublic);
|
||||
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
@ -19,11 +26,13 @@ public class DiscoveryService(AppDatabase appDatabase)
|
||||
realmsQuery = realmsQuery.Where(r => r.Name.Contains(query) || r.Description.Contains(query));
|
||||
}
|
||||
|
||||
if (tags != null && tags.Count > 0)
|
||||
{
|
||||
if (tags is { Count: > 0 })
|
||||
realmsQuery = realmsQuery.Where(r => r.RealmTags.Any(rt => tags.Contains(rt.Tag.Name)));
|
||||
}
|
||||
if (randomizer)
|
||||
realmsQuery = realmsQuery.OrderBy(r => EF.Functions.Random());
|
||||
else
|
||||
realmsQuery = realmsQuery.OrderByDescending(r => r.CreatedAt);
|
||||
|
||||
return realmsQuery.ToListAsync();
|
||||
return realmsQuery.Skip(offset).Take(take).ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user