This commit is contained in:
@@ -2,20 +2,42 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Aberwyn.Models;
|
||||
using Aberwyn.Data;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
|
||||
namespace Aberwyn.Controllers
|
||||
{
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
public class AdminController : Controller
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly RoleManager<IdentityRole> _roleManager;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IHostEnvironment _env;
|
||||
private readonly MenuService _menuService;
|
||||
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public AdminController(
|
||||
IConfiguration configuration,
|
||||
IHostEnvironment env,
|
||||
MenuService menuService,
|
||||
ApplicationDbContext context,
|
||||
UserManager<ApplicationUser> userManager,
|
||||
RoleManager<IdentityRole> roleManager)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_env = env;
|
||||
_menuService = menuService;
|
||||
_context = context;
|
||||
_userManager = userManager;
|
||||
_roleManager = roleManager;
|
||||
}
|
||||
|
||||
|
||||
public AdminController(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_roleManager = roleManager;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
@@ -86,6 +108,84 @@ 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
|
||||
{
|
||||
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")]
|
||||
[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,
|
||||
BreakfastMealId = menu.BreakfastMealName != null
|
||||
? allMeals.FirstOrDefault(m => m.Name == menu.BreakfastMealName)?.Id
|
||||
: null,
|
||||
LunchMealId = menu.LunchMealName != null
|
||||
? allMeals.FirstOrDefault(m => m.Name == menu.LunchMealName)?.Id
|
||||
: null,
|
||||
DinnerMealId = menu.DinnerMealName != null
|
||||
? allMeals.FirstOrDefault(m => m.Name == menu.DinnerMealName)?.Id
|
||||
: null,
|
||||
WeekNumber = menu.WeekNumber,
|
||||
Year = menu.Year,
|
||||
Cook = menu.Cook
|
||||
};
|
||||
|
||||
_context.WeeklyMenus.Add(newMenu);
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
TempData["Message"] = "Import av veckomenyer klar.";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class AdminUserViewModel
|
||||
|
||||
Reference in New Issue
Block a user