diff --git a/Server/Controllers/CompletionController.cs b/Server/Controllers/CompletionController.cs index b410211..831ed03 100644 --- a/Server/Controllers/CompletionController.cs +++ b/Server/Controllers/CompletionController.cs @@ -15,7 +15,7 @@ public class CompletionController : ControllerBase { _db = db; } - [HttpGet(Name = "GetCompletion")] + [HttpGet()] public IEnumerable 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 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; + } } diff --git a/Server/Migrations/20230823032740_Beginninging.Designer.cs b/Server/Migrations/20230823032740_Beginninging.Designer.cs new file mode 100644 index 0000000..ffca26a --- /dev/null +++ b/Server/Migrations/20230823032740_Beginninging.Designer.cs @@ -0,0 +1,58 @@ +// +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 + { + /// + 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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Date") + .HasColumnType("timestamp with time zone"); + + b.Property("Map") + .IsRequired() + .HasColumnType("text"); + + b.Property("Time") + .HasColumnType("double precision"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("Map"); + + b.HasIndex("UserId"); + + b.ToTable("Completions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Server/Migrations/20230823032740_Beginninging.cs b/Server/Migrations/20230823032740_Beginninging.cs new file mode 100644 index 0000000..f28929b --- /dev/null +++ b/Server/Migrations/20230823032740_Beginninging.cs @@ -0,0 +1,47 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Server.Migrations +{ + /// + public partial class Beginninging : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Completions", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Date = table.Column(type: "timestamp with time zone", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + Map = table.Column(type: "text", nullable: false), + Time = table.Column(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"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Completions"); + } + } +} diff --git a/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs b/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs new file mode 100644 index 0000000..86cf5d3 --- /dev/null +++ b/Server/Migrations/SpaceRaceDbContextModelSnapshot.cs @@ -0,0 +1,55 @@ +// +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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Date") + .HasColumnType("timestamp with time zone"); + + b.Property("Map") + .IsRequired() + .HasColumnType("text"); + + b.Property("Time") + .HasColumnType("double precision"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("Map"); + + b.HasIndex("UserId"); + + b.ToTable("Completions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Server/Models/Completion.cs b/Server/Models/Completion.cs deleted file mode 100644 index 2dfe597..0000000 --- a/Server/Models/Completion.cs +++ /dev/null @@ -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; } -} diff --git a/Server/Models/CompletionDTO.cs b/Server/Models/CompletionDTO.cs new file mode 100644 index 0000000..e47b25b --- /dev/null +++ b/Server/Models/CompletionDTO.cs @@ -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; } +} diff --git a/Server/Program.cs b/Server/Program.cs index 77c889c..3cf1645 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -14,8 +14,21 @@ builder.Services.AddDbContextPool(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().CreateScope(); + using var context = serviceScope.ServiceProvider.GetService(); + context?.Database.Migrate(); + } catch (Exception e) { + Console.Error.WriteLine("Error migrating db. {0}", e); + System.Environment.Exit(1); + } +} diff --git a/Server/Properties/launchSettings.json b/Server/Properties/launchSettings.json index 5c40582..b26525b 100644 --- a/Server/Properties/launchSettings.json +++ b/Server/Properties/launchSettings.json @@ -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" } diff --git a/Server/Repositories/SpaceRaceDbContext.cs b/Server/Repositories/SpaceRaceDbContext.cs index adf29bc..c300c2f 100644 --- a/Server/Repositories/SpaceRaceDbContext.cs +++ b/Server/Repositories/SpaceRaceDbContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; namespace Server; @@ -7,10 +8,12 @@ public class SpaceRaceDbContext : DbContext { public SpaceRaceDbContext(DbContextOptions 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; } } diff --git a/Server/appsettings.Development.json b/Server/appsettings.Development.json index dc27707..a32e316 100644 --- a/Server/appsettings.Development.json +++ b/Server/appsettings.Development.json @@ -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": { diff --git a/Server/docker-compose.yaml b/Server/docker-compose.yaml index cfbc6a1..8178adf 100644 --- a/Server/docker-compose.yaml +++ b/Server/docker-compose.yaml @@ -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"