diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Program.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Program.cs index 3331e0c..e35dffa 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Program.cs +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Program.cs @@ -29,15 +29,20 @@ builder.Services.AddSingleton(); var app = builder.Build(); -var client = app.Services.GetService()?.CreateClient("GameIdeas.WebAPI") ?? - throw new Exception("Http client not found"); -var response = await client.GetAsync("api/Translations"); -var dictionary = await response.Content.ReadFromJsonAsync>(); +await FetchTranslation(app); -if (dictionary != null) +await app.RunAsync(); + +static async Task FetchTranslation(WebAssemblyHost app) { - app.Services.GetService()!.Initialize(dictionary); - ResourcesKey.Initialize(app.Services.GetService()!); -} + var client = app.Services.GetService()?.CreateClient("GameIdeas.WebAPI") ?? + throw new Exception("Http client not found"); + var response = await client.GetAsync("api/Translations"); + var dictionary = await response.Content.ReadFromJsonAsync>(); -await app.RunAsync(); \ No newline at end of file + if (dictionary != null) + { + app.Services.GetService()!.Initialize(dictionary); + ResourcesKey.Initialize(app.Services.GetService()!); + } +} \ No newline at end of file diff --git a/src/GameIdeas/Server/GameIdeas.WebAPI/Context/GameIdeasContext.cs b/src/GameIdeas/Server/GameIdeas.WebAPI/Context/GameIdeasContext.cs index d135ee6..0ac64be 100644 --- a/src/GameIdeas/Server/GameIdeas.WebAPI/Context/GameIdeasContext.cs +++ b/src/GameIdeas/Server/GameIdeas.WebAPI/Context/GameIdeasContext.cs @@ -5,9 +5,12 @@ namespace GameIdeas.WebAPI.Context; public class GameIdeasContext : DbContext { - public GameIdeasContext(DbContextOptions option) + private ILoggerFactory LoggerFactory; + public GameIdeasContext(DbContextOptions option, ILoggerFactory loggerFactory) : base(option) - { } + { + LoggerFactory = loggerFactory; + } public virtual DbSet Games { get; set; } = null!; public virtual DbSet Platforms { get; set; } = null!; @@ -15,7 +18,8 @@ public class GameIdeasContext : DbContext 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) diff --git a/src/GameIdeas/Server/GameIdeas.WebAPI/Program.cs b/src/GameIdeas/Server/GameIdeas.WebAPI/Program.cs index b5be7c3..bbb1ee9 100644 --- a/src/GameIdeas/Server/GameIdeas.WebAPI/Program.cs +++ b/src/GameIdeas/Server/GameIdeas.WebAPI/Program.cs @@ -1,47 +1,68 @@ using GameIdeas.Resources; -using System.Resources; +using GameIdeas.WebAPI.Context; +using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); +var services = builder.Services; +var connectionString = @"Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase"; + +Action> dbContextOptions = options => +{ + options.UseNpgsql( + connectionString, + npgOption => + { + npgOption.CommandTimeout(60); + npgOption.MigrationsAssembly("GameIdeas.WebAPI"); + }); +}; // Add services to the container. +services.AddDbContext(); -builder.Services.AddControllers(); +services.AddControllers(); // 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") .AllowAnyHeader() - .WithMethods("GET", "POST"))); + .WithMethods("GET", "POST", "PUT", "DELETE"))); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); +services.AddSingleton(); +services.AddSingleton(); var app = builder.Build(); +await LoadTranslations(); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } -var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files"); -var translationFiles = Directory.GetFiles(filesDirectory, "*.json"); -var dictionary = new Dictionary(); -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().Initialize(dictionary); -ResourcesKey.Initialize(app.Services.GetRequiredService()); - app.UseCors(); app.UseAuthorization(); app.MapControllers(); app.Run(); + +async Task LoadTranslations() +{ + var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files"); + var translationFiles = Directory.GetFiles(filesDirectory, "*.json"); + var dictionary = new Dictionary(); + 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().Initialize(dictionary); + ResourcesKey.Initialize(app.Services.GetRequiredService()); + +}