Refactor Database class (#616)

Refactor Database into DatabaseContext
Moved into separate folder so it actually has a namespace instead sitting in the root
This commit is contained in:
Josh 2023-02-15 23:54:30 -06:00 committed by GitHub
commit 64b95e807d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
246 changed files with 1211 additions and 965 deletions

View file

@ -1,3 +1,4 @@
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -18,7 +19,7 @@ public static class CategoryHelper
Categories.Add(new HeartedCategory());
Categories.Add(new LuckyDipCategory());
using Database database = new();
using DatabaseContext database = new();
foreach (DatabaseCategory category in database.CustomCategories) Categories.Add(new CustomCategory(category));
}
}

View file

@ -1,5 +1,6 @@
#nullable enable
using System.Diagnostics;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
@ -11,8 +12,8 @@ namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Types.Categories;
public abstract class CategoryWithUser : Category
{
public abstract Slot? GetPreviewSlot(Database database, User user);
public override Slot? GetPreviewSlot(Database database)
public abstract Slot? GetPreviewSlot(DatabaseContext database, User user);
public override Slot? GetPreviewSlot(DatabaseContext database)
{
#if DEBUG
Logger.Error("tried to get preview slot without user on CategoryWithUser", LogArea.Category);
@ -21,8 +22,8 @@ public abstract class CategoryWithUser : Category
return null;
}
public abstract int GetTotalSlots(Database database, User user);
public override int GetTotalSlots(Database database)
public abstract int GetTotalSlots(DatabaseContext database, User user);
public override int GetTotalSlots(DatabaseContext database)
{
#if DEBUG
Logger.Error("tried to get total slots without user on CategoryWithUser", LogArea.Category);
@ -31,8 +32,8 @@ public abstract class CategoryWithUser : Category
return -1;
}
public abstract IEnumerable<Slot> GetSlots(Database database, User user, int pageStart, int pageSize);
public override IEnumerable<Slot> GetSlots(Database database, int pageStart, int pageSize)
public abstract IEnumerable<Slot> GetSlots(DatabaseContext database, User user, int pageStart, int pageSize);
public override IEnumerable<Slot> GetSlots(DatabaseContext database, int pageStart, int pageSize)
{
#if DEBUG
Logger.Error("tried to get slots without user on CategoryWithUser", LogArea.Category);
@ -41,13 +42,13 @@ public abstract class CategoryWithUser : Category
return new List<Slot>();
}
public new string Serialize(Database database)
public new string Serialize(DatabaseContext database)
{
Logger.Error("tried to serialize without user on CategoryWithUser", LogArea.Category);
return string.Empty;
}
public string Serialize(Database database, User user)
public string Serialize(DatabaseContext database, User user)
{
Slot? previewSlot = this.GetPreviewSlot(database, user);

View file

@ -1,4 +1,5 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -34,9 +35,9 @@ public class CustomCategory : Category
public sealed override string Description { get; set; }
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 Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.FirstOrDefault(s => s.SlotId == this.SlotIds[0]);
public override IEnumerable<Slot> GetSlots
(Database database, int pageStart, int pageSize)
(DatabaseContext 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;
public override int GetTotalSlots(DatabaseContext database) => this.SlotIds.Count;
}

View file

@ -1,4 +1,5 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
@ -14,7 +15,7 @@ public class HeartedCategory : CategoryWithUser
public override string Description { get; set; } = "Content you've hearted";
public override string IconHash { get; set; } = "g820611";
public override string Endpoint { get; set; } = "hearted";
public override Slot? GetPreviewSlot(Database database, User user) // note: developer slots act up in LBP3 when listed here, so I omitted it
public override Slot? GetPreviewSlot(DatabaseContext database, User user) // note: developer slots act up in LBP3 when listed here, so I omitted it
=> database.HeartedLevels.Where(h => h.UserId == user.UserId)
.Where(h => h.Slot.Type == SlotType.User && !h.Slot.Hidden && h.Slot.GameVersion <= GameVersion.LittleBigPlanet3)
.OrderByDescending(h => h.HeartedLevelId)
@ -24,7 +25,7 @@ public class HeartedCategory : CategoryWithUser
.ByGameVersion(GameVersion.LittleBigPlanet3, false, false, true)
.FirstOrDefault();
public override IEnumerable<Slot> GetSlots(Database database, User user, int pageStart, int pageSize)
public override IEnumerable<Slot> GetSlots(DatabaseContext database, User user, int pageStart, int pageSize)
=> database.HeartedLevels.Where(h => h.UserId == user.UserId)
.Where(h => h.Slot.Type == SlotType.User && !h.Slot.Hidden && h.Slot.GameVersion <= GameVersion.LittleBigPlanet3)
.OrderByDescending(h => h.HeartedLevelId)
@ -35,5 +36,5 @@ public class HeartedCategory : CategoryWithUser
.Skip(Math.Max(0, pageStart))
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database, User user) => database.HeartedLevels.Count(h => h.UserId == user.UserId);
public override int GetTotalSlots(DatabaseContext database, User user) => database.HeartedLevels.Count(h => h.UserId == user.UserId);
}

View file

@ -1,5 +1,6 @@
#nullable enable
using System.Security.Cryptography;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -13,14 +14,14 @@ public class HighestRatedCategory : Category
public override string Description { get; set; } = "Community Highest Rated content";
public override string IconHash { get; set; } = "g820603";
public override string Endpoint { get; set; } = "thumbs";
public override Slot? GetPreviewSlot(Database database) => database.Slots.Where(s => s.Type == SlotType.User).AsEnumerable().MaxBy(s => s.Thumbsup);
public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.Where(s => s.Type == SlotType.User).AsEnumerable().MaxBy(s => s.Thumbsup);
public override IEnumerable<Slot> GetSlots
(Database database, int pageStart, int pageSize)
(DatabaseContext database, int pageStart, int pageSize)
=> database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true)
.AsEnumerable()
.OrderByDescending(s => s.Thumbsup)
.ThenBy(_ => RandomNumberGenerator.GetInt32(int.MaxValue))
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.Type == SlotType.User);
public override int GetTotalSlots(DatabaseContext database) => database.Slots.Count(s => s.Type == SlotType.User);
}

View file

@ -1,4 +1,5 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -13,12 +14,12 @@ public class LuckyDipCategory : Category
public override string Description { get; set; } = "Randomized uploaded content";
public override string IconHash { get; set; } = "g820605";
public override string Endpoint { get; set; } = "lbp2luckydip";
public override Slot? GetPreviewSlot(Database database) => database.Slots.Where(s => s.Type == SlotType.User).OrderByDescending(_ => EF.Functions.Random()).FirstOrDefault();
public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.Where(s => s.Type == SlotType.User).OrderByDescending(_ => EF.Functions.Random()).FirstOrDefault();
public override IEnumerable<Slot> GetSlots
(Database database, int pageStart, int pageSize)
(DatabaseContext database, int pageStart, int pageSize)
=> database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true)
.OrderByDescending(_ => EF.Functions.Random())
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.Type == SlotType.User);
public override int GetTotalSlots(DatabaseContext database) => database.Slots.Count(s => s.Type == SlotType.User);
}

View file

@ -1,5 +1,6 @@
#nullable enable
using System.Security.Cryptography;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -13,14 +14,14 @@ public class MostHeartedCategory : Category
public override string Description { get; set; } = "The Most Hearted Content";
public override string IconHash { get; set; } = "g820607";
public override string Endpoint { get; set; } = "mostHearted";
public override Slot? GetPreviewSlot(Database database) => database.Slots.Where(s => s.Type == SlotType.User).AsEnumerable().MaxBy(s => s.Hearts);
public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.Where(s => s.Type == SlotType.User).AsEnumerable().MaxBy(s => s.Hearts);
public override IEnumerable<Slot> GetSlots
(Database database, int pageStart, int pageSize)
(DatabaseContext database, int pageStart, int pageSize)
=> database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true)
.AsEnumerable()
.OrderByDescending(s => s.Hearts)
.ThenBy(_ => RandomNumberGenerator.GetInt32(int.MaxValue))
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.Type == SlotType.User);
public override int GetTotalSlots(DatabaseContext database) => database.Slots.Count(s => s.Type == SlotType.User);
}

View file

@ -1,4 +1,5 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -12,17 +13,17 @@ public class MostPlayedCategory : Category
public override string Description { get; set; } = "The most played content";
public override string IconHash { get; set; } = "g820608";
public override string Endpoint { get; set; } = "mostUniquePlays";
public override Slot? GetPreviewSlot(Database database) => database.Slots
public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots
.Where(s => s.Type == SlotType.User)
.OrderByDescending(s => s.PlaysLBP1Unique + s.PlaysLBP2Unique + s.PlaysLBP3Unique)
.ThenByDescending(s => s.PlaysLBP1 + s.PlaysLBP2 + s.PlaysLBP3)
.FirstOrDefault();
public override IEnumerable<Slot> GetSlots
(Database database, int pageStart, int pageSize)
(DatabaseContext database, int pageStart, int pageSize)
=> database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true)
.OrderByDescending(s => s.PlaysLBP1Unique + s.PlaysLBP2Unique + s.PlaysLBP3Unique)
.ThenByDescending(s => s.PlaysLBP1 + s.PlaysLBP2 + s.PlaysLBP3)
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.Type == SlotType.User);
public override int GetTotalSlots(DatabaseContext database) => database.Slots.Count(s => s.Type == SlotType.User);
}

View file

@ -1,4 +1,5 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -12,12 +13,12 @@ public class NewestLevelsCategory : Category
public override string Description { get; set; } = "The most recently published content";
public override string IconHash { get; set; } = "g820623";
public override string Endpoint { get; set; } = "newest";
public override Slot? GetPreviewSlot(Database database) => database.Slots.Where(s => s.Type == SlotType.User).OrderByDescending(s => s.FirstUploaded).FirstOrDefault();
public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.Where(s => s.Type == SlotType.User).OrderByDescending(s => s.FirstUploaded).FirstOrDefault();
public override IEnumerable<Slot> GetSlots
(Database database, int pageStart, int pageSize)
(DatabaseContext database, int pageStart, int pageSize)
=> database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true)
.OrderByDescending(s => s.FirstUploaded)
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.Type == SlotType.User);
public override int GetTotalSlots(DatabaseContext database) => database.Slots.Count(s => s.Type == SlotType.User);
}

View file

@ -1,4 +1,5 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
@ -14,7 +15,7 @@ public class QueueCategory : CategoryWithUser
public override string Description { get; set; } = "Your queued content";
public override string IconHash { get; set; } = "g820614";
public override string Endpoint { get; set; } = "queue";
public override Slot? GetPreviewSlot(Database database, User user)
public override Slot? GetPreviewSlot(DatabaseContext database, User user)
=> database.QueuedLevels.Where(q => q.UserId == user.UserId)
.Where(q => q.Slot.Type == SlotType.User && !q.Slot.Hidden && q.Slot.GameVersion <= GameVersion.LittleBigPlanet3)
.OrderByDescending(q => q.QueuedLevelId)
@ -24,7 +25,7 @@ public class QueueCategory : CategoryWithUser
.ByGameVersion(GameVersion.LittleBigPlanet3, false, false, true)
.FirstOrDefault();
public override IEnumerable<Slot> GetSlots(Database database, User user, int pageStart, int pageSize)
public override IEnumerable<Slot> GetSlots(DatabaseContext database, User user, int pageStart, int pageSize)
=> database.QueuedLevels.Where(q => q.UserId == user.UserId)
.Where(q => q.Slot.Type == SlotType.User && !q.Slot.Hidden && q.Slot.GameVersion <= GameVersion.LittleBigPlanet3)
.OrderByDescending(q => q.QueuedLevelId)
@ -35,5 +36,5 @@ public class QueueCategory : CategoryWithUser
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database, User user) => database.QueuedLevels.Count(q => q.UserId == user.UserId);
public override int GetTotalSlots(DatabaseContext database, User user) => database.QueuedLevels.Count(q => q.UserId == user.UserId);
}

View file

@ -1,4 +1,5 @@
#nullable enable
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -12,13 +13,13 @@ public class TeamPicksCategory : Category
public override string Description { get; set; } = "Community Team Picks";
public override string IconHash { get; set; } = "g820626";
public override string Endpoint { get; set; } = "team_picks";
public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(s => s.TeamPick);
public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(s => s.TeamPick);
public override IEnumerable<Slot> GetSlots
(Database database, int pageStart, int pageSize)
(DatabaseContext database, int pageStart, int pageSize)
=> database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true)
.OrderByDescending(s => s.FirstUploaded)
.Where(s => s.TeamPick)
.Skip(Math.Max(0, pageStart - 1))
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.TeamPick);
public override int GetTotalSlots(DatabaseContext database) => database.Slots.Count(s => s.TeamPick);
}