mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-21 18:00:56 +00:00
commit
4d4227957d
5 changed files with 102 additions and 14 deletions
17
DatabaseMigrations/7.sql
Normal file
17
DatabaseMigrations/7.sql
Normal file
|
@ -0,0 +1,17 @@
|
|||
create table QueuedLevels
|
||||
(
|
||||
QueuedLevelId int,
|
||||
UserId int not null,
|
||||
SlotId int not null
|
||||
);
|
||||
|
||||
create unique index QueuedLevels_QueuedLevelId_uindex
|
||||
on QueuedLevels (QueuedLevelId);
|
||||
|
||||
alter table QueuedLevels
|
||||
add constraint QueuedLevels_pk
|
||||
primary key (QueuedLevelId);
|
||||
|
||||
alter table QueuedLevels
|
||||
modify QueuedLevelId int auto_increment;
|
||||
|
63
ProjectLighthouse/Controllers/LevelQueueController.cs
Normal file
63
ProjectLighthouse/Controllers/LevelQueueController.cs
Normal file
|
@ -0,0 +1,63 @@
|
|||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ProjectLighthouse.Serialization;
|
||||
using ProjectLighthouse.Types;
|
||||
|
||||
namespace ProjectLighthouse.Controllers {
|
||||
[ApiController]
|
||||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/xml")]
|
||||
public class LevelQueueController : ControllerBase {
|
||||
[HttpGet("slots/lolcatftw/{username}")]
|
||||
public IActionResult GetLevelQueue(string username) {
|
||||
IEnumerable<QueuedLevel> queuedLevels = new Database().QueuedLevels
|
||||
.Include(q => q.User)
|
||||
.Include(q => q.Slot)
|
||||
.Where(q => q.User.Username == username)
|
||||
.AsEnumerable();
|
||||
|
||||
string response = queuedLevels.Aggregate(string.Empty, (current, q) => current + q.Slot.Serialize());
|
||||
|
||||
return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", 1));
|
||||
}
|
||||
|
||||
[HttpPost("lolcatftw/remove/user/{id:int}")]
|
||||
public async Task<IActionResult> RemoveQueuedLevel(int id) {
|
||||
await using Database database = new();
|
||||
|
||||
User? user = await database.UserFromRequest(this.Request);
|
||||
if(user == null) return this.StatusCode(403, "");
|
||||
|
||||
QueuedLevel queuedLevel = await database.QueuedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id);
|
||||
if(queuedLevel != null) database.QueuedLevels.Remove(queuedLevel);
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return this.Ok();
|
||||
}
|
||||
|
||||
[HttpPost("lolcatftw/add/user/{id:int}")]
|
||||
public async Task<IActionResult> AddQueuedLevel(int id) {
|
||||
await using Database database = new();
|
||||
|
||||
User? user = await database.UserFromRequest(this.Request);
|
||||
if(user == null) return this.StatusCode(403, "");
|
||||
|
||||
QueuedLevel queuedLevel = await database.QueuedLevels.FirstOrDefaultAsync(q => q.UserId == user.UserId && q.SlotId == id);
|
||||
if(queuedLevel != null) return this.Ok();
|
||||
|
||||
database.QueuedLevels.Add(new QueuedLevel {
|
||||
SlotId = id,
|
||||
UserId = user.UserId
|
||||
});
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return this.Ok();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,8 +25,7 @@ namespace ProjectLighthouse.Controllers {
|
|||
|
||||
return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", 1));
|
||||
}
|
||||
|
||||
[HttpGet("s/user/{id:int}")]
|
||||
|
||||
public async Task<IActionResult> SUser(int id) {
|
||||
Slot slot = await this.database.Slots
|
||||
.Include(s => s.Creator)
|
||||
|
@ -35,16 +34,5 @@ namespace ProjectLighthouse.Controllers {
|
|||
|
||||
return this.Ok(slot.Serialize());
|
||||
}
|
||||
|
||||
[HttpGet("slots/lolcatftw/{username}")]
|
||||
public IActionResult SlotsLolCat(string username) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,3 @@
|
|||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
@ -10,6 +9,7 @@ namespace ProjectLighthouse {
|
|||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<Location> Locations { get; set; }
|
||||
public DbSet<Slot> Slots { get; set; }
|
||||
public DbSet<QueuedLevel> QueuedLevels { get; set; }
|
||||
public DbSet<Comment> Comments { get; set; }
|
||||
public DbSet<Token> Tokens { get; set; }
|
||||
|
||||
|
@ -38,6 +38,7 @@ namespace ProjectLighthouse {
|
|||
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
public async Task<Token?> AuthenticateUser(LoginData loginData) {
|
||||
// TODO: don't use psn name to authenticate
|
||||
User user = await this.Users.FirstOrDefaultAsync(u => u.Username == loginData.Username)
|
||||
|
@ -71,5 +72,6 @@ namespace ProjectLighthouse {
|
|||
|
||||
return await UserFromAuthToken(mmAuth);
|
||||
}
|
||||
#nullable disable
|
||||
}
|
||||
}
|
18
ProjectLighthouse/Types/QueuedLevel.cs
Normal file
18
ProjectLighthouse/Types/QueuedLevel.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace ProjectLighthouse.Types {
|
||||
public class QueuedLevel {
|
||||
[Key] public int QueuedLevelId { get; set; }
|
||||
|
||||
public int UserId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(UserId))]
|
||||
public User User { get; set; }
|
||||
|
||||
public int SlotId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(SlotId))]
|
||||
public Slot Slot { get; set; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue