Add entity framework #11

Merged
Egamorf merged 7 commits from release/entity-framework into main 2025-04-09 23:12:48 +02:00
3 changed files with 63 additions and 33 deletions
Showing only changes of commit 16580eaebe - Show all commits

View File

@@ -29,15 +29,20 @@ builder.Services.AddSingleton<Translations>();
var app = builder.Build();
var client = app.Services.GetService<IHttpClientFactory>()?.CreateClient("GameIdeas.WebAPI") ??
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)
{
app.Services.GetService<TranslationService>()!.Initialize(dictionary);
ResourcesKey.Initialize(app.Services.GetService<Translations>()!);
}
await FetchTranslation(app);
await app.RunAsync();
static async Task FetchTranslation(WebAssemblyHost app)
{
var client = app.Services.GetService<IHttpClientFactory>()?.CreateClient("GameIdeas.WebAPI") ??
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)
{
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 GameIdeasContext(DbContextOptions<GameIdeasContext> option)
private ILoggerFactory LoggerFactory;
public GameIdeasContext(DbContextOptions<GameIdeasContext> option, ILoggerFactory loggerFactory)
: base(option)
{ }
{
LoggerFactory = loggerFactory;
}
public virtual DbSet<Game> Games { 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)
{
optionsBuilder.UseNpgsql(@"Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase");
optionsBuilder.UseLoggerFactory(LoggerFactory);
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)

View File

@@ -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<DbContextOptionsBuilder<GameIdeasContext>> dbContextOptions = options =>
{
options.UseNpgsql(
connectionString,
npgOption =>
{
npgOption.CommandTimeout(60);
npgOption.MigrationsAssembly("GameIdeas.WebAPI");
});
};
// Add services to the container.
services.AddDbContext<GameIdeasContext>();
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<TranslationService>();
builder.Services.AddSingleton<Translations>();
services.AddSingleton<TranslationService>();
services.AddSingleton<Translations>();
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<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.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<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>());
}