adding completions

This commit is contained in:
ntr committed 2023-08-23 13:39:13 +10:00
1 parent 0ce322b906
commit b934fe9a4c
11 files changed
+210 -20

No files matched your search

+16 -1
View File
@@ -15,7 +15,7 @@ public class CompletionController : ControllerBase {
_db = db;
}
[HttpGet(Name = "GetCompletion")]
[HttpGet()]
public IEnumerable<CompletionDTO> Get() {
return _db.Completions.Select(c => new CompletionDTO {
Id = c.Id,
@@ -25,4 +25,19 @@ public class CompletionController : ControllerBase {
Date = c.Date,
});
}
[HttpPost()]
public async Task<Completion> Post(CompletionDTO dto) {
Completion v = new Completion {
Id = Guid.NewGuid(),
UserId = Guid.NewGuid(),
Map = dto.Map,
Time = dto.Time,
Date = DateTimeOffset.UtcNow,
};
_db.Completions.Add(v);
await _db.SaveChangesAsync();
return v;
}
}
@@ -0,0 +1,58 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Server;
#nullable disable
namespace Server.Migrations
{
[DbContext(typeof(SpaceRaceDbContext))]
[Migration("20230823032740_Beginninging")]
partial class Beginninging
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Server.Completion", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("Date")
.HasColumnType("timestamp with time zone");
b.Property<string>("Map")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Time")
.HasColumnType("double precision");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("Map");
b.HasIndex("UserId");
b.ToTable("Completions");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,47 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Server.Migrations
{
/// <inheritdoc />
public partial class Beginninging : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Completions",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Date = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
Map = table.Column<string>(type: "text", nullable: false),
Time = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Completions", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Completions_Map",
table: "Completions",
column: "Map");
migrationBuilder.CreateIndex(
name: "IX_Completions_UserId",
table: "Completions",
column: "UserId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Completions");
}
}
}
@@ -0,0 +1,55 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Server;
#nullable disable
namespace Server.Migrations
{
[DbContext(typeof(SpaceRaceDbContext))]
partial class SpaceRaceDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Server.Completion", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("Date")
.HasColumnType("timestamp with time zone");
b.Property<string>("Map")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Time")
.HasColumnType("double precision");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("Map");
b.HasIndex("UserId");
b.ToTable("Completions");
});
#pragma warning restore 612, 618
}
}
}
-9
View File
@@ -1,9 +0,0 @@
namespace Server.Models;
public class CompletionDTO {
public DateTimeOffset Date { get; set; }
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Map { get; set; }
public double Time { get; set; }
}
+9
View File
@@ -0,0 +1,9 @@
namespace Server.Models;
public class CompletionDTO {
public DateTimeOffset Date { get; set; }
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Map { get; set; }
public double Time { get; set; }
}
+13
View File
@@ -14,8 +14,21 @@ builder.Services.AddDbContextPool<SpaceRaceDbContext>(options => {
var app = builder.Build();
UpdateDatabase(app);
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
void UpdateDatabase(IApplicationBuilder app) {
try {
using var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
using var context = serviceScope.ServiceProvider.GetService<SpaceRaceDbContext>();
context?.Database.Migrate();
} catch (Exception e) {
Console.Error.WriteLine("Error migrating db. {0}", e);
System.Environment.Exit(1);
}
}
+5 -5
View File
@@ -4,7 +4,7 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58932",
"applicationUrl": "http://0.0.0.0:58932",
"sslPort": 44337
}
},
@@ -13,8 +13,8 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5212",
"launchUrl": "",
"applicationUrl": "http://0.0.0.0:80",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@@ -23,8 +23,8 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7246;http://localhost:5212",
"launchUrl": "",
"applicationUrl": "https://0.0.0.0:7246;http://0.0.0.0:5212",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
+4 -1
View File
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace Server;
@@ -7,10 +8,12 @@ public class SpaceRaceDbContext : DbContext {
public SpaceRaceDbContext(DbContextOptions<SpaceRaceDbContext> options) : base(options) {}
}
[Index(nameof(UserId))]
[Index(nameof(Map))]
public class Completion {
public DateTimeOffset Date { get; set; }
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Map { get; set; }
[Required] public string Map { get; set; }
public double Time { get; set; }
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"SpaceRace": "Server=pg;Port=5432;Database=spacerace;User Id=spacerace;Password=zoomzoom",
"SpaceRace": "Server=localhost;Port=45432;Database=spacerace;User Id=spacerace;Password=zoomzoom",
},
"Logging": {
"LogLevel": {
+2 -3
View File
@@ -19,16 +19,15 @@ services:
# command: dotnet watch
server:
image: mcr.microsoft.com/dotnet/sdk:7.0
depends_on:
- 'pg'
ports:
- '40080:80'
environment:
CONNECTIONSTRINGS__SPACERACE: "Server=pg;Port=5432;Database=${POSTGRES_DB};User Id=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}"
build:
context: .
dockerfile: ./Dockerfile
command: dotnet watch
working_dir: "/App"
volumes:
- "./:/App"