pure sql baybeee

This commit is contained in:
ntr committed 2023-08-30 20:21:08 +10:00
1 parent b7ad479ead
commit 5a7e493d92
12 files changed
+173 -39

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore;
using SpaceRace.Shared;
namespace Server.Services;
public class LapTimesService {
private readonly ILogger<LapTimesService> _logger;
private SpaceRaceDbContext _db;
public LapTimesService(ILogger<LapTimesService> logger, SpaceRaceDbContext db) {
_logger = logger;
_db = db;
}
public LapTimesDTO LapTimes(Guid userId, string mapName) {
PbDTO pb = _db.PbDTO
.FromSql($$"""
WITH ranked AS (
SELECT *, rank() OVER (ORDER BY "Time" ASC) AS "Position", COUNT("Id") OVER () AS "Completions"
FROM "Laps"
WHERE "Map" = {{mapName}}
ORDER BY "Position" ASC
)
SELECT * FROM ranked
WHERE "UserId" = {{userId}}
""").FirstOrDefault();
PbDTO lapRecord = _db.PbDTO
.FromSql($$"""
SELECT *, rank() OVER (ORDER BY "Time" ASC) AS "Position", COUNT("Id") OVER () AS "Completions"
FROM "Laps"
WHERE "Map" = {{mapName}}
ORDER BY "Position" ASC
""").FirstOrDefault();
return new LapTimesDTO {
Pb = pb,
LapRecord = lapRecord,
};
}
}