Add entity framework #11
@@ -29,6 +29,12 @@ builder.Services.AddSingleton<Translations>();
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
await FetchTranslation(app);
|
||||||
|
|
||||||
|
await app.RunAsync();
|
||||||
|
|
||||||
|
static async Task FetchTranslation(WebAssemblyHost app)
|
||||||
|
{
|
||||||
var client = app.Services.GetService<IHttpClientFactory>()?.CreateClient("GameIdeas.WebAPI") ??
|
var client = app.Services.GetService<IHttpClientFactory>()?.CreateClient("GameIdeas.WebAPI") ??
|
||||||
throw new Exception("Http client not found");
|
throw new Exception("Http client not found");
|
||||||
var response = await client.GetAsync("api/Translations");
|
var response = await client.GetAsync("api/Translations");
|
||||||
@@ -39,5 +45,4 @@ if (dictionary != null)
|
|||||||
app.Services.GetService<TranslationService>()!.Initialize(dictionary);
|
app.Services.GetService<TranslationService>()!.Initialize(dictionary);
|
||||||
ResourcesKey.Initialize(app.Services.GetService<Translations>()!);
|
ResourcesKey.Initialize(app.Services.GetService<Translations>()!);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
await app.RunAsync();
|
|
||||||
@@ -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)
|
||||||
|
|||||||
@@ -1,30 +1,56 @@
|
|||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.UseCors();
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
||||||
|
|
||||||
|
async Task LoadTranslations()
|
||||||
|
{
|
||||||
var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files");
|
var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files");
|
||||||
var translationFiles = Directory.GetFiles(filesDirectory, "*.json");
|
var translationFiles = Directory.GetFiles(filesDirectory, "*.json");
|
||||||
var dictionary = new Dictionary<string, string>();
|
var dictionary = new Dictionary<string, string>();
|
||||||
@@ -39,9 +65,4 @@ foreach (var file in translationFiles)
|
|||||||
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
|
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
|
||||||
ResourcesKey.Initialize(app.Services.GetRequiredService<Translations>());
|
ResourcesKey.Initialize(app.Services.GetRequiredService<Translations>());
|
||||||
|
|
||||||
app.UseCors();
|
}
|
||||||
app.UseAuthorization();
|
|
||||||
|
|
||||||
app.MapControllers();
|
|
||||||
|
|
||||||
app.Run();
|
|
||||||
|
|||||||
Reference in New Issue
Block a user