From 6e0dcd324be0006a6aa87ced8aa52e7656f56667 Mon Sep 17 00:00:00 2001 From: Pat Date: Sun, 9 Nov 2025 15:37:15 +0000 Subject: [PATCH] db initialiser migrations --- src/Games.Api/DOCKERFILE | 6 ------ src/Gateway.Api/Dockerfile | 6 ------ src/Identity.Api/Dockerfile | 6 ------ src/Infrastructure/Data/GamesDbContextInitialiser.cs | 6 +++++- .../Data/IdentityDbContextInitialiser.cs | 4 +++- src/Infrastructure/DependencyInjection.cs | 2 -- .../Middleware/GlobalExceptionHandlingMiddleware.cs | 10 ++++++---- 7 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/Games.Api/DOCKERFILE b/src/Games.Api/DOCKERFILE index 12ae3fb..cee7bb5 100644 --- a/src/Games.Api/DOCKERFILE +++ b/src/Games.Api/DOCKERFILE @@ -1,13 +1,10 @@ -# Use the official .NET 9.0 runtime as a parent image FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base WORKDIR /app EXPOSE 7001 -# Use the official .NET 9.0 SDK as a build image FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build WORKDIR /src -# Copy project files and restore dependencies COPY ["src/Games.Api/Games.Api.csproj", "src/Games.Api/"] COPY ["src/Application/Application.csproj", "src/Application/"] COPY ["src/Domain/Domain.csproj", "src/Domain/"] @@ -15,17 +12,14 @@ COPY ["src/Infrastructure/Infrastructure.csproj", "src/Infrastructure/"] RUN dotnet restore "src/Games.Api/Games.Api.csproj" -# Copy the rest of the application code COPY . . WORKDIR "/src/src/Games.Api" RUN dotnet build "Games.Api.csproj" -c Release -o /app/build -# Publish the application FROM build AS publish RUN dotnet publish "Games.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false -# Final stage/image FROM base AS final WORKDIR /app COPY --from=publish /app/publish . diff --git a/src/Gateway.Api/Dockerfile b/src/Gateway.Api/Dockerfile index cfc4535..1c39bc5 100644 --- a/src/Gateway.Api/Dockerfile +++ b/src/Gateway.Api/Dockerfile @@ -1,28 +1,22 @@ -# Use the official .NET 9.0 runtime as a parent image FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base WORKDIR /app EXPOSE 7000 -# Use the official .NET 9.0 SDK as a build image FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build WORKDIR /src -# Copy project files and restore dependencies COPY ["src/Gateway.Api/Gateway.Api.csproj", "src/Gateway.Api/"] RUN dotnet restore "src/Gateway.Api/Gateway.Api.csproj" -# Copy the rest of the application code COPY . . WORKDIR "/src/src/Gateway.Api" RUN dotnet build "Gateway.Api.csproj" -c Release -o /app/build -# Publish the application FROM build AS publish RUN dotnet publish "Gateway.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false -# Final stage/image FROM base AS final WORKDIR /app COPY --from=publish /app/publish . diff --git a/src/Identity.Api/Dockerfile b/src/Identity.Api/Dockerfile index 886acdb..f03039f 100644 --- a/src/Identity.Api/Dockerfile +++ b/src/Identity.Api/Dockerfile @@ -1,13 +1,10 @@ -# Use the official .NET 9.0 runtime as a parent image FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base WORKDIR /app EXPOSE 7002 -# Use the official .NET 9.0 SDK as a build image FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build WORKDIR /src -# Copy project files and restore dependencies COPY ["src/Identity.Api/Identity.Api.csproj", "src/Identity.Api/"] COPY ["src/Application/Application.csproj", "src/Application/"] COPY ["src/Domain/Domain.csproj", "src/Domain/"] @@ -15,17 +12,14 @@ COPY ["src/Infrastructure/Infrastructure.csproj", "src/Infrastructure/"] RUN dotnet restore "src/Identity.Api/Identity.Api.csproj" -# Copy the rest of the application code COPY . . WORKDIR "/src/src/Identity.Api" RUN dotnet build "Identity.Api.csproj" -c Release -o /app/build -# Publish the application FROM build AS publish RUN dotnet publish "Identity.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false -# Final stage/image FROM base AS final WORKDIR /app COPY --from=publish /app/publish . diff --git a/src/Infrastructure/Data/GamesDbContextInitialiser.cs b/src/Infrastructure/Data/GamesDbContextInitialiser.cs index adfaaf3..dbe2285 100644 --- a/src/Infrastructure/Data/GamesDbContextInitialiser.cs +++ b/src/Infrastructure/Data/GamesDbContextInitialiser.cs @@ -1,5 +1,6 @@ using Domain.Common.Interfaces; using Domain.Entities; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace Infrastructure.Data; @@ -12,9 +13,12 @@ public async Task InitialiseAsync() { try { - await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); + + await context.Database.MigrateAsync(); + await SeedAsync(); + } catch (Exception ex) { diff --git a/src/Infrastructure/Data/IdentityDbContextInitialiser.cs b/src/Infrastructure/Data/IdentityDbContextInitialiser.cs index ca0b679..f59eb78 100644 --- a/src/Infrastructure/Data/IdentityDbContextInitialiser.cs +++ b/src/Infrastructure/Data/IdentityDbContextInitialiser.cs @@ -2,6 +2,7 @@ using Domain.Entities; using Domain.Identity.PasswordHashers; using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace Infrastructure.Data; @@ -15,9 +16,10 @@ public async Task InitialiseAsync() { try { - await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); + await context.Database.MigrateAsync(); + await SeedAsync(); } catch (Exception ex) diff --git a/src/Infrastructure/DependencyInjection.cs b/src/Infrastructure/DependencyInjection.cs index 53625a7..fe66768 100644 --- a/src/Infrastructure/DependencyInjection.cs +++ b/src/Infrastructure/DependencyInjection.cs @@ -1,10 +1,8 @@ using Application; using Application.Identity.Queries.GetUsers; -using Application.Users.Commands.AddUserGame; using Domain.Common.Interfaces; using Infrastructure.Data; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Infrastructure/Middleware/GlobalExceptionHandlingMiddleware.cs b/src/Infrastructure/Middleware/GlobalExceptionHandlingMiddleware.cs index da4461c..56a2965 100644 --- a/src/Infrastructure/Middleware/GlobalExceptionHandlingMiddleware.cs +++ b/src/Infrastructure/Middleware/GlobalExceptionHandlingMiddleware.cs @@ -12,6 +12,11 @@ public class GlobalExceptionHandlingMiddleware(RequestDelegate next, ILogger _logger = logger; + private static readonly JsonSerializerOptions _jsonSerializerOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + public async Task InvokeAsync(HttpContext context) { try @@ -42,10 +47,7 @@ private static async Task HandleExceptionAsync(HttpContext context, Exception ex } }; - var jsonResponse = JsonSerializer.Serialize(response, new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }); + var jsonResponse = JsonSerializer.Serialize(response, _jsonSerializerOptions); await context.Response.WriteAsync(jsonResponse); }