mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-07 13:51:28 +00:00
Reuse database object in users and slots
Should hopefully reduce serialization times.
This commit is contained in:
parent
9d3bc7b31a
commit
79fd133093
3 changed files with 50 additions and 105 deletions
|
@ -55,7 +55,7 @@ public class PublishController : ControllerBase
|
||||||
if (oldSlot == null) return this.NotFound();
|
if (oldSlot == null) return this.NotFound();
|
||||||
if (oldSlot.CreatorId != user.UserId) return this.BadRequest();
|
if (oldSlot.CreatorId != user.UserId) return this.BadRequest();
|
||||||
}
|
}
|
||||||
else if (user.GetUsedSlotsForGame(gameToken.GameVersion, database) > ServerSettings.Instance.EntitledSlots)
|
else if (user.GetUsedSlotsForGame(gameToken.GameVersion) > ServerSettings.Instance.EntitledSlots)
|
||||||
{
|
{
|
||||||
return this.StatusCode(403, "");
|
return this.StatusCode(403, "");
|
||||||
}
|
}
|
||||||
|
@ -137,7 +137,7 @@ public class PublishController : ControllerBase
|
||||||
return this.Ok(oldSlot.Serialize(gameToken.GameVersion));
|
return this.Ok(oldSlot.Serialize(gameToken.GameVersion));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.GetUsedSlotsForGame(gameToken.GameVersion, database) > ServerSettings.Instance.EntitledSlots)
|
if (user.GetUsedSlotsForGame(gameToken.GameVersion) > ServerSettings.Instance.EntitledSlots)
|
||||||
{
|
{
|
||||||
return this.StatusCode(403, "");
|
return this.StatusCode(403, "");
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,23 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels;
|
||||||
[XmlType("slot")]
|
[XmlType("slot")]
|
||||||
public class Slot
|
public class Slot
|
||||||
{
|
{
|
||||||
|
[NotMapped]
|
||||||
|
[JsonIgnore]
|
||||||
|
[XmlIgnore]
|
||||||
|
private Database? _database;
|
||||||
|
|
||||||
|
[NotMapped]
|
||||||
|
[JsonIgnore]
|
||||||
|
[XmlIgnore]
|
||||||
|
private Database database {
|
||||||
|
get {
|
||||||
|
if (this._database != null) return this._database;
|
||||||
|
|
||||||
|
return this._database = new Database();
|
||||||
|
}
|
||||||
|
set => this._database = value;
|
||||||
|
}
|
||||||
|
|
||||||
[XmlAttribute("type")]
|
[XmlAttribute("type")]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
|
@ -114,24 +131,12 @@ public class Slot
|
||||||
[XmlIgnore]
|
[XmlIgnore]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int Hearts {
|
public int Hearts => database.HeartedLevels.Count(s => s.SlotId == this.SlotId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
|
|
||||||
return database.HeartedLevels.Count(s => s.SlotId == this.SlotId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[XmlIgnore]
|
[XmlIgnore]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int Comments {
|
public int Comments => database.Comments.Count(c => c.Type == CommentType.Level && c.TargetId == this.SlotId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
|
|
||||||
return database.Comments.Count(c => c.Type == CommentType.Level && c.TargetId == this.SlotId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[XmlIgnore]
|
[XmlIgnore]
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
|
@ -196,32 +201,18 @@ public class Slot
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
[XmlElement("thumbsup")]
|
[XmlElement("thumbsup")]
|
||||||
public int Thumbsup {
|
public int Thumbsup => database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
|
|
||||||
return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
[XmlElement("thumbsdown")]
|
[XmlElement("thumbsdown")]
|
||||||
public int Thumbsdown {
|
public int Thumbsdown => database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
|
|
||||||
return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonPropertyName("averageRating")]
|
[JsonPropertyName("averageRating")]
|
||||||
[XmlElement("averageRating")]
|
[XmlElement("averageRating")]
|
||||||
public double RatingLBP1 {
|
public double RatingLBP1 {
|
||||||
get {
|
get {
|
||||||
using Database database = new();
|
|
||||||
|
|
||||||
IQueryable<RatedLevel> ratedLevels = database.RatedLevels.Where(r => r.SlotId == this.SlotId && r.RatingLBP1 > 0);
|
IQueryable<RatedLevel> ratedLevels = database.RatedLevels.Where(r => r.SlotId == this.SlotId && r.RatingLBP1 > 0);
|
||||||
if (!ratedLevels.Any()) return 3.0;
|
if (!ratedLevels.Any()) return 3.0;
|
||||||
|
|
||||||
|
@ -232,13 +223,7 @@ public class Slot
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
[XmlElement("reviewCount")]
|
[XmlElement("reviewCount")]
|
||||||
public int ReviewCount {
|
public int ReviewCount => database.Reviews.Count(r => r.SlotId == this.SlotId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
|
|
||||||
return database.Reviews.Count(r => r.SlotId == this.SlotId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[XmlElement("leveltype")]
|
[XmlElement("leveltype")]
|
||||||
public string LevelType { get; set; } = "";
|
public string LevelType { get; set; } = "";
|
||||||
|
|
|
@ -10,6 +10,21 @@ namespace LBPUnion.ProjectLighthouse.Types;
|
||||||
|
|
||||||
public class User
|
public class User
|
||||||
{
|
{
|
||||||
|
[NotMapped]
|
||||||
|
[JsonIgnore]
|
||||||
|
private Database? _database;
|
||||||
|
|
||||||
|
[NotMapped]
|
||||||
|
[JsonIgnore]
|
||||||
|
private Database database {
|
||||||
|
get {
|
||||||
|
if (this._database != null) return this._database;
|
||||||
|
|
||||||
|
return this._database = new Database();
|
||||||
|
}
|
||||||
|
set => this._database = value;
|
||||||
|
}
|
||||||
|
|
||||||
public int UserId { get; set; }
|
public int UserId { get; set; }
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
|
|
||||||
|
@ -54,39 +69,19 @@ public class User
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int Reviews {
|
public int Reviews => database.Reviews.Count(r => r.ReviewerId == this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return database.Reviews.Count(r => r.ReviewerId == this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int Comments {
|
public int Comments => database.Comments.Count(c => c.Type == CommentType.Profile && c.TargetId == this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return database.Comments.Count(c => c.Type == CommentType.Profile && c.TargetId == this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int PhotosByMe {
|
public int PhotosByMe => database.Photos.Count(p => p.CreatorId == this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return database.Photos.Count(p => p.CreatorId == this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int PhotosWithMe {
|
public int PhotosWithMe => Enumerable.Sum(database.Photos, photo => photo.Subjects.Count(subject => subject.User.UserId == this.UserId));
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return Enumerable.Sum(database.Photos, photo => photo.Subjects.Count(subject => subject.User.UserId == this.UserId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int LocationId { get; set; }
|
public int LocationId { get; set; }
|
||||||
|
@ -100,30 +95,15 @@ public class User
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int HeartedLevels {
|
public int HeartedLevels => database.HeartedLevels.Count(p => p.UserId == this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return database.HeartedLevels.Count(p => p.UserId == this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int HeartedUsers {
|
public int HeartedUsers => database.HeartedProfiles.Count(p => p.UserId == this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return database.HeartedProfiles.Count(p => p.UserId == this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int QueuedLevels {
|
public int QueuedLevels => database.QueuedLevels.Count(p => p.UserId == this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return database.QueuedLevels.Count(p => p.UserId == this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string Pins { get; set; } = "";
|
public string Pins { get; set; } = "";
|
||||||
|
@ -138,13 +118,7 @@ public class User
|
||||||
public string PlanetHashLBPVita { get; set; } = "";
|
public string PlanetHashLBPVita { get; set; } = "";
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int Hearts {
|
public int Hearts => database.HeartedProfiles.Count(s => s.HeartedUserId == this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
|
|
||||||
return database.HeartedProfiles.Count(s => s.HeartedUserId == this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public bool IsAdmin { get; set; } = false;
|
public bool IsAdmin { get; set; } = false;
|
||||||
|
@ -156,16 +130,9 @@ public class User
|
||||||
public string BooHash { get; set; } = "";
|
public string BooHash { get; set; } = "";
|
||||||
public string MehHash { get; set; } = "";
|
public string MehHash { get; set; } = "";
|
||||||
|
|
||||||
#nullable enable
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public UserStatus Status {
|
public UserStatus Status => new(database, this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return new UserStatus(database, this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public bool Banned { get; set; }
|
public bool Banned { get; set; }
|
||||||
|
@ -223,18 +190,11 @@ public class User
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int UsedSlots {
|
public int UsedSlots => database.Slots.Count(s => s.CreatorId == this.UserId);
|
||||||
get {
|
|
||||||
using Database database = new();
|
|
||||||
return database.Slots.Count(s => s.CreatorId == this.UserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#nullable enable
|
#nullable enable
|
||||||
public int GetUsedSlotsForGame(GameVersion version, Database? database = null)
|
public int GetUsedSlotsForGame(GameVersion version)
|
||||||
{
|
{
|
||||||
database ??= new Database();
|
|
||||||
|
|
||||||
return database.Slots.Count(s => s.CreatorId == this.UserId && s.GameVersion == version);
|
return database.Slots.Count(s => s.CreatorId == this.UserId && s.GameVersion == version);
|
||||||
}
|
}
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue