From 2a29571dbf7e33a28949c4b167f62a0f82c67c23 Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Wed, 1 Oct 2025 22:44:15 +0300 Subject: [PATCH] chore: cringed very hard --- LctMonolith/Database/Data/AppDbContext.cs | 43 ++++++++----- LctMonolith/Database/Data/DbSeeder.cs | 62 +++++++++---------- LctMonolith/Dockerfile | 45 +++++++------- LctMonolith/Models/Database/Dialogue.cs | 6 +- .../Models/Database/DialogueMessage.cs | 7 --- LctMonolith/appsettings.Development.json | 2 +- LctMonolith/appsettings.json | 2 +- 7 files changed, 86 insertions(+), 81 deletions(-) diff --git a/LctMonolith/Database/Data/AppDbContext.cs b/LctMonolith/Database/Data/AppDbContext.cs index f97fba3..3e42986 100644 --- a/LctMonolith/Database/Data/AppDbContext.cs +++ b/LctMonolith/Database/Data/AppDbContext.cs @@ -11,7 +11,9 @@ namespace LctMonolith.Database.Data; /// public class AppDbContext : IdentityDbContext, Guid> { - public AppDbContext(DbContextOptions options) : base(options) { } + public AppDbContext(DbContextOptions options) : base(options) { + Database.EnsureCreated(); + } // Rank related entities public DbSet Ranks => Set(); @@ -92,7 +94,7 @@ public class AppDbContext : IdentityDbContext, Guid> .WithMany(m => m.ChildMissions) .HasForeignKey(m => m.ParentMissionId) .IsRequired(false); - // Dialogue relationship for Mission + // Dialogue relationship for Mission (optional dialogue for a mission) b.Entity() .HasOne(m => m.Dialogue) .WithOne(d => d.Mission) @@ -171,31 +173,28 @@ public class AppDbContext : IdentityDbContext, Guid> .WithMany(m => m.PlayerMissions) .HasForeignKey(pm => pm.MissionId); - // Dialogue configurations + // Dialogue configurations (Mission required for Dialogue) b.Entity() .HasOne(d => d.Mission) .WithOne(m => m.Dialogue) .HasForeignKey(d => d.MissionId) .IsRequired(); - // DialogueMessage configurations - b.Entity() - .HasOne(dm => dm.InitialDialogue) + // Dialogue -> DialogueMessage relationships (three distinct message slots) + b.Entity() + .HasOne(d => d.InitialDialogueMessage) .WithMany() - .HasForeignKey(dm => dm.InitialDialogueId) - .IsRequired(false) + .HasForeignKey(d => d.InitialDialogueMessageId) .OnDelete(DeleteBehavior.Restrict); - b.Entity() - .HasOne(dm => dm.InterimDialogue) + b.Entity() + .HasOne(d => d.InterimDialogueMessage) .WithMany() - .HasForeignKey(dm => dm.InterimDialogueId) - .IsRequired(false) + .HasForeignKey(d => d.InterimDialogueMessageId) .OnDelete(DeleteBehavior.Restrict); - b.Entity() - .HasOne(dm => dm.EndDialogue) + b.Entity() + .HasOne(d => d.EndDialogueMessage) .WithMany() - .HasForeignKey(dm => dm.EndDialogueId) - .IsRequired(false) + .HasForeignKey(d => d.EndDialogueMessageId) .OnDelete(DeleteBehavior.Restrict); // DialogueMessageResponseOption configurations @@ -211,6 +210,18 @@ public class AppDbContext : IdentityDbContext, Guid> .IsRequired(false) .OnDelete(DeleteBehavior.Restrict); + // UserInventoryItem composite key & relationships + b.Entity() + .HasKey(ui => new { ui.UserId, ui.StoreItemId }); + b.Entity() + .HasOne(ui => ui.User) + .WithMany(u => u.Inventory) + .HasForeignKey(ui => ui.UserId); + b.Entity() + .HasOne(ui => ui.StoreItem) + .WithMany(si => si.UserInventory) + .HasForeignKey(ui => ui.StoreItemId); + // Refresh token index unique b.Entity().HasIndex(x => x.Token).IsUnique(); diff --git a/LctMonolith/Database/Data/DbSeeder.cs b/LctMonolith/Database/Data/DbSeeder.cs index 40a6722..91e8317 100644 --- a/LctMonolith/Database/Data/DbSeeder.cs +++ b/LctMonolith/Database/Data/DbSeeder.cs @@ -12,36 +12,36 @@ public static class DbSeeder { public static async Task SeedAsync(AppDbContext db, CancellationToken ct = default) { - await db.Database.EnsureCreatedAsync(ct); - - if (!await db.Ranks.AnyAsync(ct)) - { - var ranks = new List - { - new() { Title = "Искатель", ExpNeeded = 0 }, - new() { Title = "Пилот-кандидат", ExpNeeded = 500 }, - new() { Title = "Принятый в экипаж", ExpNeeded = 1500 } - }; - db.Ranks.AddRange(ranks); - Log.Information("Seeded {Count} ranks", ranks.Count); - } - - if (!await db.Skills.AnyAsync(ct)) - { - var comps = new[] - { - "Вера в дело","Стремление к большему","Общение","Аналитика","Командование","Юриспруденция","Трёхмерное мышление","Базовая экономика","Основы аэронавигации" - }.Select(n => new Skill { Title = n }); - db.Skills.AddRange(comps); - Log.Information("Seeded competencies"); - } - - if (!await db.StoreItems.AnyAsync(ct)) - { - db.StoreItems.AddRange(new StoreItem { Name = "Футболка Алабуга", Price = 100 }, new StoreItem { Name = "Брелок Буран", Price = 50 }); - Log.Information("Seeded store items"); - } - - await db.SaveChangesAsync(ct); + // await db.Database.EnsureCreatedAsync(ct); + // + // if (!await db.Ranks.AnyAsync(ct)) + // { + // var ranks = new List + // { + // new() { Title = "Искатель", ExpNeeded = 0 }, + // new() { Title = "Пилот-кандидат", ExpNeeded = 500 }, + // new() { Title = "Принятый в экипаж", ExpNeeded = 1500 } + // }; + // db.Ranks.AddRange(ranks); + // Log.Information("Seeded {Count} ranks", ranks.Count); + // } + // + // if (!await db.Skills.AnyAsync(ct)) + // { + // var comps = new[] + // { + // "Вера в дело","Стремление к большему","Общение","Аналитика","Командование","Юриспруденция","Трёхмерное мышление","Базовая экономика","Основы аэронавигации" + // }.Select(n => new Skill { Title = n }); + // db.Skills.AddRange(comps); + // Log.Information("Seeded competencies"); + // } + // + // if (!await db.StoreItems.AnyAsync(ct)) + // { + // db.StoreItems.AddRange(new StoreItem { Name = "Футболка Алабуга", Price = 100 }, new StoreItem { Name = "Брелок Буран", Price = 50 }); + // Log.Information("Seeded store items"); + // } + // + // await db.SaveChangesAsync(ct); } } diff --git a/LctMonolith/Dockerfile b/LctMonolith/Dockerfile index fc5ed8b..4198520 100644 --- a/LctMonolith/Dockerfile +++ b/LctMonolith/Dockerfile @@ -1,23 +1,24 @@ -FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base -USER $APP_UID -WORKDIR /app -EXPOSE 8080 -EXPOSE 8081 - -FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build -ARG BUILD_CONFIGURATION=Release -WORKDIR /src -COPY ["LctMonolith/LctMonolith.csproj", "LctMonolith/"] -RUN dotnet restore "LctMonolith/LctMonolith.csproj" -COPY . . -WORKDIR "/src/LctMonolith" -RUN dotnet build "./LctMonolith.csproj" -c $BUILD_CONFIGURATION -o /app/build - -FROM build AS publish -ARG BUILD_CONFIGURATION=Release -RUN dotnet publish "./LctMonolith.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false - -FROM base AS final -WORKDIR /app -COPY --from=publish /app/publish . +FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base +WORKDIR /app +EXPOSE 8080 + +ENV ASPNETCORE_URLS=http://+:8080 + +USER app +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build +ARG configuration=Release +WORKDIR /src +COPY ["LctMonolith.csproj", "./"] +RUN dotnet restore "LctMonolith.csproj" +COPY . . +WORKDIR "/src/." +RUN dotnet build "LctMonolith.csproj" -c $configuration -o /app/build + +FROM build AS publish +ARG configuration=Release +RUN dotnet publish "LctMonolith.csproj" -c $configuration -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "LctMonolith.dll"] diff --git a/LctMonolith/Models/Database/Dialogue.cs b/LctMonolith/Models/Database/Dialogue.cs index d51fe59..161b674 100644 --- a/LctMonolith/Models/Database/Dialogue.cs +++ b/LctMonolith/Models/Database/Dialogue.cs @@ -8,9 +8,9 @@ public class Dialogue public required Mission Mission { get; set; } public Guid InitialDialogueMessageId { get; set; } - public Dialogue? InitialDialogueMessage { get; set; } + public DialogueMessage? InitialDialogueMessage { get; set; } public Guid InterimDialogueMessageId { get; set; } - public Dialogue? InterimDialogueMessage { get; set; } + public DialogueMessage? InterimDialogueMessage { get; set; } public Guid EndDialogueMessageId { get; set; } - public Dialogue? EndDialogueMessage { get; set; } + public DialogueMessage? EndDialogueMessage { get; set; } } diff --git a/LctMonolith/Models/Database/DialogueMessage.cs b/LctMonolith/Models/Database/DialogueMessage.cs index 66d138e..db67027 100644 --- a/LctMonolith/Models/Database/DialogueMessage.cs +++ b/LctMonolith/Models/Database/DialogueMessage.cs @@ -16,12 +16,5 @@ public class DialogueMessage public bool AllowMessageAi { get; set; } public string MessageAiButtonText { get; set; } = string.Empty; - public Guid InitialDialogueId { get; set; } - public Dialogue? InitialDialogue { get; set; } - public Guid InterimDialogueId { get; set; } - public Dialogue? InterimDialogue { get; set; } - public Guid EndDialogueId { get; set; } - public Dialogue? EndDialogue { get; set; } - public ICollection DialogueMessageResponseOptions = new List(); } diff --git a/LctMonolith/appsettings.Development.json b/LctMonolith/appsettings.Development.json index 9ff01d6..4bc4499 100644 --- a/LctMonolith/appsettings.Development.json +++ b/LctMonolith/appsettings.Development.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "Default": "Host=localhost;Port=5432;Database=lct2025_dev;Username=postgres;Password=postgres" + "Default": "Host=postgres;Port=5432;Database=lct2025_dev;Username=postgres;Password=postgres" }, "Jwt": { "Key": "Dev_Insecure_Key_Change_Me", diff --git a/LctMonolith/appsettings.json b/LctMonolith/appsettings.json index de6f563..8e4cc77 100644 --- a/LctMonolith/appsettings.json +++ b/LctMonolith/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "Default": "Host=localhost;Port=5432;Database=lct2025;Username=postgres;Password=postgres" + "Default": "Host=postgres;Port=5432;Database=lct2025;Username=postgres;Password=postgres" }, "Jwt": { "Key": "Dev_Insecure_Key_Change_Me",