diff --git a/Server/Controllers/LapController.cs b/Server/Controllers/LapController.cs index eb25a85..fb91b82 100644 --- a/Server/Controllers/LapController.cs +++ b/Server/Controllers/LapController.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System.Data.SqlClient; using Server.Models; namespace Server.Controllers; @@ -25,29 +27,6 @@ public class LapController : ControllerBase { }); } - // [HttpGet()] - // public LapStorageDTO UserLaps(Guid userId, string mapName) { - // List laps = _db.Laps - // .Where(l => l.Id == userId) - // .Where(l => l.Map == mapName); - - // int completions = _db.Laps - // .Where(l => l.Map == mapName) - // .Count(); - - // int position = _db.Database.sql(""" - // SELECT - // """); - - // return new LapStorageDTO { - // Map = mapName, - // Laps = laps, - // public int Completions { get; set; } - // public int Position { get; set; } - - // } - // } - [HttpPost()] public async Task Post(LapDTO dto) { Lap existing = _db.Laps diff --git a/Server/Controllers/PbController.cs b/Server/Controllers/PbController.cs new file mode 100644 index 0000000..bfcc6d1 --- /dev/null +++ b/Server/Controllers/PbController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System.Data.SqlClient; +using Server.Models; + +namespace Server.Controllers; + +[ApiController] +[Route("[controller]")] +public class PbController : ControllerBase { + private readonly ILogger _logger; + private SpaceRaceDbContext _db; + + public PbController(ILogger logger, SpaceRaceDbContext db) { + _logger = logger; + _db = db; + } + + [HttpGet()] + public PbDTO Pb(Guid userId, string mapName) { + _logger.LogInformation($"{userId} {mapName}"); + + return _db.PbDTO + .Where(pb => pb.UserId == userId) + .Where(pb => pb.Map == mapName) + .Single(); + } +} diff --git a/Server/Migrations/20230829014321_Beginninging.Designer.cs b/Server/Migrations/20230829030053_Beginninging.Designer.cs similarity index 59% rename from Server/Migrations/20230829014321_Beginninging.Designer.cs rename to Server/Migrations/20230829030053_Beginninging.Designer.cs index 381b6e0..93930c5 100644 --- a/Server/Migrations/20230829014321_Beginninging.Designer.cs +++ b/Server/Migrations/20230829030053_Beginninging.Designer.cs @@ -12,7 +12,7 @@ using Server; namespace Server.Migrations { [DbContext(typeof(SpaceRaceDbContext))] - [Migration("20230829014321_Beginninging")] + [Migration("20230829030053_Beginninging")] partial class Beginninging { /// @@ -56,29 +56,55 @@ namespace Server.Migrations b.HasData( new { - Id = new Guid("c3a3f8d9-01a7-4420-bb57-a8f26baabc4d"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5704), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7826), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 128.0, - UserId = new Guid("9d2c95b9-5ba6-4ff5-afc5-f20f34620d82") + UserId = new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d") }, new { - Id = new Guid("6cb7876e-3059-44ea-af56-1968d653f001"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5742), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7873), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 132.0, - UserId = new Guid("157a119a-e3e2-4623-ac6c-2dda797dd301") + UserId = new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd") }, new { - Id = new Guid("2b649610-1da8-450a-ad24-021626d80c60"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5750), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7881), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 148.0, - UserId = new Guid("7ca95bb5-0145-4c59-86cc-a499b01ba036") + UserId = new Guid("b3a23d2c-ee77-4122-9086-50a884bf49b5") }); }); + + modelBuilder.Entity("Server.Models.PbDTO", b => + { + b.Property("Completions") + .HasColumnType("integer"); + + b.Property("Created") + .HasColumnType("timestamp with time zone"); + + b.Property("Id") + .HasColumnType("uuid"); + + b.Property("Map") + .HasColumnType("text"); + + b.Property("Position") + .HasColumnType("integer"); + + b.Property("Time") + .HasColumnType("double precision"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.ToTable("PbDTO"); + }); #pragma warning restore 612, 618 } } diff --git a/Server/Migrations/20230829014321_Beginninging.cs b/Server/Migrations/20230829030053_Beginninging.cs similarity index 51% rename from Server/Migrations/20230829014321_Beginninging.cs rename to Server/Migrations/20230829030053_Beginninging.cs index 5f7f96a..4116da2 100644 --- a/Server/Migrations/20230829014321_Beginninging.cs +++ b/Server/Migrations/20230829030053_Beginninging.cs @@ -28,14 +28,30 @@ namespace Server.Migrations table.PrimaryKey("PK_Laps", x => x.Id); }); + migrationBuilder.CreateTable( + name: "PbDTO", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + Created = table.Column(type: "timestamp with time zone", nullable: false), + Time = table.Column(type: "double precision", nullable: false), + Map = table.Column(type: "text", nullable: true), + Completions = table.Column(type: "integer", nullable: false), + Position = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + }); + migrationBuilder.InsertData( table: "Laps", columns: new[] { "Id", "Created", "Map", "Time", "UserId" }, values: new object[,] { - { new Guid("2b649610-1da8-450a-ad24-021626d80c60"), new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5750), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 148.0, new Guid("7ca95bb5-0145-4c59-86cc-a499b01ba036") }, - { new Guid("6cb7876e-3059-44ea-af56-1968d653f001"), new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5742), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 132.0, new Guid("157a119a-e3e2-4623-ac6c-2dda797dd301") }, - { new Guid("c3a3f8d9-01a7-4420-bb57-a8f26baabc4d"), new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5704), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 128.0, new Guid("9d2c95b9-5ba6-4ff5-afc5-f20f34620d82") } + { new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"), new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7873), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 132.0, new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd") }, + { new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"), new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7881), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 148.0, new Guid("b3a23d2c-ee77-4122-9086-50a884bf49b5") }, + { new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"), new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7826), new TimeSpan(0, 0, 0, 0, 0)), "sr_TestTrack", 128.0, new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d") } }); migrationBuilder.CreateIndex( @@ -55,6 +71,9 @@ namespace Server.Migrations { migrationBuilder.DropTable( name: "Laps"); + + migrationBuilder.DropTable( + name: "PbDTO"); } } } diff --git a/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs b/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs index 8b8f412..889618c 100644 --- a/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs +++ b/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs @@ -53,29 +53,55 @@ namespace Server.Migrations b.HasData( new { - Id = new Guid("c3a3f8d9-01a7-4420-bb57-a8f26baabc4d"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5704), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7826), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 128.0, - UserId = new Guid("9d2c95b9-5ba6-4ff5-afc5-f20f34620d82") + UserId = new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d") }, new { - Id = new Guid("6cb7876e-3059-44ea-af56-1968d653f001"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5742), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7873), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 132.0, - UserId = new Guid("157a119a-e3e2-4623-ac6c-2dda797dd301") + UserId = new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd") }, new { - Id = new Guid("2b649610-1da8-450a-ad24-021626d80c60"), - Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5750), new TimeSpan(0, 0, 0, 0, 0)), + Id = new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"), + Created = new DateTimeOffset(new DateTime(2023, 8, 29, 3, 0, 53, 704, DateTimeKind.Unspecified).AddTicks(7881), new TimeSpan(0, 0, 0, 0, 0)), Map = "sr_TestTrack", Time = 148.0, - UserId = new Guid("7ca95bb5-0145-4c59-86cc-a499b01ba036") + UserId = new Guid("b3a23d2c-ee77-4122-9086-50a884bf49b5") }); }); + + modelBuilder.Entity("Server.Models.PbDTO", b => + { + b.Property("Completions") + .HasColumnType("integer"); + + b.Property("Created") + .HasColumnType("timestamp with time zone"); + + b.Property("Id") + .HasColumnType("uuid"); + + b.Property("Map") + .HasColumnType("text"); + + b.Property("Position") + .HasColumnType("integer"); + + b.Property("Time") + .HasColumnType("double precision"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.ToTable("PbDTO"); + }); #pragma warning restore 612, 618 } } diff --git a/Server/Models/PbDTO.cs b/Server/Models/PbDTO.cs index 226433a..bc709a3 100644 --- a/Server/Models/PbDTO.cs +++ b/Server/Models/PbDTO.cs @@ -3,6 +3,7 @@ namespace Server.Models; public class PbDTO { public Guid Id { get; set; } public Guid UserId { get; set; } + public DateTimeOffset Created { get; set; } public double Time { get; set; } public string Map { get; set; } public int Completions { get; set; } diff --git a/Server/Repositories/SpaceRaceDbContext.cs b/Server/Repositories/SpaceRaceDbContext.cs index 14c39c0..94690df 100644 --- a/Server/Repositories/SpaceRaceDbContext.cs +++ b/Server/Repositories/SpaceRaceDbContext.cs @@ -1,15 +1,32 @@ using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; +using Server.Models; + namespace Server; public class SpaceRaceDbContext : DbContext { public DbSet Laps { get; set; } + public DbSet PbDTO { get; set; } + public SpaceRaceDbContext(DbContextOptions options) : base(options) {} protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); new SpaceRaceInitialiser(modelBuilder).Seed(); + + modelBuilder.Entity(pb => { + var pbQuery = $$""" + WITH ranked AS ( + SELECT *, rank() OVER (ORDER BY "Time" ASC) AS "Position", COUNT("Id") OVER () AS "Completions" + FROM "Laps" + ) + + SELECT * FROM ranked + """; + pb.HasNoKey(); + pb.ToSqlQuery(pbQuery); + }); } }