Reuse database object in users and slots

Should hopefully reduce serialization times.
This commit is contained in:
jvyden 2022-03-05 15:22:48 -05:00
parent 9d3bc7b31a
commit 79fd133093
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
3 changed files with 50 additions and 105 deletions

View file

@ -55,7 +55,7 @@ public class PublishController : ControllerBase
if (oldSlot == null) return this.NotFound();
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, "");
}
@ -137,7 +137,7 @@ public class PublishController : ControllerBase
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, "");
}

View file

@ -19,6 +19,23 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels;
[XmlType("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")]
[NotMapped]
[JsonIgnore]
@ -114,24 +131,12 @@ public class Slot
[XmlIgnore]
[NotMapped]
[JsonIgnore]
public int Hearts {
get {
using Database database = new();
return database.HeartedLevels.Count(s => s.SlotId == this.SlotId);
}
}
public int Hearts => database.HeartedLevels.Count(s => s.SlotId == this.SlotId);
[XmlIgnore]
[NotMapped]
[JsonIgnore]
public int Comments {
get {
using Database database = new();
return database.Comments.Count(c => c.Type == CommentType.Level && c.TargetId == this.SlotId);
}
}
public int Comments => database.Comments.Count(c => c.Type == CommentType.Level && c.TargetId == this.SlotId);
[XmlIgnore]
[NotMapped]
@ -196,32 +201,18 @@ public class Slot
[NotMapped]
[JsonIgnore]
[XmlElement("thumbsup")]
public int Thumbsup {
get {
using Database database = new();
return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1);
}
}
public int Thumbsup => database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1);
[NotMapped]
[JsonIgnore]
[XmlElement("thumbsdown")]
public int Thumbsdown {
get {
using Database database = new();
return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1);
}
}
public int Thumbsdown => database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1);
[NotMapped]
[JsonPropertyName("averageRating")]
[XmlElement("averageRating")]
public double RatingLBP1 {
get {
using Database database = new();
IQueryable<RatedLevel> ratedLevels = database.RatedLevels.Where(r => r.SlotId == this.SlotId && r.RatingLBP1 > 0);
if (!ratedLevels.Any()) return 3.0;
@ -232,13 +223,7 @@ public class Slot
[NotMapped]
[JsonIgnore]
[XmlElement("reviewCount")]
public int ReviewCount {
get {
using Database database = new();
return database.Reviews.Count(r => r.SlotId == this.SlotId);
}
}
public int ReviewCount => database.Reviews.Count(r => r.SlotId == this.SlotId);
[XmlElement("leveltype")]
public string LevelType { get; set; } = "";

View file

@ -10,6 +10,21 @@ namespace LBPUnion.ProjectLighthouse.Types;
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 string Username { get; set; }
@ -54,39 +69,19 @@ public class User
[NotMapped]
[JsonIgnore]
public int Reviews {
get {
using Database database = new();
return database.Reviews.Count(r => r.ReviewerId == this.UserId);
}
}
public int Reviews => database.Reviews.Count(r => r.ReviewerId == this.UserId);
[NotMapped]
[JsonIgnore]
public int Comments {
get {
using Database database = new();
return database.Comments.Count(c => c.Type == CommentType.Profile && c.TargetId == this.UserId);
}
}
public int Comments => database.Comments.Count(c => c.Type == CommentType.Profile && c.TargetId == this.UserId);
[NotMapped]
[JsonIgnore]
public int PhotosByMe {
get {
using Database database = new();
return database.Photos.Count(p => p.CreatorId == this.UserId);
}
}
public int PhotosByMe => database.Photos.Count(p => p.CreatorId == this.UserId);
[NotMapped]
[JsonIgnore]
public int PhotosWithMe {
get {
using Database database = new();
return Enumerable.Sum(database.Photos, photo => photo.Subjects.Count(subject => subject.User.UserId == this.UserId));
}
}
public int PhotosWithMe => Enumerable.Sum(database.Photos, photo => photo.Subjects.Count(subject => subject.User.UserId == this.UserId));
[JsonIgnore]
public int LocationId { get; set; }
@ -100,30 +95,15 @@ public class User
[NotMapped]
[JsonIgnore]
public int HeartedLevels {
get {
using Database database = new();
return database.HeartedLevels.Count(p => p.UserId == this.UserId);
}
}
public int HeartedLevels => database.HeartedLevels.Count(p => p.UserId == this.UserId);
[NotMapped]
[JsonIgnore]
public int HeartedUsers {
get {
using Database database = new();
return database.HeartedProfiles.Count(p => p.UserId == this.UserId);
}
}
public int HeartedUsers => database.HeartedProfiles.Count(p => p.UserId == this.UserId);
[NotMapped]
[JsonIgnore]
public int QueuedLevels {
get {
using Database database = new();
return database.QueuedLevels.Count(p => p.UserId == this.UserId);
}
}
public int QueuedLevels => database.QueuedLevels.Count(p => p.UserId == this.UserId);
[JsonIgnore]
public string Pins { get; set; } = "";
@ -138,13 +118,7 @@ public class User
public string PlanetHashLBPVita { get; set; } = "";
[JsonIgnore]
public int Hearts {
get {
using Database database = new();
return database.HeartedProfiles.Count(s => s.HeartedUserId == this.UserId);
}
}
public int Hearts => database.HeartedProfiles.Count(s => s.HeartedUserId == this.UserId);
[JsonIgnore]
public bool IsAdmin { get; set; } = false;
@ -156,16 +130,9 @@ public class User
public string BooHash { get; set; } = "";
public string MehHash { get; set; } = "";
#nullable enable
[NotMapped]
[JsonIgnore]
public UserStatus Status {
get {
using Database database = new();
return new UserStatus(database, this.UserId);
}
}
#nullable disable
public UserStatus Status => new(database, this.UserId);
[JsonIgnore]
public bool Banned { get; set; }
@ -223,18 +190,11 @@ public class User
/// </summary>
[NotMapped]
[JsonIgnore]
public int UsedSlots {
get {
using Database database = new();
return database.Slots.Count(s => s.CreatorId == this.UserId);
}
}
public int UsedSlots => database.Slots.Count(s => s.CreatorId == this.UserId);
#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);
}
#nullable disable