diff --git a/.idea/.idea.ProjectLighthouse/.idea/discord.xml b/.idea/.idea.ProjectLighthouse/.idea/discord.xml index d8e95616..62733844 100644 --- a/.idea/.idea.ProjectLighthouse/.idea/discord.xml +++ b/.idea/.idea.ProjectLighthouse/.idea/discord.xml @@ -2,6 +2,8 @@ \ No newline at end of file diff --git a/ProjectLighthouse/Controllers/GameApi/Slots/SlotsController.cs b/ProjectLighthouse/Controllers/GameApi/Slots/SlotsController.cs index eb60df41..bac17fa8 100644 --- a/ProjectLighthouse/Controllers/GameApi/Slots/SlotsController.cs +++ b/ProjectLighthouse/Controllers/GameApi/Slots/SlotsController.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Helpers; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -25,24 +26,6 @@ public class SlotsController : ControllerBase this.database = database; } - private IQueryable getSlots(GameVersion gameVersion, bool includeSublevels = false) - { - IQueryable query = this.database.Slots.Include(s => s.Creator).Include(s => s.Location); - - if (gameVersion == GameVersion.LittleBigPlanetVita || gameVersion == GameVersion.LittleBigPlanetPSP || gameVersion == GameVersion.Unknown) - { - query = query.Where(s => s.GameVersion == gameVersion); - } - else - { - query = query.Where(s => s.GameVersion <= gameVersion); - } - - if (!includeSublevels) query = query.Where(s => !s.SubLevel); - - return query; - } - [HttpGet("slots/by")] public async Task SlotsBy([FromQuery] string u, [FromQuery] int pageStart, [FromQuery] int pageSize) { @@ -56,7 +39,7 @@ public class SlotsController : ControllerBase string response = Enumerable.Aggregate ( - this.getSlots(gameVersion, token.UserId == user.UserId) + this.database.Slots.ByGameVersion(gameVersion, token.UserId == user.UserId) .Where(s => s.Creator!.Username == user.Username) .Skip(pageStart - 1) .Take(Math.Min(pageSize, ServerSettings.Instance.EntitledSlots)), @@ -94,7 +77,7 @@ public class SlotsController : ControllerBase GameVersion gameVersion = token.GameVersion; - Slot? slot = await this.getSlots(gameVersion).FirstOrDefaultAsync(s => s.SlotId == id); + Slot? slot = await this.database.Slots.ByGameVersion(gameVersion).FirstOrDefaultAsync(s => s.SlotId == id); if (slot == null) return this.NotFound(); @@ -136,7 +119,11 @@ public class SlotsController : ControllerBase GameVersion gameVersion = token.GameVersion; - IQueryable slots = this.getSlots(gameVersion).OrderByDescending(s => s.FirstUploaded).Skip(pageStart - 1).Take(Math.Min(pageSize, 30)); + IQueryable slots = this.database.Slots.ByGameVersion + (gameVersion) + .OrderByDescending(s => s.FirstUploaded) + .Skip(pageStart - 1) + .Take(Math.Min(pageSize, 30)); string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion)); @@ -167,7 +154,7 @@ public class SlotsController : ControllerBase GameVersion gameVersion = token.GameVersion; - IQueryable slots = this.getSlots(gameVersion) + IQueryable slots = this.database.Slots.ByGameVersion(gameVersion) .Where(s => s.TeamPick) .OrderByDescending(s => s.LastUpdated) .Skip(pageStart - 1) @@ -201,7 +188,7 @@ public class SlotsController : ControllerBase GameVersion gameVersion = token.GameVersion; - IEnumerable slots = this.getSlots(gameVersion).OrderBy(_ => EF.Functions.Random()).Take(Math.Min(pageSize, 30)); + IEnumerable slots = this.database.Slots.ByGameVersion(gameVersion).OrderBy(_ => EF.Functions.Random()).Take(Math.Min(pageSize, 30)); string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(gameVersion)); @@ -390,7 +377,7 @@ public class SlotsController : ControllerBase { if (version == GameVersion.LittleBigPlanetVita || version == GameVersion.LittleBigPlanetPSP || version == GameVersion.Unknown) { - return this.getSlots(version); + return this.database.Slots.ByGameVersion(version); } string _dateFilterType = dateFilterType ?? ""; diff --git a/ProjectLighthouse/Helpers/Extensions/SlotsExtensions.cs b/ProjectLighthouse/Helpers/Extensions/SlotsExtensions.cs new file mode 100644 index 00000000..457ef413 --- /dev/null +++ b/ProjectLighthouse/Helpers/Extensions/SlotsExtensions.cs @@ -0,0 +1,31 @@ +using System.Linq; +using LBPUnion.ProjectLighthouse.Types; +using LBPUnion.ProjectLighthouse.Types.Levels; +using Microsoft.EntityFrameworkCore; + +namespace LBPUnion.ProjectLighthouse.Helpers.Extensions; + +public static class SlotsExtensions +{ + public static IQueryable ByGameVersion + (this DbSet set, GameVersion gameVersion, bool includeSublevels = false) + => set.AsQueryable().ByGameVersion(gameVersion, includeSublevels); + + public static IQueryable ByGameVersion(this IQueryable queryable, GameVersion gameVersion, bool includeSublevels = false) + { + IQueryable query = queryable.Include(s => s.Creator).Include(s => s.Location); + + if (gameVersion == GameVersion.LittleBigPlanetVita || gameVersion == GameVersion.LittleBigPlanetPSP || gameVersion == GameVersion.Unknown) + { + query = query.Where(s => s.GameVersion == gameVersion); + } + else + { + query = query.Where(s => s.GameVersion <= gameVersion); + } + + if (!includeSublevels) query = query.Where(s => !s.SubLevel); + + return query; + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/CustomCategory.cs b/ProjectLighthouse/Types/Categories/CustomCategory.cs index 54b824f4..4d48d3dd 100644 --- a/ProjectLighthouse/Types/Categories/CustomCategory.cs +++ b/ProjectLighthouse/Types/Categories/CustomCategory.cs @@ -1,6 +1,7 @@ #nullable enable using System.Collections.Generic; using System.Linq; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Types.Levels; namespace LBPUnion.ProjectLighthouse.Types.Categories; @@ -34,6 +35,8 @@ public class CustomCategory : Category public sealed override string IconHash { get; set; } public sealed override string Endpoint { get; set; } public override Slot? GetPreviewSlot(Database database) => database.Slots.FirstOrDefault(s => s.SlotId == this.SlotIds[0]); - public override IEnumerable GetSlots(Database database, int pageStart, int pageSize) => database.Slots.Where(s => this.SlotIds.Contains(s.SlotId)); + public override IEnumerable GetSlots + (Database database, int pageStart, int pageSize) + => database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3).Where(s => this.SlotIds.Contains(s.SlotId)); public override int GetTotalSlots(Database database) => this.SlotIds.Count; } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/HeartedCategory.cs b/ProjectLighthouse/Types/Categories/HeartedCategory.cs index 318aa5c3..24b9e12c 100644 --- a/ProjectLighthouse/Types/Categories/HeartedCategory.cs +++ b/ProjectLighthouse/Types/Categories/HeartedCategory.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Types.Levels; using Microsoft.EntityFrameworkCore; @@ -16,5 +17,11 @@ public class HeartedCategory : CategoryWithUser public override Slot? GetPreviewSlot(Database database, User user) => database.HeartedLevels.FirstOrDefault(h => h.UserId == user.UserId)?.Slot; public override int GetTotalSlots(Database database, User user) => database.HeartedLevels.Count(h => h.UserId == user.UserId); public override IEnumerable GetSlots(Database database, User user, int pageStart, int pageSize) - => database.HeartedLevels.Where(h => h.UserId == user.UserId).Include(h => h.Slot).Select(h => h.Slot).Skip(pageStart).Take(Math.Min(pageSize, 20)); + => database.HeartedLevels.Where + (h => h.UserId == user.UserId) + .Include(h => h.Slot) + .Select(h => h.Slot) + .ByGameVersion(GameVersion.LittleBigPlanet3) + .Skip(pageStart) + .Take(Math.Min(pageSize, 20)); } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs b/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs index 65f14ccb..492c66e8 100644 --- a/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs +++ b/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Types.Levels; namespace LBPUnion.ProjectLighthouse.Types.Categories; @@ -15,6 +16,6 @@ public class NewestLevelsCategory : Category public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(); public override IEnumerable GetSlots (Database database, int pageStart, int pageSize) - => database.Slots.OrderByDescending(s => s.FirstUploaded).Skip(pageStart - 1).Take(Math.Min(pageSize, 20)); + => database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3).OrderByDescending(s => s.FirstUploaded).Skip(pageStart - 1).Take(Math.Min(pageSize, 20)); public override int GetTotalSlots(Database database) => database.Slots.Count(); } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/QueueCategory.cs b/ProjectLighthouse/Types/Categories/QueueCategory.cs index 40d2f6b0..322afea6 100644 --- a/ProjectLighthouse/Types/Categories/QueueCategory.cs +++ b/ProjectLighthouse/Types/Categories/QueueCategory.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Types.Levels; using Microsoft.EntityFrameworkCore; @@ -18,7 +19,13 @@ public class QueueCategory : CategoryWithUser public override Slot? GetPreviewSlot(Database database, User user) => database.QueuedLevels.Include(q => q.Slot).FirstOrDefault(q => q.UserId == user.UserId)?.Slot; public override IEnumerable GetSlots(Database database, User user, int pageStart, int pageSize) - => database.QueuedLevels.Include(q => q.Slot).Include(q => q.Slot.Location).Select(q => q.Slot).Skip(pageStart - 1).Take(Math.Min(pageSize, 20)); + => database.QueuedLevels.Include + (q => q.Slot) + .Include(q => q.Slot.Location) + .Select(q => q.Slot) + .ByGameVersion(GameVersion.LittleBigPlanet3) + .Skip(pageStart - 1) + .Take(Math.Min(pageSize, 20)); public override int GetTotalSlots(Database database, User user) => database.QueuedLevels.Count(q => q.UserId == user.UserId); } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs b/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs index 116daee3..3c719553 100644 --- a/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs +++ b/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using LBPUnion.ProjectLighthouse.Helpers.Extensions; using LBPUnion.ProjectLighthouse.Types.Levels; namespace LBPUnion.ProjectLighthouse.Types.Categories; @@ -15,6 +16,11 @@ public class TeamPicksCategory : Category public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(s => s.TeamPick); public override IEnumerable GetSlots (Database database, int pageStart, int pageSize) - => database.Slots.OrderByDescending(s => s.FirstUploaded).Where(s => s.TeamPick).Skip(pageStart - 1).Take(Math.Min(pageSize, 20)); + => database.Slots.ByGameVersion + (GameVersion.LittleBigPlanet3) + .OrderByDescending(s => s.FirstUploaded) + .Where(s => s.TeamPick) + .Skip(pageStart - 1) + .Take(Math.Min(pageSize, 20)); public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.TeamPick); } \ No newline at end of file