diff --git a/ProjectLighthouse/Controllers/ReviewController.cs b/ProjectLighthouse/Controllers/ReviewController.cs index 919e1dba..cece8a3e 100644 --- a/ProjectLighthouse/Controllers/ReviewController.cs +++ b/ProjectLighthouse/Controllers/ReviewController.cs @@ -20,29 +20,59 @@ namespace LBPUnion.ProjectLighthouse.Controllers this.database = database; } - [HttpPost("dpadrate/user/{slotId}")] - public async Task DPadRate(int slotId, [FromQuery] int rating) + // LBP1 rating + [HttpPost("rate/user/{slotId}")] + public async Task Rate(int slotId, [FromQuery] int rating) { User? user = await this.database.UserFromRequest(this.Request); if (user == null) return this.StatusCode(403, ""); - Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == slotId); + Slot? slot = await this.database.Slots.Include(s => s.Creator).Include(s => s.Location).FirstOrDefaultAsync(s => s.SlotId == slotId); if (slot == null) return this.StatusCode(403, ""); RatedLevel? ratedLevel = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.SlotId == slotId && r.UserId == user.UserId); if (ratedLevel == null) { ratedLevel = new(); + ratedLevel.SlotId = slotId; + ratedLevel.UserId = user.UserId; + ratedLevel.Rating = 0; this.database.RatedLevels.Add(ratedLevel); } - ratedLevel.SlotId = slotId; - ratedLevel.UserId = user.UserId; - ratedLevel.Rating = rating; - // Unsupported: ratedLevel.LBP1Rating + + ratedLevel.RatingLBP1 = Math.Max(Math.Min(5, rating), 0); await this.database.SaveChangesAsync(); return this.Ok(); } + + // LBP2 and beyond rating + [HttpPost("dpadrate/user/{slotId}")] + public async Task DPadRate(int slotId, [FromQuery] int rating) + { + User? user = await this.database.UserFromRequest(this.Request); + if (user == null) return this.StatusCode(403, ""); + + Slot? slot = await this.database.Slots.Include(s => s.Creator).Include(s => s.Location).FirstOrDefaultAsync(s => s.SlotId == slotId); + if (slot == null) return this.StatusCode(403, ""); + + RatedLevel? ratedLevel = await this.database.RatedLevels.FirstOrDefaultAsync(r => r.SlotId == slotId && r.UserId == user.UserId); + if (ratedLevel == null) + { + ratedLevel = new(); + ratedLevel.SlotId = slotId; + ratedLevel.UserId = user.UserId; + ratedLevel.RatingLBP1 = 0; + this.database.RatedLevels.Add(ratedLevel); + } + + ratedLevel.Rating = Math.Max(Math.Min(1, rating), -1); + + await this.database.SaveChangesAsync(); + + return this.Ok(); + } + } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Levels/Slot.cs b/ProjectLighthouse/Types/Levels/Slot.cs index c48749bc..d461ff5f 100644 --- a/ProjectLighthouse/Types/Levels/Slot.cs +++ b/ProjectLighthouse/Types/Levels/Slot.cs @@ -139,6 +139,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels [XmlIgnore] public int PlaysLBP3Unique { get; set; } + [NotMapped] [XmlElement("thumbsup")] public int Thumbsup { @@ -149,6 +150,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == 1); } } + + [NotMapped] [XmlElement("thumbsdown")] public int Thumbsdown { @@ -159,6 +162,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels return database.RatedLevels.Count(r => r.SlotId == this.SlotId && r.Rating == -1); } } + + [NotMapped] [XmlElement("averageRating")] public double RatingLBP1 { get { using Database database = new(); @@ -167,8 +172,24 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels if (!ratedLevels.Any()) return 3.0; return Enumerable.Average(ratedLevels, r => r.RatingLBP1); ; - } - } + } + } + + [NotMapped] + [XmlElement("yourRating")] + public double YourRating { get; set; } + + [NotMapped] + [XmlElement("yourDPadRating")] + public int YourDPadRating { get; set; } + + [NotMapped] + [XmlElement("yourLBP1PlayCount")] + public int YourLBP1PlayCount { get; set; } + + [NotMapped] + [XmlElement("yourLBP2PlayCount")] + public int YourLBP2PlayCount { get; set; } public string SerializeResources() { @@ -212,6 +233,10 @@ namespace LBPUnion.ProjectLighthouse.Types.Levels LbpSerializer.StringElement("lbp3UniquePlayCount", this.PlaysLBP3Unique) + LbpSerializer.StringElement("thumbsup", this.Thumbsup) + LbpSerializer.StringElement("thumbsdown", this.Thumbsdown) + + LbpSerializer.StringElement("yourRating", this.YourRating) + + LbpSerializer.StringElement("yourDPadRating", this.YourDPadRating) + + LbpSerializer.StringElement("yourLBP1PlayCount", this.YourLBP1PlayCount) + + LbpSerializer.StringElement("yourLBP2PlayCount", this.YourLBP2PlayCount) + LbpSerializer.StringElement("averageRating", this.RatingLBP1); return LbpSerializer.TaggedStringElement("slot", slotData, "type", "user");