This commit is contained in:
@@ -104,7 +104,7 @@ namespace Aberwyn.Controllers
|
||||
[Authorize(Roles = "Admin")]
|
||||
public IActionResult ImportMealsFromProd()
|
||||
{
|
||||
var prodService = MenuService.CreateWithConfig(_configuration, _env, useProdDb: true);
|
||||
var prodService = MenuService.CreateWithSetup(_env);
|
||||
var devService = new MenuService(_context); // injicerad context används
|
||||
|
||||
var prodMeals = prodService.GetMealsDetailed();
|
||||
@@ -184,7 +184,7 @@ namespace Aberwyn.Controllers
|
||||
public IActionResult ImportBudgetFromProd()
|
||||
{
|
||||
// Hämta connection till produktion
|
||||
using var prodContext = ApplicationDbContextFactory.CreateWithConfig(_configuration, _env, useProdDb: true);
|
||||
using var prodContext = ApplicationDbContextFactory.CreateWithConfig(_env, true);
|
||||
|
||||
// Importera definitioner först
|
||||
var prodCategoryDefs = prodContext.BudgetCategoryDefinitions.ToList();
|
||||
|
||||
@@ -5,6 +5,7 @@ using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using static Aberwyn.Data.SetupService;
|
||||
|
||||
namespace Aberwyn.Data
|
||||
{
|
||||
@@ -30,26 +31,18 @@ namespace Aberwyn.Data
|
||||
return new ApplicationDbContext(optionsBuilder.Options);
|
||||
}
|
||||
|
||||
public static ApplicationDbContext CreateWithConfig(IConfiguration config, IHostEnvironment env, bool useProdDb = false)
|
||||
public static ApplicationDbContext CreateWithConfig(IHostEnvironment env, bool useProdDb = false)
|
||||
{
|
||||
var setup = LoadSetup();
|
||||
|
||||
var csBuilder = new MySqlConnector.MySqlConnectionStringBuilder
|
||||
{
|
||||
Server = setup.DbHost,
|
||||
Port = (uint)setup.DbPort,
|
||||
Database = setup.DbName,
|
||||
UserID = setup.DbUser,
|
||||
Password = setup.DbPassword,
|
||||
AllowUserVariables = true
|
||||
};
|
||||
var setup = SetupLoader.Load(env);
|
||||
var connStr = SetupLoader.GetConnectionString(setup);
|
||||
|
||||
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
optionsBuilder.UseMySql(csBuilder.ConnectionString, new MySqlServerVersion(new Version(8, 0, 36)));
|
||||
optionsBuilder.UseMySql(connStr, new MySqlServerVersion(new Version(8, 0, 36)));
|
||||
|
||||
return new ApplicationDbContext(optionsBuilder.Options);
|
||||
}
|
||||
|
||||
|
||||
private static SetupSettings LoadSetup()
|
||||
{
|
||||
var basePath = Directory.GetCurrentDirectory();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using Aberwyn.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Globalization;
|
||||
|
||||
using static Aberwyn.Data.SetupService;
|
||||
|
||||
namespace Aberwyn.Data
|
||||
{
|
||||
@@ -47,6 +47,15 @@ public class MenuService
|
||||
var context = new ApplicationDbContext(builder.Options);
|
||||
return new MenuService(context);
|
||||
}
|
||||
public static MenuService CreateWithSetup(IHostEnvironment env)
|
||||
{
|
||||
var setup = SetupLoader.Load(env);
|
||||
var connStr = SetupLoader.GetConnectionString(setup);
|
||||
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
builder.UseMySql(connStr, ServerVersion.AutoDetect(connStr));
|
||||
var context = new ApplicationDbContext(builder.Options);
|
||||
return new MenuService(context);
|
||||
}
|
||||
|
||||
public void UpdateWeeklyMenu(MenuViewModel model)
|
||||
{
|
||||
|
||||
@@ -3,6 +3,9 @@ using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Aberwyn.Data
|
||||
{
|
||||
@@ -50,6 +53,21 @@ namespace Aberwyn.Data
|
||||
return services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
public static class SetupLoader
|
||||
{
|
||||
public static SetupSettings Load(IHostEnvironment env)
|
||||
{
|
||||
var path = Path.Combine(env.ContentRootPath, "infrastructure", "setup.json");
|
||||
var json = File.ReadAllText(path);
|
||||
return JsonSerializer.Deserialize<SetupSettings>(json)!;
|
||||
}
|
||||
|
||||
public static string GetConnectionString(SetupSettings setup)
|
||||
{
|
||||
return $"server={setup.DbHost};port={setup.DbPort};database={setup.DbName};user={setup.DbUser};password={setup.DbPassword}";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user