✨ Realm tags and discovery
This commit is contained in:
17
DysonNetwork.Sphere/Discovery/DiscoveryController.cs
Normal file
17
DysonNetwork.Sphere/Discovery/DiscoveryController.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using DysonNetwork.Sphere.Realm;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DysonNetwork.Sphere.Discovery;
|
||||
|
||||
[ApiController]
|
||||
[Route("discovery")]
|
||||
public class DiscoveryController(DiscoveryService discoveryService) : ControllerBase
|
||||
{
|
||||
[HttpGet("realms")]
|
||||
public Task<List<Realm.Realm>> GetPublicRealms([FromQuery] string? query, [FromQuery] List<string>? tags)
|
||||
{
|
||||
return discoveryService.GetPublicRealmsAsync(query, tags);
|
||||
}
|
||||
}
|
29
DysonNetwork.Sphere/Discovery/DiscoveryService.cs
Normal file
29
DysonNetwork.Sphere/Discovery/DiscoveryService.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using DysonNetwork.Sphere;
|
||||
using DysonNetwork.Sphere.Realm;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DysonNetwork.Sphere.Discovery;
|
||||
|
||||
public class DiscoveryService(AppDatabase appDatabase)
|
||||
{
|
||||
public Task<List<Realm.Realm>> GetPublicRealmsAsync(string? query, List<string>? tags)
|
||||
{
|
||||
var realmsQuery = appDatabase.Realms
|
||||
.Where(r => r.IsPublic);
|
||||
|
||||
if (!string.IsNullOrEmpty(query))
|
||||
{
|
||||
realmsQuery = realmsQuery.Where(r => r.Name.Contains(query) || r.Description.Contains(query));
|
||||
}
|
||||
|
||||
if (tags != null && tags.Count > 0)
|
||||
{
|
||||
realmsQuery = realmsQuery.Where(r => r.RealmTags.Any(rt => tags.Contains(rt.Tag.Name)));
|
||||
}
|
||||
|
||||
return realmsQuery.ToListAsync();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user