returning pb

This commit is contained in:
ntr committed 2023-08-29 13:52:19 +10:00
1 parent 9f857aeeda
commit 0b2d4ccd14
7 files changed
+141 -45

No files matched your search

+2 -23
View File
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Data.SqlClient;
using Server.Models; using Server.Models;
namespace Server.Controllers; namespace Server.Controllers;
@@ -25,29 +27,6 @@ public class LapController : ControllerBase {
}); });
} }
// [HttpGet()]
// public LapStorageDTO UserLaps(Guid userId, string mapName) {
// List<Lap> 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<Laps>.sql("""
// SELECT
// """);
// return new LapStorageDTO {
// Map = mapName,
// Laps = laps,
// public int Completions { get; set; }
// public int Position { get; set; }
// }
// }
[HttpPost()] [HttpPost()]
public async Task<Lap> Post(LapDTO dto) { public async Task<Lap> Post(LapDTO dto) {
Lap existing = _db.Laps Lap existing = _db.Laps
+28
View File
@@ -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<PbController> _logger;
private SpaceRaceDbContext _db;
public PbController(ILogger<PbController> 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();
}
}
@@ -12,7 +12,7 @@ using Server;
namespace Server.Migrations namespace Server.Migrations
{ {
[DbContext(typeof(SpaceRaceDbContext))] [DbContext(typeof(SpaceRaceDbContext))]
[Migration("20230829014321_Beginninging")] [Migration("20230829030053_Beginninging")]
partial class Beginninging partial class Beginninging
{ {
/// <inheritdoc /> /// <inheritdoc />
@@ -56,29 +56,55 @@ namespace Server.Migrations
b.HasData( b.HasData(
new new
{ {
Id = new Guid("c3a3f8d9-01a7-4420-bb57-a8f26baabc4d"), Id = new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5704), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 128.0, Time = 128.0,
UserId = new Guid("9d2c95b9-5ba6-4ff5-afc5-f20f34620d82") UserId = new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d")
}, },
new new
{ {
Id = new Guid("6cb7876e-3059-44ea-af56-1968d653f001"), Id = new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5742), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 132.0, Time = 132.0,
UserId = new Guid("157a119a-e3e2-4623-ac6c-2dda797dd301") UserId = new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd")
}, },
new new
{ {
Id = new Guid("2b649610-1da8-450a-ad24-021626d80c60"), Id = new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5750), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 148.0, 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<int>("Completions")
.HasColumnType("integer");
b.Property<DateTimeOffset>("Created")
.HasColumnType("timestamp with time zone");
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<string>("Map")
.HasColumnType("text");
b.Property<int>("Position")
.HasColumnType("integer");
b.Property<double>("Time")
.HasColumnType("double precision");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.ToTable("PbDTO");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }
@@ -28,14 +28,30 @@ namespace Server.Migrations
table.PrimaryKey("PK_Laps", x => x.Id); table.PrimaryKey("PK_Laps", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "PbDTO",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
Created = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
Time = table.Column<double>(type: "double precision", nullable: false),
Map = table.Column<string>(type: "text", nullable: true),
Completions = table.Column<int>(type: "integer", nullable: false),
Position = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
});
migrationBuilder.InsertData( migrationBuilder.InsertData(
table: "Laps", table: "Laps",
columns: new[] { "Id", "Created", "Map", "Time", "UserId" }, columns: new[] { "Id", "Created", "Map", "Time", "UserId" },
values: new object[,] 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("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("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("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("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("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( migrationBuilder.CreateIndex(
@@ -55,6 +71,9 @@ namespace Server.Migrations
{ {
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Laps"); name: "Laps");
migrationBuilder.DropTable(
name: "PbDTO");
} }
} }
} }
@@ -53,29 +53,55 @@ namespace Server.Migrations
b.HasData( b.HasData(
new new
{ {
Id = new Guid("c3a3f8d9-01a7-4420-bb57-a8f26baabc4d"), Id = new Guid("dd85f455-658c-4e39-b77e-a8f227c3d0cf"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5704), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 128.0, Time = 128.0,
UserId = new Guid("9d2c95b9-5ba6-4ff5-afc5-f20f34620d82") UserId = new Guid("a6e068ac-5aaa-48f2-adf5-ca30963d2f9d")
}, },
new new
{ {
Id = new Guid("6cb7876e-3059-44ea-af56-1968d653f001"), Id = new Guid("be83b30b-15f0-4013-95c1-d4c71ac602f9"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5742), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 132.0, Time = 132.0,
UserId = new Guid("157a119a-e3e2-4623-ac6c-2dda797dd301") UserId = new Guid("3620fbda-a45a-4eec-824b-9a6c77e9dbfd")
}, },
new new
{ {
Id = new Guid("2b649610-1da8-450a-ad24-021626d80c60"), Id = new Guid("cb42e7cb-02c4-4725-8d41-a62fa3868d67"),
Created = new DateTimeOffset(new DateTime(2023, 8, 29, 1, 43, 21, 914, DateTimeKind.Unspecified).AddTicks(5750), new TimeSpan(0, 0, 0, 0, 0)), 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", Map = "sr_TestTrack",
Time = 148.0, 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<int>("Completions")
.HasColumnType("integer");
b.Property<DateTimeOffset>("Created")
.HasColumnType("timestamp with time zone");
b.Property<Guid>("Id")
.HasColumnType("uuid");
b.Property<string>("Map")
.HasColumnType("text");
b.Property<int>("Position")
.HasColumnType("integer");
b.Property<double>("Time")
.HasColumnType("double precision");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.ToTable("PbDTO");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }
+1
View File
@@ -3,6 +3,7 @@ namespace Server.Models;
public class PbDTO { public class PbDTO {
public Guid Id { get; set; } public Guid Id { get; set; }
public Guid UserId { get; set; } public Guid UserId { get; set; }
public DateTimeOffset Created { get; set; }
public double Time { get; set; } public double Time { get; set; }
public string Map { get; set; } public string Map { get; set; }
public int Completions { get; set; } public int Completions { get; set; }
+17
View File
@@ -1,15 +1,32 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Server.Models;
namespace Server; namespace Server;
public class SpaceRaceDbContext : DbContext { public class SpaceRaceDbContext : DbContext {
public DbSet<Lap> Laps { get; set; } public DbSet<Lap> Laps { get; set; }
public DbSet<PbDTO> PbDTO { get; set; }
public SpaceRaceDbContext(DbContextOptions<SpaceRaceDbContext> options) : base(options) {} public SpaceRaceDbContext(DbContextOptions<SpaceRaceDbContext> options) : base(options) {}
protected override void OnModelCreating(ModelBuilder modelBuilder) { protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
new SpaceRaceInitialiser(modelBuilder).Seed(); new SpaceRaceInitialiser(modelBuilder).Seed();
modelBuilder.Entity<PbDTO>(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);
});
} }
} }