From 465f9afc997c18ccc87a2967ea28dadcffd3bc3f Mon Sep 17 00:00:00 2001 From: Elias Jansson Date: Tue, 10 Jun 2025 18:59:04 +0200 Subject: [PATCH] Bunch of fixes and session extensions --- Aberwyn.sln | 4 -- .../Identity/Pages/Account/Login.cshtml.cs | 2 +- Aberwyn/Controllers/MealController.cs | 26 +++++++--- Aberwyn/Data/MenuService.cs | 47 +++++++++++++++++-- Aberwyn/Program.cs | 8 +++- Aberwyn/Views/FoodMenu/Veckomeny.cshtml | 5 +- Aberwyn/Views/Home/Offline.cshtml | 14 ++++++ Aberwyn/Views/Home/Offline.cshtml.cs | 12 +++++ Aberwyn/Views/Meal/View.cshtml | 37 +++------------ Aberwyn/wwwroot/manifest.json | 2 +- 10 files changed, 106 insertions(+), 51 deletions(-) create mode 100644 Aberwyn/Views/Home/Offline.cshtml create mode 100644 Aberwyn/Views/Home/Offline.cshtml.cs diff --git a/Aberwyn.sln b/Aberwyn.sln index ad2809d..2e7b639 100644 --- a/Aberwyn.sln +++ b/Aberwyn.sln @@ -11,10 +11,6 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {423CD237-7404-4355-868F-CE5861835258}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {423CD237-7404-4355-868F-CE5861835258}.Debug|Any CPU.Build.0 = Debug|Any CPU - {423CD237-7404-4355-868F-CE5861835258}.Release|Any CPU.ActiveCfg = Release|Any CPU - {423CD237-7404-4355-868F-CE5861835258}.Release|Any CPU.Build.0 = Release|Any CPU {F5586986-B726-4E05-B31B-2E24CA5B2B89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F5586986-B726-4E05-B31B-2E24CA5B2B89}.Debug|Any CPU.Build.0 = Debug|Any CPU {F5586986-B726-4E05-B31B-2E24CA5B2B89}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/Aberwyn/Areas/Identity/Pages/Account/Login.cshtml.cs b/Aberwyn/Areas/Identity/Pages/Account/Login.cshtml.cs index 7b28fcc..18a2f60 100644 --- a/Aberwyn/Areas/Identity/Pages/Account/Login.cshtml.cs +++ b/Aberwyn/Areas/Identity/Pages/Account/Login.cshtml.cs @@ -99,7 +99,7 @@ namespace Aberwyn.Areas.Identity.Pages.Account { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true - var result = await _signInManager.PasswordSignInAsync(Input.UserName, Input.Password, Input.RememberMe, lockoutOnFailure: false); + var result = await _signInManager.PasswordSignInAsync(Input.UserName, Input.Password, isPersistent: true, lockoutOnFailure: false); if (result.Succeeded) { _logger.LogInformation("User logged in."); diff --git a/Aberwyn/Controllers/MealController.cs b/Aberwyn/Controllers/MealController.cs index da572a9..9272520 100644 --- a/Aberwyn/Controllers/MealController.cs +++ b/Aberwyn/Controllers/MealController.cs @@ -20,21 +20,33 @@ namespace Aberwyn.Controllers } [HttpGet] - public IActionResult View(int id, bool edit = false) + public IActionResult View(int? id, bool edit = false) { - var service = _menuService; - var meal = service.GetMealById(id); + Meal meal; - ViewData["IsEditing"] = edit; // → här - - if (meal == null) + if (id.HasValue) { - return NotFound(); + meal = _menuService.GetMealById(id.Value); + if (meal == null) + return NotFound(); + } + else + { + meal = new Meal + { + Name = "", + Description = "", + Ingredients = new List(), + IsAvailable = true, + CreatedAt = DateTime.Now + }; } + ViewData["IsEditing"] = edit; return View("View", meal); } + [HttpGet] [Route("Meal/Tooltip/{id}")] public IActionResult Tooltip(int id) diff --git a/Aberwyn/Data/MenuService.cs b/Aberwyn/Data/MenuService.cs index 0329304..4ba0483 100644 --- a/Aberwyn/Data/MenuService.cs +++ b/Aberwyn/Data/MenuService.cs @@ -77,9 +77,7 @@ public class MenuService } - - - public void SaveMeal(Meal meal) + public void SaveMeal2(Meal meal) { if (string.IsNullOrWhiteSpace(meal?.Name)) return; @@ -104,7 +102,38 @@ public class MenuService _context.SaveChanges(); } -public int GenerateMissingThumbnails() + public void SaveMeal(Meal meal) + { + if (string.IsNullOrWhiteSpace(meal?.Name)) return; + + meal.Name = meal.Name.Trim(); + meal.CreatedAt = meal.CreatedAt == default ? DateTime.Now : meal.CreatedAt; + + if (meal.Id == 0) + { + // Ny måltid + _context.Meals.Add(meal); + } + else + { + // Uppdatera existerande utan tracking-krockar + var existing = _context.Meals + .Include(m => m.Ingredients) + .FirstOrDefault(m => m.Id == meal.Id); + + if (existing != null) + { + _context.Entry(existing).CurrentValues.SetValues(meal); + + // OBS: Ingredienser hanteras separat + } + } + + _context.SaveChanges(); + } + + + public int GenerateMissingThumbnails() { var updatedCount = 0; var meals = _context.Meals @@ -173,10 +202,17 @@ public int GenerateMissingThumbnails() { var existing = _context.Ingredients.Where(i => i.MealId == mealId); _context.Ingredients.RemoveRange(existing); + + foreach (var ing in ingredients) + { + ing.MealId = mealId; + } + _context.Ingredients.AddRange(ingredients); _context.SaveChanges(); } + public List GetMeals() { return _context.Meals.ToList(); @@ -216,6 +252,8 @@ public int GenerateMissingThumbnails() public void SaveOrUpdateMealWithIngredients(Meal meal) { + var isNew = meal.Id == 0; + SaveMeal(meal); if (meal.Ingredients != null && meal.Ingredients.Count > 0) @@ -224,6 +262,7 @@ public int GenerateMissingThumbnails() } } + public List GetMealsByCategory(string category) { return _context.Meals diff --git a/Aberwyn/Program.cs b/Aberwyn/Program.cs index 58dcb22..876091a 100644 --- a/Aberwyn/Program.cs +++ b/Aberwyn/Program.cs @@ -149,6 +149,9 @@ builder.Services.Configure(builder.Configuration.GetSection("Vapid builder.Services.ConfigureApplicationCookie(options => { options.LoginPath = "/Identity/Account/Login"; + options.ExpireTimeSpan = TimeSpan.FromDays(30); + options.SlidingExpiration = true; + options.Cookie.IsEssential = true; }); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); @@ -244,6 +247,9 @@ if (setup.IsConfigured) await IdentityDataInitializer.SeedData(services, setup); } } - +if (app.Environment.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); // Detta ger stacktraces i browsern +} app.Run(); diff --git a/Aberwyn/Views/FoodMenu/Veckomeny.cshtml b/Aberwyn/Views/FoodMenu/Veckomeny.cshtml index 8760389..7c79926 100644 --- a/Aberwyn/Views/FoodMenu/Veckomeny.cshtml +++ b/Aberwyn/Views/FoodMenu/Veckomeny.cshtml @@ -143,8 +143,9 @@ - @foreach (var m in ViewBag.AvailableMeals as List) { - diff --git a/Aberwyn/Views/Home/Offline.cshtml b/Aberwyn/Views/Home/Offline.cshtml new file mode 100644 index 0000000..a12e504 --- /dev/null +++ b/Aberwyn/Views/Home/Offline.cshtml @@ -0,0 +1,14 @@ + + + + + Offline + + + +

🌐 Du är offline

+

LEWEL är inte tillgänglig just nu. Kontrollera din internetanslutning och försök igen.

+ + diff --git a/Aberwyn/Views/Home/Offline.cshtml.cs b/Aberwyn/Views/Home/Offline.cshtml.cs new file mode 100644 index 0000000..9577482 --- /dev/null +++ b/Aberwyn/Views/Home/Offline.cshtml.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace Aberwyn.Views.Home +{ + public class OfflineModel : PageModel + { + public void OnGet() + { + } + } +} diff --git a/Aberwyn/Views/Meal/View.cshtml b/Aberwyn/Views/Meal/View.cshtml index 5f1c96e..8ae2584 100644 --- a/Aberwyn/Views/Meal/View.cshtml +++ b/Aberwyn/Views/Meal/View.cshtml @@ -64,6 +64,10 @@ +
+ + +
@@ -79,7 +83,7 @@
- @for (int i = 0; i < Model.Ingredients.Count; i++) + @for (int i = 0; i < (Model.Ingredients?.Count ?? 0); i++) {
@@ -215,7 +219,7 @@ closeImageModal(); } }); - $(document).ready(function () { + $(document).ready(function () { if ($('#Instructions').length > 0) { $('#Instructions').trumbowyg({ autogrow: true, @@ -223,35 +227,6 @@ }); } }); - if ('serviceWorker' in navigator && 'PushManager' in window) { - navigator.serviceWorker.register('/service-worker.js') - .then(function (registration) { - console.log('Service Worker registered', registration); - return registration.pushManager.getSubscription() - .then(async function (subscription) { - if (subscription) { - console.log('Already subscribed to push notifications.'); - return subscription; - } - const response = await fetch('/api/push/vapid-public-key'); - const vapidPublicKey = await response.text(); - const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey); - - return registration.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey: convertedVapidKey - }); - }); - }).then(function (subscription) { - return fetch('/api/push/subscribe', { - method: 'POST', - body: JSON.stringify(subscription), - headers: { - 'Content-Type': 'application/json' - } - }); - }); - } diff --git a/Aberwyn/wwwroot/manifest.json b/Aberwyn/wwwroot/manifest.json index fe813b3..9be370e 100644 --- a/Aberwyn/wwwroot/manifest.json +++ b/Aberwyn/wwwroot/manifest.json @@ -4,7 +4,7 @@ "start_url": "/", "display": "standalone", "background_color": "#1F2C3C", - "theme_color": "#6a0dad", + "theme_color": "#1F2C3C", "icons": [ { "src": "/images/lewel-icon.png",