Woohoo working version gooo
This commit is contained in:
@@ -102,54 +102,20 @@ namespace Aberwyn.Controllers
|
||||
}
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public IActionResult ImportMealsFromProd()
|
||||
public IActionResult ImportMenusFromCustom(string dbHost, int dbPort, string dbName, string dbUser, string dbPassword)
|
||||
{
|
||||
var prodService = MenuService.CreateWithSetup(_env);
|
||||
var devService = new MenuService(_context); // injicerad context används
|
||||
var connStr = $"Server={dbHost};Port={dbPort};Database={dbName};Uid={dbUser};Pwd={dbPassword};";
|
||||
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
optionsBuilder.UseMySql(connStr, ServerVersion.AutoDetect(connStr));
|
||||
|
||||
var prodMeals = prodService.GetMealsDetailed();
|
||||
|
||||
foreach (var meal in prodMeals)
|
||||
{
|
||||
// Kopiera utan ID (för att undvika konflikt) och spara
|
||||
var newMeal = new Meal
|
||||
{
|
||||
Name = meal.Name,
|
||||
Description = meal.Description,
|
||||
ProteinType = meal.ProteinType,
|
||||
Category = meal.Category,
|
||||
CarbType = meal.CarbType,
|
||||
RecipeUrl = meal.RecipeUrl,
|
||||
ImageUrl = meal.ImageUrl,
|
||||
IsAvailable = meal.IsAvailable,
|
||||
CreatedAt = meal.CreatedAt,
|
||||
Instructions = meal.Instructions,
|
||||
ImageData = meal.ImageData,
|
||||
ImageMimeType = meal.ImageMimeType,
|
||||
Ingredients = meal.Ingredients.Select(i => new Ingredient
|
||||
{
|
||||
Quantity = i.Quantity,
|
||||
Item = i.Item
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
devService.SaveOrUpdateMealWithIngredients(newMeal);
|
||||
}
|
||||
|
||||
return Content("Import klar!");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public IActionResult ImportMenusFromProd()
|
||||
{
|
||||
var prodService = MenuService.CreateWithConfig(_configuration, _env, useProdDb: true);
|
||||
using var customContext = new ApplicationDbContext(optionsBuilder.Options);
|
||||
var sourceService = new MenuService(customContext);
|
||||
var devService = new MenuService(_context);
|
||||
|
||||
var allProdMenus = prodService.GetAllWeeklyMenus();
|
||||
var allMeals = devService.GetMeals();
|
||||
var sourceMenus = sourceService.GetAllWeeklyMenus();
|
||||
var devMeals = devService.GetMeals();
|
||||
|
||||
foreach (var menu in allProdMenus)
|
||||
foreach (var menu in sourceMenus)
|
||||
{
|
||||
var newMenu = new WeeklyMenu
|
||||
{
|
||||
@@ -163,34 +129,92 @@ namespace Aberwyn.Controllers
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(menu.BreakfastMealName))
|
||||
newMenu.BreakfastMealId = allMeals.FirstOrDefault(m => m.Name == menu.BreakfastMealName)?.Id;
|
||||
newMenu.BreakfastMealId = devMeals.FirstOrDefault(m => m.Name == menu.BreakfastMealName)?.Id;
|
||||
|
||||
if (!string.IsNullOrEmpty(menu.LunchMealName))
|
||||
newMenu.LunchMealId = allMeals.FirstOrDefault(m => m.Name == menu.LunchMealName)?.Id;
|
||||
newMenu.LunchMealId = devMeals.FirstOrDefault(m => m.Name == menu.LunchMealName)?.Id;
|
||||
|
||||
if (!string.IsNullOrEmpty(menu.DinnerMealName))
|
||||
newMenu.DinnerMealId = allMeals.FirstOrDefault(m => m.Name == menu.DinnerMealName)?.Id;
|
||||
newMenu.DinnerMealId = devMeals.FirstOrDefault(m => m.Name == menu.DinnerMealName)?.Id;
|
||||
|
||||
_context.WeeklyMenus.Add(newMenu);
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
TempData["Message"] = "Import av veckomenyer klar.";
|
||||
TempData["Message"] = $"✅ Import av veckomenyer från extern databas klar ({sourceMenus.Count}).";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public IActionResult ImportBudgetFromProd()
|
||||
public IActionResult ImportMealsFromCustom(string dbHost, int dbPort, string dbName, string dbUser, string dbPassword)
|
||||
{
|
||||
// Hämta connection till produktion
|
||||
using var prodContext = ApplicationDbContextFactory.CreateWithConfig(_env, true);
|
||||
var connStr = $"Server={dbHost};Port={dbPort};Database={dbName};Uid={dbUser};Pwd={dbPassword};";
|
||||
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
optionsBuilder.UseMySql(connStr, ServerVersion.AutoDetect(connStr), mySqlOptions => mySqlOptions.CommandTimeout(180));
|
||||
using var customContext = new ApplicationDbContext(optionsBuilder.Options);
|
||||
|
||||
// Importera definitioner först
|
||||
var prodCategoryDefs = prodContext.BudgetCategoryDefinitions.ToList();
|
||||
var prodItemDefs = prodContext.BudgetItemDefinitions.ToList();
|
||||
var customService = new MenuService(customContext);
|
||||
var devService = new MenuService(_context);
|
||||
|
||||
foreach (var def in prodCategoryDefs)
|
||||
try
|
||||
{
|
||||
var importedMeals = customService.GetMealsDetailed(); // Ska inkludera Ingredients
|
||||
foreach (var meal in importedMeals)
|
||||
{
|
||||
var newMeal = new Meal
|
||||
{
|
||||
Id = meal.Id, // 👈 Viktigt!
|
||||
Name = meal.Name,
|
||||
Description = meal.Description,
|
||||
ProteinType = meal.ProteinType,
|
||||
Category = meal.Category,
|
||||
CarbType = meal.CarbType,
|
||||
RecipeUrl = meal.RecipeUrl,
|
||||
ImageUrl = meal.ImageUrl,
|
||||
IsAvailable = meal.IsAvailable,
|
||||
CreatedAt = meal.CreatedAt,
|
||||
Instructions = meal.Instructions,
|
||||
ImageData = meal.ImageData,
|
||||
ImageMimeType = meal.ImageMimeType,
|
||||
Ingredients = meal.Ingredients.Select(i => new Ingredient
|
||||
{
|
||||
MealId = meal.Id, // 👈 Koppla till rätt måltid
|
||||
Quantity = i.Quantity,
|
||||
Item = i.Item
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
devService.SaveOrUpdateMealWithIngredients(newMeal);
|
||||
}
|
||||
|
||||
TempData["Message"] = $"✅ {importedMeals.Count} måltider importerade från extern databas.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TempData["Message"] = $"❌ Fel vid import: {ex.Message}";
|
||||
}
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public IActionResult ImportBudgetFromCustom(string dbHost, int dbPort, string dbName, string dbUser, string dbPassword)
|
||||
{
|
||||
var connStr = $"Server={dbHost};Port={dbPort};Database={dbName};Uid={dbUser};Pwd={dbPassword};";
|
||||
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
optionsBuilder.UseMySql(connStr, ServerVersion.AutoDetect(connStr));
|
||||
|
||||
using var sourceContext = new ApplicationDbContext(optionsBuilder.Options);
|
||||
|
||||
var categoryDefs = sourceContext.BudgetCategoryDefinitions.ToList();
|
||||
var itemDefs = sourceContext.BudgetItemDefinitions.ToList();
|
||||
|
||||
foreach (var def in categoryDefs)
|
||||
{
|
||||
if (!_context.BudgetCategoryDefinitions.Any(d => d.Name == def.Name))
|
||||
{
|
||||
@@ -199,11 +223,10 @@ namespace Aberwyn.Controllers
|
||||
Name = def.Name,
|
||||
Color = def.Color ?? "#cccccc"
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var def in prodItemDefs)
|
||||
foreach (var def in itemDefs)
|
||||
{
|
||||
if (!_context.BudgetItemDefinitions.Any(d => d.Name == def.Name))
|
||||
{
|
||||
@@ -211,19 +234,17 @@ namespace Aberwyn.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
_context.SaveChanges(); // Se till att ID:n finns för FK:n nedan
|
||||
_context.SaveChanges();
|
||||
|
||||
// Ladda definitioner i minnet för snabb lookup
|
||||
var devCategoryDefs = _context.BudgetCategoryDefinitions.ToList();
|
||||
var devItemDefs = _context.BudgetItemDefinitions.ToList();
|
||||
|
||||
// Importera budgetperioder med kategorier och items
|
||||
var prodPeriods = prodContext.BudgetPeriods
|
||||
var periods = sourceContext.BudgetPeriods
|
||||
.Include(p => p.Categories)
|
||||
.ThenInclude(c => c.Items)
|
||||
.ToList();
|
||||
|
||||
foreach (var period in prodPeriods)
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var exists = _context.BudgetPeriods
|
||||
.Any(p => p.Year == period.Year && p.Month == period.Month);
|
||||
@@ -259,11 +280,13 @@ namespace Aberwyn.Controllers
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
TempData["Message"] = "✅ Import av budgetdata från produktion är klar.";
|
||||
TempData["Message"] = $"✅ Import av budgetdata från extern databas klar ({periods.Count} månader).";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Todo
|
||||
|
||||
[HttpGet]
|
||||
@@ -314,9 +337,31 @@ namespace Aberwyn.Controllers
|
||||
|
||||
return Ok();
|
||||
}
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public IActionResult TestDbConnection(string dbHost, int dbPort, string dbName, string dbUser, string dbPassword)
|
||||
{
|
||||
var connStr = $"Server={dbHost};Port={dbPort};Database={dbName};Uid={dbUser};Pwd={dbPassword};";
|
||||
|
||||
try
|
||||
{
|
||||
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||
builder.UseMySql(connStr, ServerVersion.AutoDetect(connStr));
|
||||
|
||||
using var context = new ApplicationDbContext(builder.Options);
|
||||
context.Database.OpenConnection();
|
||||
context.Database.CloseConnection();
|
||||
|
||||
return Json(new { success = true, message = "✅ Anslutning lyckades!" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Json(new { success = false, message = $"❌ Anslutning misslyckades: {ex.Message}" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class AdminUserViewModel
|
||||
{
|
||||
public string UserId { get; set; }
|
||||
|
||||
Reference in New Issue
Block a user