EF conversion and local DB
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Elias Jansson
2025-06-02 23:32:28 +02:00
parent 3b0ea79748
commit 5169889753
62 changed files with 1332 additions and 9428 deletions

View File

@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Aberwyn.Models;
using Aberwyn.Data;
using Microsoft.EntityFrameworkCore;
namespace Aberwyn.Controllers
{
@@ -100,62 +101,225 @@ namespace Aberwyn.Controllers
return RedirectToAction("Index");
}
[HttpPost]
[Authorize(Roles = "Admin")]
public IActionResult ImportMealsFromProd()
{
var prodService = MenuService.CreateWithConfig(_configuration, _env, useProdDb: true);
var devService = new MenuService(_context); // injicerad context används
var prodMeals = prodService.GetMealsDetailed();
foreach (var meal in prodMeals)
{
// Kopiera utan ID (för att undvika konflikt) och spara
var newMeal = new Meal
[Authorize(Roles = "Admin")]
public IActionResult ImportMealsFromProd()
{
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
var prodService = MenuService.CreateWithConfig(_configuration, _env, useProdDb: true);
var devService = new MenuService(_context); // injicerad context används
var prodMeals = prodService.GetMealsDetailed();
foreach (var meal in prodMeals)
{
Quantity = i.Quantity,
Item = i.Item
}).ToList()
};
// 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);
}
devService.SaveOrUpdateMealWithIngredients(newMeal);
}
return Content("Import klar!");
}
[HttpPost]
[Authorize(Roles = "Admin")]
public IActionResult ImportMenusFromProd()
{
var prodService = MenuService.CreateWithConfig(_configuration, _env, useProdDb: true);
var devService = new MenuService(_context);
return Content("Import klar!");
}
var allProdMenus = prodService.GetAllWeeklyMenus();
var allMeals = devService.GetMeals();
[HttpPost]
[Authorize(Roles = "Admin")]
public IActionResult ImportMenusFromProd()
{
var prodService = MenuService.CreateWithConfig(_configuration, _env, useProdDb: true);
var devService = new MenuService(_context);
var allProdMenus = prodService.GetAllWeeklyMenus();
var allMeals = devService.GetMeals();
foreach (var menu in allProdMenus)
{
var newMenu = new WeeklyMenu
{
DayOfWeek = menu.DayOfWeek,
WeekNumber = menu.WeekNumber,
Year = menu.Year,
Cook = menu.Cook,
BreakfastMealId = null,
LunchMealId = null,
DinnerMealId = null
};
if (!string.IsNullOrEmpty(menu.BreakfastMealName))
newMenu.BreakfastMealId = allMeals.FirstOrDefault(m => m.Name == menu.BreakfastMealName)?.Id;
if (!string.IsNullOrEmpty(menu.LunchMealName))
newMenu.LunchMealId = allMeals.FirstOrDefault(m => m.Name == menu.LunchMealName)?.Id;
if (!string.IsNullOrEmpty(menu.DinnerMealName))
newMenu.DinnerMealId = allMeals.FirstOrDefault(m => m.Name == menu.DinnerMealName)?.Id;
_context.WeeklyMenus.Add(newMenu);
}
_context.SaveChanges();
TempData["Message"] = "Import av veckomenyer klar.";
return RedirectToAction("Index");
}
[HttpPost]
[Authorize(Roles = "Admin")]
public IActionResult ImportBudgetFromProd()
{
// Hämta connection till produktion
using var prodContext = ApplicationDbContextFactory.CreateWithConfig(_configuration, _env, useProdDb: true);
// Importera definitioner först
var prodCategoryDefs = prodContext.BudgetCategoryDefinitions.ToList();
var prodItemDefs = prodContext.BudgetItemDefinitions.ToList();
foreach (var def in prodCategoryDefs)
{
if (!_context.BudgetCategoryDefinitions.Any(d => d.Name == def.Name))
{
_context.BudgetCategoryDefinitions.Add(new BudgetCategoryDefinition
{
Name = def.Name,
Color = def.Color ?? "#cccccc"
});
}
}
foreach (var def in prodItemDefs)
{
if (!_context.BudgetItemDefinitions.Any(d => d.Name == def.Name))
{
_context.BudgetItemDefinitions.Add(new BudgetItemDefinition { Name = def.Name });
}
}
_context.SaveChanges(); // Se till att ID:n finns för FK:n nedan
// 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
.Include(p => p.Categories)
.ThenInclude(c => c.Items)
.ToList();
foreach (var period in prodPeriods)
{
var exists = _context.BudgetPeriods
.Any(p => p.Year == period.Year && p.Month == period.Month);
if (exists)
continue;
var newPeriod = new BudgetPeriod
{
Year = period.Year,
Month = period.Month,
Categories = period.Categories.Select(c => new BudgetCategory
{
Name = c.Name,
Color = string.IsNullOrWhiteSpace(c.Color) ? "#cccccc" : c.Color,
Order = c.Order,
BudgetCategoryDefinitionId = devCategoryDefs
.FirstOrDefault(d => d.Name == c.Definition?.Name)?.Id,
Items = c.Items.Select(i => new BudgetItem
{
Name = i.Name,
Amount = i.Amount,
IsExpense = i.IsExpense,
IncludeInSummary = i.IncludeInSummary,
Order = i.Order,
BudgetItemDefinitionId = devItemDefs
.FirstOrDefault(d => d.Name == i.BudgetItemDefinition?.Name)?.Id
}).ToList()
}).ToList()
};
_context.BudgetPeriods.Add(newPeriod);
}
_context.SaveChanges();
TempData["Message"] = "✅ Import av budgetdata från produktion är klar.";
return RedirectToAction("Index");
}
}
public class AdminUserViewModel
{
public string UserId { get; set; }
public string Email { get; set; }
public List<string> Roles { get; set; }
//Todo
[HttpGet]
public IActionResult Todo()
{
return View();
}
[HttpGet]
public IActionResult GetTodoTasks()
{
var tasks = _context.TodoTasks
.OrderByDescending(t => t.CreatedAt)
.ToList();
return Json(tasks);
}
[HttpPost]
public IActionResult AddTodoTask([FromBody] TodoTask task)
{
if (string.IsNullOrWhiteSpace(task?.Title))
return BadRequest("Titel krävs");
task.CreatedAt = DateTime.UtcNow;
_context.TodoTasks.Add(task);
_context.SaveChanges();
return Json(task);
}
[HttpPost]
public IActionResult UpdateTodoTask([FromBody] TodoTask task)
{
var existing = _context.TodoTasks.FirstOrDefault(t => t.Id == task.Id);
if (existing == null)
return NotFound();
existing.Title = task.Title;
existing.Status = task.Status;
existing.Priority = task.Priority;
_context.SaveChanges();
return Ok();
}
}
public class AdminUserViewModel
{
public string UserId { get; set; }
public string Email { get; set; }
public List<string> Roles { get; set; }
}
}