mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-19 11:41:30 +00:00
Add simple api
This commit is contained in:
parent
2fee6c62e1
commit
219e02d6cb
12 changed files with 210 additions and 3 deletions
27
ProjectLighthouse/Controllers/Api/GetSlotEndpoint.cs
Normal file
27
ProjectLighthouse/Controllers/Api/GetSlotEndpoint.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Controllers.Api;
|
||||
|
||||
public class GetSlotEndpoint : ApiEndpoint
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public GetSlotEndpoint(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("slot/{id:int}")]
|
||||
public async Task<IActionResult> OnGet(int id)
|
||||
{
|
||||
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(u => u.SlotId == id);
|
||||
if (slot == null) return this.NotFound();
|
||||
|
||||
return this.Ok(slot);
|
||||
}
|
||||
}
|
32
ProjectLighthouse/Controllers/Api/GetSlotsEndpoint.cs
Normal file
32
ProjectLighthouse/Controllers/Api/GetSlotsEndpoint.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Controllers.Api;
|
||||
|
||||
public class GetSlotsEndpoint : ApiEndpoint
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public GetSlotsEndpoint(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("slots")]
|
||||
public async Task<IActionResult> OnGet([FromQuery] int limit = 20, [FromQuery] int skip = 0)
|
||||
{
|
||||
limit = Math.Min(ServerStatics.PageSize, limit);
|
||||
|
||||
IEnumerable<MinimalSlot> minimalSlots = (await this.database.Slots.OrderByDescending(s => s.FirstUploaded).Skip(skip).Take(limit).ToListAsync()).Select
|
||||
(MinimalSlot.FromSlot);
|
||||
|
||||
return this.Ok(minimalSlots);
|
||||
}
|
||||
}
|
26
ProjectLighthouse/Controllers/Api/GetUserEndpoint.cs
Normal file
26
ProjectLighthouse/Controllers/Api/GetUserEndpoint.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Controllers.Api;
|
||||
|
||||
public class GetUserEndpoint : ApiEndpoint
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public GetUserEndpoint(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("user/{id:int}")]
|
||||
public async Task<IActionResult> OnGet(int id)
|
||||
{
|
||||
User? user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
|
||||
if (user == null) return this.NotFound();
|
||||
|
||||
return this.Ok(user);
|
||||
}
|
||||
}
|
24
ProjectLighthouse/Controllers/Api/StatisticsEndpoint.cs
Normal file
24
ProjectLighthouse/Controllers/Api/StatisticsEndpoint.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Api;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Controllers.Api;
|
||||
|
||||
public class StatisticsEndpoint : ApiEndpoint
|
||||
{
|
||||
[HttpGet("statistics")]
|
||||
public async Task<IActionResult> OnGet()
|
||||
=> this.Ok
|
||||
(
|
||||
new StatisticsResponse
|
||||
{
|
||||
Photos = await StatisticsHelper.PhotoCount(),
|
||||
Slots = await StatisticsHelper.SlotCount(),
|
||||
Users = await StatisticsHelper.UserCount(),
|
||||
RecentMatches = await StatisticsHelper.RecentMatches(),
|
||||
TeamPicks = await StatisticsHelper.TeamPickCount(),
|
||||
}
|
||||
);
|
||||
}
|
|
@ -177,7 +177,7 @@ public class SlotsController : ControllerBase
|
|||
"hint_start", pageStart + Math.Min(pageSize, ServerSettings.Instance.EntitledSlots)
|
||||
},
|
||||
{
|
||||
"total", await StatisticsHelper.MMPicksCount()
|
||||
"total", await StatisticsHelper.TeamPickCount()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
|
|
@ -24,7 +24,7 @@ public class StatisticsController : ControllerBase
|
|||
public async Task<IActionResult> PlanetStats()
|
||||
{
|
||||
int totalSlotCount = await StatisticsHelper.SlotCount();
|
||||
int mmPicksCount = await StatisticsHelper.MMPicksCount();
|
||||
int mmPicksCount = await StatisticsHelper.TeamPickCount();
|
||||
|
||||
return this.Ok
|
||||
(
|
||||
|
|
|
@ -14,7 +14,7 @@ public static class StatisticsHelper
|
|||
|
||||
public static async Task<int> UserCount() => await database.Users.CountAsync(u => !u.Banned);
|
||||
|
||||
public static async Task<int> MMPicksCount() => await database.Slots.CountAsync(s => s.TeamPick);
|
||||
public static async Task<int> TeamPickCount() => await database.Slots.CountAsync(s => s.TeamPick);
|
||||
|
||||
public static async Task<int> PhotoCount() => await database.Photos.CountAsync();
|
||||
}
|
10
ProjectLighthouse/Types/Api/StatisticsResponse.cs
Normal file
10
ProjectLighthouse/Types/Api/StatisticsResponse.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace LBPUnion.ProjectLighthouse.Types.Api;
|
||||
|
||||
public class StatisticsResponse
|
||||
{
|
||||
public int RecentMatches { get; set; }
|
||||
public int Slots { get; set; }
|
||||
public int Users { get; set; }
|
||||
public int TeamPicks { get; set; }
|
||||
public int Photos { get; set; }
|
||||
}
|
9
ProjectLighthouse/Types/ApiEndpoint.cs
Normal file
9
ProjectLighthouse/Types/ApiEndpoint.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Types;
|
||||
|
||||
[ApiController]
|
||||
[Route("/api/v1/")]
|
||||
[Produces("application/json")]
|
||||
public class ApiEndpoint : ControllerBase
|
||||
{}
|
26
ProjectLighthouse/Types/Levels/MinimalSlot.cs
Normal file
26
ProjectLighthouse/Types/Levels/MinimalSlot.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
namespace LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
|
||||
public struct MinimalSlot
|
||||
{
|
||||
public int SlotId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string IconHash { get; set; }
|
||||
public bool TeamPick { get; set; }
|
||||
public GameVersion GameVersion { get; set; }
|
||||
#if DEBUG
|
||||
public long FirstUploaded { get; set; }
|
||||
#endif
|
||||
|
||||
public static MinimalSlot FromSlot(Slot slot)
|
||||
=> new()
|
||||
{
|
||||
SlotId = slot.SlotId,
|
||||
Name = slot.Name,
|
||||
IconHash = slot.IconHash,
|
||||
TeamPick = slot.TeamPick,
|
||||
GameVersion = slot.GameVersion,
|
||||
#if DEBUG
|
||||
FirstUploaded = slot.FirstUploaded,
|
||||
#endif
|
||||
};
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
|
@ -19,6 +20,7 @@ public class Slot
|
|||
{
|
||||
[XmlAttribute("type")]
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public string Type { get; set; } = "user";
|
||||
|
||||
[Key]
|
||||
|
@ -35,24 +37,29 @@ public class Slot
|
|||
public string IconHash { get; set; } = "";
|
||||
|
||||
[XmlElement("rootLevel")]
|
||||
[JsonIgnore]
|
||||
public string RootLevel { get; set; } = "";
|
||||
|
||||
[JsonIgnore]
|
||||
public string ResourceCollection { get; set; } = "";
|
||||
|
||||
[NotMapped]
|
||||
[XmlElement("resource")]
|
||||
[JsonIgnore]
|
||||
public string[] Resources {
|
||||
get => this.ResourceCollection.Split(",");
|
||||
set => this.ResourceCollection = string.Join(',', value);
|
||||
}
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int LocationId { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
public int CreatorId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(CreatorId))]
|
||||
[JsonIgnore]
|
||||
public User? Creator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
@ -60,6 +67,7 @@ public class Slot
|
|||
/// </summary>
|
||||
[XmlElement("location")]
|
||||
[ForeignKey(nameof(LocationId))]
|
||||
[JsonIgnore]
|
||||
public Location? Location { get; set; }
|
||||
|
||||
[XmlElement("initiallyLocked")]
|
||||
|
@ -78,6 +86,7 @@ public class Slot
|
|||
public string AuthorLabels { get; set; } = "";
|
||||
|
||||
[XmlElement("background")]
|
||||
[JsonIgnore]
|
||||
public string BackgroundHash { get; set; } = "";
|
||||
|
||||
[XmlElement("minPlayers")]
|
||||
|
@ -103,6 +112,7 @@ public class Slot
|
|||
|
||||
[XmlIgnore]
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int Hearts {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -124,42 +134,55 @@ public class Slot
|
|||
public int PlaysComplete => this.PlaysLBP1Complete + this.PlaysLBP2Complete + this.PlaysLBP3Complete + this.PlaysLBPVitaComplete;
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP1 { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP1Complete { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP1Unique { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP2 { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP2Complete { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP2Unique { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP3 { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP3Complete { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBP3Unique { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBPVita { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBPVitaComplete { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
[JsonIgnore]
|
||||
public int PlaysLBPVitaUnique { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
[XmlElement("thumbsup")]
|
||||
public int Thumbsup {
|
||||
get {
|
||||
|
@ -170,6 +193,7 @@ public class Slot
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
[XmlElement("thumbsdown")]
|
||||
public int Thumbsdown {
|
||||
get {
|
||||
|
@ -180,6 +204,7 @@ public class Slot
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonPropertyName("averageRating")]
|
||||
[XmlElement("averageRating")]
|
||||
public double RatingLBP1 {
|
||||
get {
|
||||
|
@ -193,6 +218,7 @@ public class Slot
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
[XmlElement("reviewCount")]
|
||||
public int ReviewCount {
|
||||
get {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||
|
@ -13,11 +14,17 @@ public class User
|
|||
public readonly ClientsConnected ClientsConnected = new();
|
||||
public int UserId { get; set; }
|
||||
public string Username { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public string Password { get; set; }
|
||||
|
||||
public string IconHash { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public int Game { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int Lists => 0;
|
||||
|
||||
/// <summary>
|
||||
|
@ -26,6 +33,7 @@ public class User
|
|||
public string Biography { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public string WebsiteAvatarHash {
|
||||
get {
|
||||
string avatarHash = this.IconHash;
|
||||
|
@ -40,6 +48,7 @@ public class User
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int Reviews {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -48,6 +57,7 @@ public class User
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int Comments {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -56,6 +66,7 @@ public class User
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int PhotosByMe {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -64,6 +75,7 @@ public class User
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int PhotosWithMe {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -71,15 +83,18 @@ public class User
|
|||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public int LocationId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The location of the profile card on the user's earth
|
||||
/// </summary>
|
||||
[ForeignKey("LocationId")]
|
||||
[JsonIgnore]
|
||||
public Location Location { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int HeartedLevels {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -88,6 +103,7 @@ public class User
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int HeartedUsers {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -96,6 +112,7 @@ public class User
|
|||
}
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int QueuedLevels {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -103,10 +120,13 @@ public class User
|
|||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public string Pins { get; set; } = "";
|
||||
|
||||
[JsonIgnore]
|
||||
public string PlanetHash { get; set; } = "";
|
||||
|
||||
[JsonIgnore]
|
||||
public int Hearts {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -115,8 +135,10 @@ public class User
|
|||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public bool IsAdmin { get; set; } = false;
|
||||
|
||||
[JsonIgnore]
|
||||
public bool PasswordResetRequired { get; set; }
|
||||
|
||||
public string YayHash { get; set; } = "";
|
||||
|
@ -125,6 +147,7 @@ public class User
|
|||
|
||||
#nullable enable
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public string Status {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -139,8 +162,10 @@ public class User
|
|||
}
|
||||
#nullable disable
|
||||
|
||||
[JsonIgnore]
|
||||
public bool Banned { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public string BannedReason { get; set; }
|
||||
|
||||
public string Serialize(GameVersion gameVersion = GameVersion.LittleBigPlanet1)
|
||||
|
@ -178,6 +203,7 @@ public class User
|
|||
/// The number of used slots on the earth
|
||||
/// </summary>
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int UsedSlots {
|
||||
get {
|
||||
using Database database = new();
|
||||
|
@ -194,6 +220,7 @@ public class User
|
|||
/// <summary>
|
||||
/// The number of slots remaining on the earth
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public int FreeSlots => ServerSettings.Instance.EntitledSlots - this.UsedSlots;
|
||||
|
||||
private static readonly string[] slotTypes =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue