mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-12 21:02:27 +00:00
Refactor Database into DatabaseContext Moved into separate folder so it actually has a namespace instead sitting in the root
73 lines
2.7 KiB
C#
73 lines
2.7 KiB
C#
#nullable enable
|
|
using LBPUnion.ProjectLighthouse.Database;
|
|
using LBPUnion.ProjectLighthouse.Extensions;
|
|
using LBPUnion.ProjectLighthouse.Serialization;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
|
|
using LBPUnion.ProjectLighthouse.Types.Levels;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Slots;
|
|
|
|
[ApiController]
|
|
[Authorize]
|
|
[Route("LITTLEBIGPLANETPS3_XML/slots")]
|
|
[Produces("text/xml")]
|
|
public class SearchController : ControllerBase
|
|
{
|
|
private readonly DatabaseContext database;
|
|
public SearchController(DatabaseContext database)
|
|
{
|
|
this.database = database;
|
|
}
|
|
|
|
[HttpGet("searchLBP3")]
|
|
public Task<IActionResult> SearchSlotsLBP3([FromQuery] int pageSize, [FromQuery] int pageStart, [FromQuery] string textFilter)
|
|
=> this.SearchSlots(textFilter, pageSize, pageStart, "results");
|
|
|
|
[HttpGet("search")]
|
|
public async Task<IActionResult> SearchSlots(
|
|
[FromQuery] string query,
|
|
[FromQuery] int pageSize,
|
|
[FromQuery] int pageStart,
|
|
string? keyName = "slots"
|
|
)
|
|
{
|
|
GameToken token = this.GetToken();
|
|
|
|
if (pageSize <= 0) return this.BadRequest();
|
|
|
|
if (string.IsNullOrWhiteSpace(query)) return this.BadRequest();
|
|
|
|
query = query.ToLower();
|
|
|
|
string[] keywords = query.Split(" ");
|
|
|
|
IQueryable<Slot> dbQuery = this.database.Slots.ByGameVersion(token.GameVersion, false, true)
|
|
.Where(s => s.Type == SlotType.User)
|
|
.OrderBy(s => !s.TeamPick)
|
|
.ThenByDescending(s => s.FirstUploaded)
|
|
.Where(s => s.SlotId >= 0); // dumb query to conv into IQueryable
|
|
|
|
// ReSharper disable once LoopCanBeConvertedToQuery
|
|
foreach (string keyword in keywords)
|
|
dbQuery = dbQuery.Where
|
|
(
|
|
s => s.Name.ToLower().Contains(keyword) ||
|
|
s.Description.ToLower().Contains(keyword) ||
|
|
s.Creator!.Username.ToLower().Contains(keyword) ||
|
|
s.SlotId.ToString().Equals(keyword)
|
|
);
|
|
|
|
List<Slot> slots = await dbQuery.Skip(Math.Max(0, pageStart - 1)).Take(Math.Min(pageSize, 30)).ToListAsync();
|
|
|
|
string response = slots.Aggregate("", (current, slot) => current + slot.Serialize(token.GameVersion));
|
|
|
|
return this.Ok(LbpSerializer.TaggedStringElement(keyName, response, "total", dbQuery.Count()));
|
|
}
|
|
|
|
// /LITTLEBIGPLANETPS3_XML?pageStart=1&pageSize=10&resultTypes[]=slot&resultTypes[]=playlist&resultTypes[]=user&adventure=dontCare&textFilter=qwer
|
|
|
|
}
|