From f941befc7e73bc767a5b3582295def0639fbdac0 Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 16 Oct 2021 17:19:25 -0400 Subject: [PATCH 1/5] Fix /s/user/{id} endpoint --- ProjectLighthouse/Controllers/SlotsController.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ProjectLighthouse/Controllers/SlotsController.cs b/ProjectLighthouse/Controllers/SlotsController.cs index ba095e09..1900932c 100644 --- a/ProjectLighthouse/Controllers/SlotsController.cs +++ b/ProjectLighthouse/Controllers/SlotsController.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -18,15 +19,12 @@ namespace ProjectLighthouse.Controllers { } [HttpGet("s/user/{id:int}")] - public async Task SUser(int id) { - Slot slot = await new Database().Slots.FirstOrDefaultAsync(s => s.SlotId == id); + public IActionResult SUser(int id) { + IEnumerable slots = new Database().Slots + .Where(s => s.CreatorId == id) + .AsEnumerable(); - return this.Ok(slot.Serialize()); - } - - [HttpGet("slots/lolcatftw/{username}")] - public IActionResult SlotsLolCat(string username) { - string response = Enumerable.Aggregate(new Database().Slots, string.Empty, (current, slot) => current + slot.Serialize()); + string response = slots.Aggregate(string.Empty, (current, s) => current + s.Serialize()); return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", 1)); } From 0ff9877808975f0042bcdc25750e7a781bbfaec7 Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 16 Oct 2021 17:19:41 -0400 Subject: [PATCH 2/5] Add proper implementation of fetching the level queue --- DatabaseMigrations/7.sql | 17 ++++++++++++ .../Controllers/LevelQueueController.cs | 27 +++++++++++++++++++ ProjectLighthouse/Database.cs | 6 ++++- ProjectLighthouse/Types/QueuedLevel.cs | 19 +++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 DatabaseMigrations/7.sql create mode 100644 ProjectLighthouse/Controllers/LevelQueueController.cs create mode 100644 ProjectLighthouse/Types/QueuedLevel.cs diff --git a/DatabaseMigrations/7.sql b/DatabaseMigrations/7.sql new file mode 100644 index 00000000..ac8e1bc9 --- /dev/null +++ b/DatabaseMigrations/7.sql @@ -0,0 +1,17 @@ +create table QueuedLevels +( + QueuedLevelId int, + UserId int not null, + SlotId int not null +); + +create unique index QueuedLevels_QueuedLevelId_uindex + on QueuedLevels (QueuedLevelId); + +alter table QueuedLevels + add constraint QueuedLevels_pk + primary key (QueuedLevelId); + +alter table QueuedLevels + modify QueuedLevelId int auto_increment; + diff --git a/ProjectLighthouse/Controllers/LevelQueueController.cs b/ProjectLighthouse/Controllers/LevelQueueController.cs new file mode 100644 index 00000000..7fecf4f8 --- /dev/null +++ b/ProjectLighthouse/Controllers/LevelQueueController.cs @@ -0,0 +1,27 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +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 LevelQueueController : ControllerBase { + [HttpGet("slots/lolcatftw/{username}")] + public IActionResult GetLevelQueue(string username) { + IEnumerable queuedLevels = new Database().QueuedLevels + .Include(q => q.User) + .Include(q => q.Slot) + .Where(q => q.User.Username == username) + .AsEnumerable(); + + string response = queuedLevels.Aggregate(string.Empty, (current, q) => current + q.Slot.Serialize()); + + return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", 1)); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index dc33f549..af758fcd 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -1,4 +1,4 @@ -#nullable enable +//#nullable enable using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -11,6 +11,8 @@ namespace ProjectLighthouse { public DbSet Users { get; set; } public DbSet Locations { get; set; } public DbSet Slots { get; set; } + + public DbSet QueuedLevels { get; set; } public DbSet Comments { get; set; } public DbSet Tokens { get; set; } @@ -39,6 +41,7 @@ namespace ProjectLighthouse { } + #nullable enable public async Task AuthenticateUser(LoginData loginData) { // TODO: don't use psn name to authenticate User user = await this.Users.FirstOrDefaultAsync(u => u.Username == loginData.Username) @@ -70,5 +73,6 @@ namespace ProjectLighthouse { return await UserFromAuthToken(mmAuth); } + #nullable disable } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/QueuedLevel.cs b/ProjectLighthouse/Types/QueuedLevel.cs new file mode 100644 index 00000000..03eabc15 --- /dev/null +++ b/ProjectLighthouse/Types/QueuedLevel.cs @@ -0,0 +1,19 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; + +namespace ProjectLighthouse.Types { + public class QueuedLevel { + [Key] public int QueuedLevelId { get; set; } + + public int UserId { get; set; } + + [ForeignKey(nameof(UserId))] + public User User { get; set; } + + public int SlotId { get; set; } + + [ForeignKey(nameof(SlotId))] + public Slot Slot { get; set; } + } +} \ No newline at end of file From f0388732f33c47126f08273913cdb8df3ab9a18b Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 16 Oct 2021 17:39:18 -0400 Subject: [PATCH 3/5] Add/Remove level queue endpoints --- .../Controllers/LevelQueueController.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/ProjectLighthouse/Controllers/LevelQueueController.cs b/ProjectLighthouse/Controllers/LevelQueueController.cs index 7fecf4f8..2c31638b 100644 --- a/ProjectLighthouse/Controllers/LevelQueueController.cs +++ b/ProjectLighthouse/Controllers/LevelQueueController.cs @@ -1,6 +1,8 @@ +#nullable enable using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using ProjectLighthouse.Serialization; @@ -23,5 +25,40 @@ namespace ProjectLighthouse.Controllers { return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", 1)); } + + [HttpPost("lolcatftw/remove/user/{id:int}")] + public async Task RemoveQueuedLevel(int id) { + await using Database database = new(); + + User? user = await database.UserFromRequest(this.Request); + if(user == null) return this.StatusCode(403, ""); + + QueuedLevel queuedLevel = await database.QueuedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); + if(queuedLevel != null) database.QueuedLevels.Remove(queuedLevel); + + await database.SaveChangesAsync(); + + return this.Ok(); + } + + [HttpPost("lolcatftw/add/user/{id:int}")] + public async Task AddQueuedLevel(int id) { + await using Database database = new(); + + User? user = await database.UserFromRequest(this.Request); + if(user == null) return this.StatusCode(403, ""); + + QueuedLevel queuedLevel = await database.QueuedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id); + if(queuedLevel != null) return this.Ok(); + + database.QueuedLevels.Add(new QueuedLevel { + SlotId = id, + UserId = user.UserId + }); + + await database.SaveChangesAsync(); + + return this.Ok(); + } } } \ No newline at end of file From 73fa59157ac5c220063bed5dd872dca170dfa5d0 Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 16 Oct 2021 17:43:10 -0400 Subject: [PATCH 4/5] Cleanup unused directives --- ProjectLighthouse/Controllers/LevelQueueController.cs | 1 - ProjectLighthouse/Controllers/LoginController.cs | 2 -- ProjectLighthouse/Controllers/MessageController.cs | 1 - ProjectLighthouse/Controllers/SlotsController.cs | 2 -- ProjectLighthouse/Database.cs | 2 +- ProjectLighthouse/Helpers/BinaryHelper.cs | 1 - ProjectLighthouse/Types/QueuedLevel.cs | 1 - 7 files changed, 1 insertion(+), 9 deletions(-) diff --git a/ProjectLighthouse/Controllers/LevelQueueController.cs b/ProjectLighthouse/Controllers/LevelQueueController.cs index 2c31638b..85d464c8 100644 --- a/ProjectLighthouse/Controllers/LevelQueueController.cs +++ b/ProjectLighthouse/Controllers/LevelQueueController.cs @@ -1,5 +1,4 @@ #nullable enable -using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; diff --git a/ProjectLighthouse/Controllers/LoginController.cs b/ProjectLighthouse/Controllers/LoginController.cs index 5b137aa4..0d45e1f3 100644 --- a/ProjectLighthouse/Controllers/LoginController.cs +++ b/ProjectLighthouse/Controllers/LoginController.cs @@ -1,9 +1,7 @@ #nullable enable -using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Primitives; using ProjectLighthouse.Types; diff --git a/ProjectLighthouse/Controllers/MessageController.cs b/ProjectLighthouse/Controllers/MessageController.cs index c6a4089c..89e4b89a 100644 --- a/ProjectLighthouse/Controllers/MessageController.cs +++ b/ProjectLighthouse/Controllers/MessageController.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; using ProjectLighthouse.Types; namespace ProjectLighthouse.Controllers { diff --git a/ProjectLighthouse/Controllers/SlotsController.cs b/ProjectLighthouse/Controllers/SlotsController.cs index 1900932c..0caa9313 100644 --- a/ProjectLighthouse/Controllers/SlotsController.cs +++ b/ProjectLighthouse/Controllers/SlotsController.cs @@ -1,8 +1,6 @@ using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; using ProjectLighthouse.Serialization; using ProjectLighthouse.Types; diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index af758fcd..6891f0b5 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -1,5 +1,5 @@ //#nullable enable -using System; + using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; diff --git a/ProjectLighthouse/Helpers/BinaryHelper.cs b/ProjectLighthouse/Helpers/BinaryHelper.cs index c62d14c8..6b66ff1c 100644 --- a/ProjectLighthouse/Helpers/BinaryHelper.cs +++ b/ProjectLighthouse/Helpers/BinaryHelper.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Text; diff --git a/ProjectLighthouse/Types/QueuedLevel.cs b/ProjectLighthouse/Types/QueuedLevel.cs index 03eabc15..f9e191a7 100644 --- a/ProjectLighthouse/Types/QueuedLevel.cs +++ b/ProjectLighthouse/Types/QueuedLevel.cs @@ -1,6 +1,5 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; namespace ProjectLighthouse.Types { public class QueuedLevel { From cf8292031376a888478d79059b12d3d1a32243ea Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 16 Oct 2021 19:21:22 -0400 Subject: [PATCH 5/5] Fix DbConnectionString getter --- ProjectLighthouse/Types/ServerSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectLighthouse/Types/ServerSettings.cs b/ProjectLighthouse/Types/ServerSettings.cs index bde98a9c..d1e5f71f 100644 --- a/ProjectLighthouse/Types/ServerSettings.cs +++ b/ProjectLighthouse/Types/ServerSettings.cs @@ -16,7 +16,7 @@ namespace ProjectLighthouse.Types { public static string DbConnectionString { get { if(dbConnectionString == null) { - return dbConnectionString = Environment.GetEnvironmentVariable("") ?? ""; + return dbConnectionString = Environment.GetEnvironmentVariable("LIGHTHOUSE_DB_CONNECTION_STRING") ?? ""; } return dbConnectionString; }