From 90c572a5e6cd311ee25d1d431763c56e5da3cda4 Mon Sep 17 00:00:00 2001 From: jvyden Date: Tue, 19 Oct 2021 19:01:41 -0400 Subject: [PATCH] Implement searching slots --- .../Controllers/SearchController.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ProjectLighthouse/Controllers/SearchController.cs diff --git a/ProjectLighthouse/Controllers/SearchController.cs b/ProjectLighthouse/Controllers/SearchController.cs new file mode 100644 index 00000000..002bb731 --- /dev/null +++ b/ProjectLighthouse/Controllers/SearchController.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using ProjectLighthouse.Serialization; +using ProjectLighthouse.Types; + +namespace ProjectLighthouse.Controllers { + [ApiController] + [Route("LITTLEBIGPLANETPS3_XML/")] + [Produces("text/xml")] + public class SearchController : ControllerBase { + private readonly Database database; + public SearchController(Database database) { + this.database = database; + } + + [HttpGet("slots/search")] + public async Task SearchSlots([FromQuery] string query) { + query = query.ToLower(); + + string[] keywords = query.Split(" "); + + IQueryable dbQuery = this.database.Slots + .Include(s => s.Creator) + .Include(s => s.Location) + .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) + ); + } + + List slots = await dbQuery.ToListAsync(); + string response = slots.Aggregate("", (current, slot) => current + slot.Serialize()); + + return this.Ok(LbpSerializer.StringElement("slots", response)); + } + } +} \ No newline at end of file