mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-20 08:12:27 +00:00
Switch to ForeignKeys and dependency injection for EF
This commit is contained in:
parent
b366a41f90
commit
d8e34bcf8c
13 changed files with 90 additions and 76 deletions
|
@ -13,11 +13,17 @@ namespace ProjectLighthouse.Controllers {
|
|||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/xml")]
|
||||
public class CommentController : ControllerBase {
|
||||
private readonly Database database;
|
||||
public CommentController(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("userComments/{username}")]
|
||||
public async Task<IActionResult> GetComments(string username) {
|
||||
// the following is downright retarded, but its 12:48am and i do not give a shit
|
||||
// ↓ ok... ↓ why does this need to be wrapped ↓ again???? whyyyy
|
||||
List<Comment> comments = (await new Database().Comments.ToListAsync()).Where(c => c.TargetUsername == username).ToList();
|
||||
List<Comment> comments = await database.Comments
|
||||
.Include(c => c.Target)
|
||||
.Where(c => c.Target.Username == username)
|
||||
.ToListAsync();
|
||||
|
||||
string outputXml = comments.Aggregate(string.Empty, (current, comment) => current + comment.Serialize());
|
||||
return this.Ok(LbpSerializer.StringElement("comments", outputXml));
|
||||
|
@ -31,7 +37,6 @@ namespace ProjectLighthouse.Controllers {
|
|||
XmlSerializer serializer = new(typeof(Comment));
|
||||
Comment comment = (Comment)serializer.Deserialize(new StringReader(bodyString));
|
||||
|
||||
await using Database database = new();
|
||||
User poster = await database.UserFromRequest(Request);
|
||||
|
||||
if(poster == null) return this.StatusCode(403, "");
|
||||
|
|
|
@ -12,6 +12,12 @@ namespace ProjectLighthouse.Controllers {
|
|||
[Route("LITTLEBIGPLANETPS3_XML/login")]
|
||||
[Produces("text/xml")]
|
||||
public class LoginController : ControllerBase {
|
||||
private readonly Database database;
|
||||
|
||||
public LoginController(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Login() {
|
||||
if(!this.Request.Query.TryGetValue("titleID", out StringValues _))
|
||||
|
@ -27,8 +33,6 @@ namespace ProjectLighthouse.Controllers {
|
|||
return this.BadRequest();
|
||||
}
|
||||
|
||||
await using Database database = new();
|
||||
|
||||
Token? token = await database.AuthenticateUser(loginData);
|
||||
|
||||
if(token == null) return this.StatusCode(403, "");
|
||||
|
|
|
@ -8,9 +8,14 @@ namespace ProjectLighthouse.Controllers {
|
|||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/plain")]
|
||||
public class MessageController : ControllerBase {
|
||||
private readonly Database database;
|
||||
public MessageController(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("eula")]
|
||||
public async Task<IActionResult> Eula() {
|
||||
User user = await new Database().UserFromRequest(Request);
|
||||
User user = await this.database.UserFromRequest(Request);
|
||||
return user == null ? this.StatusCode(403, "") : this.Ok($"You are logged in as user {user.Username} (id {user.UserId})");
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,12 @@ namespace ProjectLighthouse.Controllers {
|
|||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/xml")]
|
||||
public class PublishController : ControllerBase {
|
||||
private readonly Database database;
|
||||
|
||||
public PublishController(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Endpoint the game uses to verify that the level is compatible (?)
|
||||
/// </summary>
|
||||
|
@ -27,8 +33,6 @@ namespace ProjectLighthouse.Controllers {
|
|||
/// </summary>
|
||||
[HttpPost("publish")]
|
||||
public async Task<IActionResult> Publish() {
|
||||
await using Database database = new();
|
||||
|
||||
User user = await database.UserFromRequest(Request);
|
||||
if(user == null) return this.StatusCode(403, "");
|
||||
|
||||
|
@ -52,8 +56,6 @@ namespace ProjectLighthouse.Controllers {
|
|||
|
||||
[HttpPost("unpublish/{id:int}")]
|
||||
public async Task<IActionResult> Unpublish(int id) {
|
||||
await using Database database = new();
|
||||
|
||||
Slot slot = await database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
|
||||
|
||||
database.Locations.Remove(slot.Location);
|
||||
|
|
|
@ -10,23 +10,39 @@ namespace ProjectLighthouse.Controllers {
|
|||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/xml")]
|
||||
public class SlotsController : ControllerBase {
|
||||
private readonly Database database;
|
||||
public SlotsController(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("slots/by")]
|
||||
public IActionResult SlotsBy() {
|
||||
string response = Enumerable.Aggregate(new Database().Slots, string.Empty, (current, slot) => current + slot.Serialize());
|
||||
string response = Enumerable.Aggregate(
|
||||
database.Slots
|
||||
.Include(s => s.Creator)
|
||||
.Include(s => s.Location)
|
||||
, string.Empty, (current, slot) => current + slot.Serialize());
|
||||
|
||||
return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", 1));
|
||||
}
|
||||
|
||||
[HttpGet("s/user/{id:int}")]
|
||||
public async Task<IActionResult> SUser(int id) {
|
||||
Slot slot = await new Database().Slots.FirstOrDefaultAsync(s => s.SlotId == id);
|
||||
Slot slot = await this.database.Slots
|
||||
.Include(s => s.Creator)
|
||||
.Include(s => s.Location)
|
||||
.FirstOrDefaultAsync(s => s.SlotId == id);
|
||||
|
||||
return this.Ok(slot.Serialize());
|
||||
}
|
||||
|
||||
[HttpGet("slots/lolcatftw/{username}")]
|
||||
public IActionResult SlotsLolCat(string username) {
|
||||
string response = Enumerable.Aggregate(new Database().Slots, string.Empty, (current, slot) => current + slot.Serialize());
|
||||
string response = Enumerable.Aggregate(
|
||||
database.Slots
|
||||
.Include(s => s.Creator)
|
||||
.Include(s => s.Location)
|
||||
, string.Empty, (current, slot) => current + slot.Serialize());
|
||||
|
||||
return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", 1));
|
||||
}
|
||||
|
|
|
@ -12,11 +12,16 @@ namespace ProjectLighthouse.Controllers {
|
|||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/xml")]
|
||||
public class UserController : ControllerBase {
|
||||
private readonly Database database;
|
||||
public UserController(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("user/{username}")]
|
||||
public async Task<IActionResult> GetUser(string username) {
|
||||
await using Database database = new();
|
||||
|
||||
User user = await database.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||
User user = await database.Users
|
||||
.Include(u => u.Location)
|
||||
.FirstOrDefaultAsync(u => u.Username == username);
|
||||
|
||||
if(user == null) return this.NotFound();
|
||||
return this.Ok(user.Serialize());
|
||||
|
@ -30,7 +35,6 @@ namespace ProjectLighthouse.Controllers {
|
|||
|
||||
[HttpPost("updateUser")]
|
||||
public async Task<IActionResult> UpdateUser() {
|
||||
await using Database database = new();
|
||||
User user = await database.UserFromRequest(Request);
|
||||
|
||||
if(user == null) return this.StatusCode(403, "");
|
||||
|
|
|
@ -60,7 +60,9 @@ namespace ProjectLighthouse {
|
|||
public async Task<User?> UserFromAuthToken(string authToken) {
|
||||
Token? token = await Tokens.FirstOrDefaultAsync(t => t.UserToken == authToken);
|
||||
if(token == null) return null;
|
||||
return await Users.FirstOrDefaultAsync(u => u.UserId == token.UserId);
|
||||
return await Users
|
||||
.Include(u => u.Location)
|
||||
.FirstOrDefaultAsync(u => u.UserId == token.UserId);
|
||||
}
|
||||
|
||||
public async Task<User?> UserFromRequest(HttpRequest request) {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using ProjectLighthouse.Types;
|
||||
|
||||
|
@ -9,13 +11,13 @@ namespace ProjectLighthouse {
|
|||
Console.WriteLine("Welcome to Project Lighthouse!");
|
||||
Console.WriteLine(ServerSettings.DbConnected ? "Connected to the database." : "Database unavailable. Starting in stateless mode.");
|
||||
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
IHostBuilder builder = Host.CreateDefaultBuilder(args);
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder => {
|
||||
builder.ConfigureWebHostDefaults(webBuilder => {
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
|
||||
builder.Build().Run();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,8 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.10" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.11" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -3,10 +3,12 @@ using System.IO;
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using ProjectLighthouse.Serialization;
|
||||
using ProjectLighthouse.Types;
|
||||
|
||||
namespace ProjectLighthouse {
|
||||
public class Startup {
|
||||
|
@ -18,10 +20,11 @@ namespace ProjectLighthouse {
|
|||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services) {
|
||||
|
||||
services.AddControllers();
|
||||
services.AddMvc(options =>
|
||||
options.OutputFormatters.Add(new XmlOutputFormatter()));
|
||||
|
||||
services.AddDbContext<Database>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
using ProjectLighthouse.Serialization;
|
||||
|
@ -14,25 +15,11 @@ namespace ProjectLighthouse.Types {
|
|||
|
||||
public int TargetUserId { get; set; }
|
||||
|
||||
private string posterUsername;
|
||||
[ForeignKey(nameof(PosterUserId))]
|
||||
public User Poster { get; set; }
|
||||
|
||||
// [XmlAttribute("username")]
|
||||
public string PosterUsername {
|
||||
get {
|
||||
if(this.posterUsername != null) return this.posterUsername;
|
||||
return this.posterUsername = new Database().Users.First(u => u.UserId == PosterUserId).Username;
|
||||
}
|
||||
}
|
||||
|
||||
private string targetUsername;
|
||||
|
||||
public string TargetUsername {
|
||||
get {
|
||||
if(this.targetUsername != null) return this.targetUsername;
|
||||
|
||||
return this.targetUsername = new Database().Users.First(u => u.UserId == TargetUserId).Username;
|
||||
}
|
||||
}
|
||||
[ForeignKey(nameof(TargetUserId))]
|
||||
public User Target { get; set; }
|
||||
|
||||
public long Timestamp { get; set; }
|
||||
|
||||
|
@ -43,7 +30,7 @@ namespace ProjectLighthouse.Types {
|
|||
|
||||
private string serialize() {
|
||||
return LbpSerializer.StringElement("id", CommentId) +
|
||||
LbpSerializer.StringElement("npHandle", this.PosterUsername) +
|
||||
LbpSerializer.StringElement("npHandle", this.Poster.Username) +
|
||||
LbpSerializer.StringElement("timestamp", Timestamp) +
|
||||
LbpSerializer.StringElement("message", Message) +
|
||||
LbpSerializer.StringElement("thumbsup", ThumbsUp) +
|
||||
|
|
|
@ -2,6 +2,7 @@ using System.ComponentModel.DataAnnotations;
|
|||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using ProjectLighthouse.Serialization;
|
||||
|
||||
namespace ProjectLighthouse.Types {
|
||||
|
@ -40,29 +41,15 @@ namespace ProjectLighthouse.Types {
|
|||
[XmlIgnore]
|
||||
public int CreatorId { get; set; }
|
||||
|
||||
private User creator;
|
||||
|
||||
public User Creator {
|
||||
get {
|
||||
if(this.creator != null) return this.creator;
|
||||
|
||||
return creator = new Database().Users.First(u => u.UserId == CreatorId);
|
||||
}
|
||||
}
|
||||
|
||||
private Location location;
|
||||
[ForeignKey(nameof(CreatorId))]
|
||||
public User Creator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The location of the level on the creator's earth
|
||||
/// </summary>
|
||||
[XmlElement("location")]
|
||||
public Location Location {
|
||||
get {
|
||||
if(location != null) return this.location;
|
||||
|
||||
return location = new Database().Locations.First(l => l.Id == LocationId);
|
||||
}
|
||||
}
|
||||
[ForeignKey(nameof(LocationId))]
|
||||
public Location Location { get; set; }
|
||||
|
||||
[XmlElement("initiallyLocked")]
|
||||
public bool InitiallyLocked { get; set; }
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using ProjectLighthouse.Serialization;
|
||||
|
||||
|
@ -24,16 +25,11 @@ namespace ProjectLighthouse.Types {
|
|||
|
||||
public int LocationId { get; set; }
|
||||
|
||||
private Location location;
|
||||
/// <summary>
|
||||
/// The location of the profile card on the user's earth
|
||||
/// </summary>
|
||||
public Location Location {
|
||||
get {
|
||||
if(location != null) return this.location;
|
||||
return location = new Database().Locations.First(l => l.Id == LocationId);
|
||||
}
|
||||
}
|
||||
[ForeignKey("LocationId")]
|
||||
public Location Location { get; set; }
|
||||
|
||||
public int FavouriteSlotCount { get; set; }
|
||||
public int FavouriteUserCount { get; set; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue