Add db context to services
All checks were successful
Game Ideas build for PR / build_blazor_app (pull_request) Successful in 52s

This commit is contained in:
Maxime Adler
2025-04-09 15:10:49 +02:00
parent cb60f3de31
commit 16580eaebe
3 changed files with 63 additions and 33 deletions

View File

@@ -29,15 +29,20 @@ builder.Services.AddSingleton<Translations>();
var app = builder.Build(); var app = builder.Build();
var client = app.Services.GetService<IHttpClientFactory>()?.CreateClient("GameIdeas.WebAPI") ?? await FetchTranslation(app);
throw new Exception("Http client not found");
var response = await client.GetAsync("api/Translations");
var dictionary = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();
if (dictionary != null) await app.RunAsync();
static async Task FetchTranslation(WebAssemblyHost app)
{ {
app.Services.GetService<TranslationService>()!.Initialize(dictionary); var client = app.Services.GetService<IHttpClientFactory>()?.CreateClient("GameIdeas.WebAPI") ??
ResourcesKey.Initialize(app.Services.GetService<Translations>()!); throw new Exception("Http client not found");
} var response = await client.GetAsync("api/Translations");
var dictionary = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();
await app.RunAsync(); if (dictionary != null)
{
app.Services.GetService<TranslationService>()!.Initialize(dictionary);
ResourcesKey.Initialize(app.Services.GetService<Translations>()!);
}
}

View File

@@ -5,9 +5,12 @@ namespace GameIdeas.WebAPI.Context;
public class GameIdeasContext : DbContext public class GameIdeasContext : DbContext
{ {
public GameIdeasContext(DbContextOptions<GameIdeasContext> option) private ILoggerFactory LoggerFactory;
public GameIdeasContext(DbContextOptions<GameIdeasContext> option, ILoggerFactory loggerFactory)
: base(option) : base(option)
{ } {
LoggerFactory = loggerFactory;
}
public virtual DbSet<Game> Games { get; set; } = null!; public virtual DbSet<Game> Games { get; set; } = null!;
public virtual DbSet<Platform> Platforms { get; set; } = null!; public virtual DbSet<Platform> Platforms { get; set; } = null!;
@@ -15,7 +18,8 @@ public class GameIdeasContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
optionsBuilder.UseNpgsql(@"Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase"); optionsBuilder.UseLoggerFactory(LoggerFactory);
base.OnConfiguring(optionsBuilder);
} }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)

View File

@@ -1,47 +1,68 @@
using GameIdeas.Resources; using GameIdeas.Resources;
using System.Resources; using GameIdeas.WebAPI.Context;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var connectionString = @"Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase";
Action<DbContextOptionsBuilder<GameIdeasContext>> dbContextOptions = options =>
{
options.UseNpgsql(
connectionString,
npgOption =>
{
npgOption.CommandTimeout(60);
npgOption.MigrationsAssembly("GameIdeas.WebAPI");
});
};
// Add services to the container. // Add services to the container.
services.AddDbContext<GameIdeasContext>();
builder.Services.AddControllers(); services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi(); services.AddOpenApi();
builder.Services.AddCors(option => option.AddDefaultPolicy(policy => services.AddCors(option => option.AddDefaultPolicy(policy =>
policy.WithOrigins("http://localhost:5172") policy.WithOrigins("http://localhost:5172")
.AllowAnyHeader() .AllowAnyHeader()
.WithMethods("GET", "POST"))); .WithMethods("GET", "POST", "PUT", "DELETE")));
builder.Services.AddSingleton<TranslationService>(); services.AddSingleton<TranslationService>();
builder.Services.AddSingleton<Translations>(); services.AddSingleton<Translations>();
var app = builder.Build(); var app = builder.Build();
await LoadTranslations();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.MapOpenApi(); app.MapOpenApi();
} }
var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files");
var translationFiles = Directory.GetFiles(filesDirectory, "*.json");
var dictionary = new Dictionary<string, string>();
foreach (var file in translationFiles)
{
var name = file.Split('.');
var culture = name[^2];
var content = await File.ReadAllTextAsync(file);
dictionary.Add(culture, content);
}
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
ResourcesKey.Initialize(app.Services.GetRequiredService<Translations>());
app.UseCors(); app.UseCors();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.Run(); app.Run();
async Task LoadTranslations()
{
var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files");
var translationFiles = Directory.GetFiles(filesDirectory, "*.json");
var dictionary = new Dictionary<string, string>();
foreach (var file in translationFiles)
{
var name = file.Split('.');
var culture = name[^2];
var content = await File.ReadAllTextAsync(file);
dictionary.Add(culture, content);
}
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
ResourcesKey.Initialize(app.Services.GetRequiredService<Translations>());
}