From 380978959b44ad19d95b80a0f8f2ca1341b332d6 Mon Sep 17 00:00:00 2001 From: elias Date: Mon, 7 Jul 2025 09:37:56 +0200 Subject: [PATCH] Lab changes --- Aberwyn/Controllers/MealController.cs | 57 ++++-- Aberwyn/Data/MenuService.cs | 34 +++- Aberwyn/Views/Meal/Lab.cshtml | 242 +++++++++++++++++--------- Aberwyn/wwwroot/css/lab.css | 116 ++++++++++++ 4 files changed, 346 insertions(+), 103 deletions(-) create mode 100644 Aberwyn/wwwroot/css/lab.css diff --git a/Aberwyn/Controllers/MealController.cs b/Aberwyn/Controllers/MealController.cs index ddd1da4..e71ca72 100644 --- a/Aberwyn/Controllers/MealController.cs +++ b/Aberwyn/Controllers/MealController.cs @@ -199,27 +199,48 @@ namespace Aberwyn.Controllers [HttpGet("/meal/lab")] - public IActionResult Lab(int? id) + public IActionResult Lab(int? id) + { + if (id.HasValue) { - if (id.HasValue) - { - var entry = _menuService.GetRecipeLabEntryById(id.Value); - if (entry == null) return NotFound(); + var entry = _menuService.GetRecipeLabEntryById(id.Value); + if (entry == null) return NotFound(); - return View("Lab", entry); - } - - // Skapa ett tomt labb-entry för formulär - var newEntry = new RecipeLabEntry - { - Title = "", - Inspiration = "", - Notes = "", - Tags = "" - }; - - return View("Lab", newEntry); + // Hämta versioner först när vi vet att entry finns + entry.Versions = _menuService.GetLabVersionsForEntry(id.Value); + return View("Lab", entry); } + + // Skapa ett tomt labb-entry för formulär + var newEntry = new RecipeLabEntry + { + Title = "", + Inspiration = "", + Notes = "", + Tags = "" + }; + + return View("Lab", newEntry); + } + // Lägg till dessa actions i din MealController + + [HttpPost("/meal/lab/save")] + public IActionResult SaveLabEntry(RecipeLabEntry entry) + { + if (entry.Id == 0) + { + // Ny entry + entry.CreatedAt = DateTime.Now; + _menuService.AddLabEntry(entry); + } + else + { + // Uppdatera befintlig + _menuService.UpdateLabEntry(entry); + } + + return RedirectToAction("Lab", new { id = entry.Id }); + } [HttpPost("/meal/lab/create")] public IActionResult CreateLabEntry(RecipeLabEntry entry) { diff --git a/Aberwyn/Data/MenuService.cs b/Aberwyn/Data/MenuService.cs index 2d5e862..79bde2d 100644 --- a/Aberwyn/Data/MenuService.cs +++ b/Aberwyn/Data/MenuService.cs @@ -404,7 +404,7 @@ public List GetWeeklyMenu(int weekNumber, int year) .ToList(); } - + public List GetMenuEntriesByDateRange(DateTime startDate, DateTime endDate) { var results = new List(); @@ -507,6 +507,28 @@ public List GetWeeklyMenu(int weekNumber, int year) _context.LabVersionIngredients.AddRange(ingredients); _context.SaveChanges(); } + // Lägg till dessa metoder i din MenuService klass + + public void UpdateLabEntry(RecipeLabEntry entry) + { + var existing = _context.RecipeLabEntries.Find(entry.Id); + if (existing != null) + { + existing.Title = entry.Title; + existing.Inspiration = entry.Inspiration; + existing.Notes = entry.Notes; + existing.Tags = entry.Tags; + _context.SaveChanges(); + } + } + + public RecipeLabEntry GetRecipeLabEntryWithIngredients(int id) + { + return _context.RecipeLabEntries + .Include(e => e.Versions) + .Include(e => e.Ingredients) + .FirstOrDefault(e => e.Id == id); + } public void SaveIngredientsForLabEntry(int labEntryId, List ingredients) { var existing = _context.LabIngredients @@ -522,6 +544,16 @@ public List GetWeeklyMenu(int weekNumber, int year) _context.SaveChanges(); } + public List GetLabVersionsForEntry(int entryId) + { + return _context.RecipeLabVersions + .Where(v => v.RecipeLabEntryId == entryId) + .Include(v => v.Ingredients) + .OrderByDescending(v => v.CreatedAt) + .ToList(); + } + + #endregion } diff --git a/Aberwyn/Views/Meal/Lab.cshtml b/Aberwyn/Views/Meal/Lab.cshtml index fcbd7a7..4e94887 100644 --- a/Aberwyn/Views/Meal/Lab.cshtml +++ b/Aberwyn/Views/Meal/Lab.cshtml @@ -1,99 +1,173 @@ @model Aberwyn.Models.RecipeLabEntry + @{ ViewData["Title"] = "Matlabb"; } -

Matlabb: @Model.Title

+ -
- +
+ +
+

Matlabb – @(string.IsNullOrEmpty(Model.Title) ? "Ny" : Model.Title)

-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- - - -
- -

Ingredienser

-
- - -
- @for (int i = 0; i < Model.Ingredients.Count; i++) - { -
- - + + +
+ +
- } -
- - +
+ + +
+
+ + +
+
+ + +
+ + -
-

Versioner

-
- - -
- - -
- - -
- -@if (Model.Versions.Any()) -{ -
- @foreach (var version in Model.Versions) + @if (Model.Id > 0) { -
-
- @version.VersionLabel (@version.CreatedAt.ToShortDateString()) -
-
-
Ingredienser
- @if (version.Ingredients != null && version.Ingredients.Any()) - { -
    - @foreach (var ing in version.Ingredients) +
    + + +

    Ingredienser

    +
    +
    + +
    + @if (Model.Ingredients?.Any() == true) + { + @for (int i = 0; i < Model.Ingredients.Count; i++) { -
  • @ing.Quantity @ing.Item
  • +
    + + + +
    } -
- } - else - { - Inga ingredienser kopplade. - } - -
Instruktioner
-

@version.Instructions

- -
Kommentar
-

@version.ResultNotes

-
+ } + else + { +
+ + + +
+ } +
+ + +
+ +
+ + +

Ny version

+
+ +
+ + +
+
+ + +
+
+ + +
+ +
+ } + else + { +

Spara labbet först för att kunna lägga till ingredienser och versioner.

}
-} \ No newline at end of file + + +
+

Versioner

+ @if (Model.Versions?.Any() == true) + { +
+ @foreach (var version in Model.Versions.OrderByDescending(v => v.CreatedAt)) + { +
+

@version.VersionLabel

+

Skapat: @version.CreatedAt.ToString("yyyy-MM-dd")

+ + @if (!string.IsNullOrEmpty(version.Instructions)) + { +

Instruktioner:
@version.Instructions

+ } + + @if (!string.IsNullOrEmpty(version.ResultNotes)) + { +

Resultat:
@version.ResultNotes

+ } + + @if (version.Ingredients?.Any() == true) + { +

Ingredienser:

+
    + @foreach (var ing in version.Ingredients) + { +
  • @ing.Quantity @ing.Item
  • + } +
+ } +
+ } +
+ } + else + { +

Inga tidigare versioner ännu.

+ } +
+
+ + \ No newline at end of file diff --git a/Aberwyn/wwwroot/css/lab.css b/Aberwyn/wwwroot/css/lab.css new file mode 100644 index 0000000..68a85c7 --- /dev/null +++ b/Aberwyn/wwwroot/css/lab.css @@ -0,0 +1,116 @@ +.lab-flex-layout { + display: flex; + gap: 24px; + padding: 24px; + box-sizing: border-box; +} + +.lab-section { + background: #fff; + padding: 16px; + border-radius: 8px; + box-shadow: 0 2px 6px rgba(0,0,0,0.1); + flex-grow: 1; + min-width: 0; + color: #1F2C3C; +} + +.lab-section.info { + flex: 1; +} + +.lab-section.ingredients { + flex: 2; +} + +.lab-section.versions { + flex: 1; + max-width: 300px; + overflow-y: auto; + position: sticky; + top: 70px; +} + +.lab-section h2 { + margin-top: 0; + font-size: 1.2rem; +} + +.lab-field { + margin-bottom: 12px; + display: flex; + flex-direction: column; +} + +.lab-field input, +.lab-field textarea { + padding: 6px; + border-radius: 4px; + border: 1px solid #ccc; + font-size: 14px; +} + +.ingredient-row { + display: flex; + gap: 10px; + margin-bottom: 8px; +} + +.ingredient-row input { + flex: 1; +} + +.add-version-form { + margin-top: 24px; +} + +.lab-version { + border-top: 1px solid #ddd; + padding-top: 12px; + margin-top: 12px; +} + +.version-notes { + font-style: italic; + margin-bottom: 4px; +} + +.version-result { + margin-bottom: 6px; +} +input[type="text"], +input[type="number"], +textarea { + width: 100%; + padding: 10px; + font-size: 14px; + border-radius: 6px; + border: 1px solid #ccc; + box-sizing: border-box; + margin-bottom: 12px; + background: #f9f9f9; +} + +input:focus, +textarea:focus { + outline: none; + border-color: #6a0dad; + background-color: #fff; +} + +button { + background-color: #6a0dad; + color: #fff; + padding: 10px 16px; + border: none; + border-radius: 6px; + font-weight: bold; + font-size: 14px; + cursor: pointer; + margin-top: 12px; + transition: background 0.2s ease; +} + +button:hover { + background-color: #5a0ca0; +}