diff --git a/CocktailWeb/Data/DbDataContext.cs b/CocktailWeb/Data/DbDataContext.cs index 527ac7d..6b6ee00 100644 --- a/CocktailWeb/Data/DbDataContext.cs +++ b/CocktailWeb/Data/DbDataContext.cs @@ -10,8 +10,7 @@ namespace CocktailWeb.Data public DbSet CocktailFlaschen { get; set; } public DbSet Cocktails { get; set; } public DbSet Fillers { get; set; } - - public DbSet Glaeser { get; set; } + public DbSet Glasses { get; set; } public DbDataContext(IConfiguration configuration) { diff --git a/CocktailWeb/Data/Glas.cs b/CocktailWeb/Data/Glas.cs index a66d0d6..7e4c794 100644 --- a/CocktailWeb/Data/Glas.cs +++ b/CocktailWeb/Data/Glas.cs @@ -3,9 +3,13 @@ public class Glas { public int Id { get; set; } - public string Name { get + public string? Name { get; set; } + public string DisplayName { get { - return $"{Fuellmenge} ml"; + string name = ""; + if (Name != null) name += $"{Name} - "; + name += $"{Fuellmenge} ml"; + return name; } } public int Fuellmenge { get; set; } diff --git a/CocktailWeb/Data/cocktails.db b/CocktailWeb/Data/cocktails.db index 60277f2..c7871a3 100644 Binary files a/CocktailWeb/Data/cocktails.db and b/CocktailWeb/Data/cocktails.db differ diff --git a/CocktailWeb/Data/cocktails.db-shm b/CocktailWeb/Data/cocktails.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/CocktailWeb/Data/cocktails.db-shm differ diff --git a/CocktailWeb/Data/cocktails.db-wal b/CocktailWeb/Data/cocktails.db-wal new file mode 100644 index 0000000..e69de29 diff --git a/CocktailWeb/Pages/Settings/GlasEdit.razor b/CocktailWeb/Pages/Settings/GlasEdit.razor new file mode 100644 index 0000000..2d5b61c --- /dev/null +++ b/CocktailWeb/Pages/Settings/GlasEdit.razor @@ -0,0 +1,147 @@ +@page "/settings/glasses/edit" +@page "/settings/glasses/edit/{id}" +@using CocktailWeb.Data +@using Microsoft.AspNetCore.Components.Sections +@using Microsoft.EntityFrameworkCore +@using Microsoft.Extensions.Options +@inject IDbContextFactory DataContextFactory; +@inject NavigationManager nav; +@inject IWebHostEnvironment env; +@inject IOptions Config; + + + + + + +
+
+
+ + +
+
+ +
+ + ml + + +
+
+
+ + +
+
+
+ +
+ @if (curGlas.ImageURL != null) + { + + } + else + { +
Kein Bild vorhanden
+ } +
+ + +
+
+
+
+ +@code { + [Parameter] + public string? id { get; set; } + + public Glas curGlas = null!; + + public IBrowserFile? NewImage; + + private DbDataContext? _DataContext; + + private string PrevPageURL = "/settings/glasses"; + + EditContext? editContext; + ValidationMessageStore? valMessages; + + protected override async Task OnInitializedAsync() + { + if (id != null) + { + _DataContext ??= await DataContextFactory.CreateDbContextAsync(); + if (_DataContext != null) curGlas = _DataContext.Glasses.Single(f => f.Id == Convert.ToInt32(id)); + } + curGlas ??= new(); // Falls keine ID angegeben wurde oder der Eintrag in der Datenbank nicht gefunden wurde, gehen wir davon aus dass ein neuer Eintrag erstellt wird. + + editContext = new(curGlas); + editContext.OnValidationRequested += editContext_OnValidationRequested; + valMessages = new(editContext); + } + + private void Bild_OnChange(InputFileChangeEventArgs e) + { + NewImage = e.GetMultipleFiles().FirstOrDefault(); + } + + private void ModifyAmount(int amount) + { + curGlas.Fuellmenge = Math.Max(curGlas.Fuellmenge + amount, 0); + } + + private void editContext_OnValidationRequested(object? sender, ValidationRequestedEventArgs args) + { + valMessages?.Clear(); + if (curGlas.Fuellmenge <= 0) valMessages?.Add(() => curGlas.Fuellmenge, "Füllmenge muss größer 0 sein!"); + } + + + private async Task OnSubmit() + { + _DataContext ??= await DataContextFactory.CreateDbContextAsync(); + + if (curGlas != null && _DataContext != null) + { + if (id != null) + { + _DataContext.Glasses.Update(curGlas); + } + else + { + _DataContext.Glasses.Add(curGlas); + } + await _DataContext.SaveChangesAsync(); + + if (NewImage != null) + { + try + { + // Bild hochladen + var relativeuploadpath = Path.Join(Config.Value.ImageUploadDir, $"glas_{curGlas.Id}{Path.GetExtension(NewImage.Name)}"); + var fullpath = Path.Join(env.WebRootPath, relativeuploadpath); + string? folder = Path.GetDirectoryName(fullpath); + if (folder != null && !Path.Exists(folder)) Directory.CreateDirectory(folder); + + Stream stream = NewImage.OpenReadStream(maxAllowedSize: Config.Value.MaxAllowedUploadSizeInMB * 1024 * 1024); + FileStream fs = File.Create(fullpath); + await stream.CopyToAsync(fs); + stream.Close(); + fs.Close(); + curGlas.ImageURL = relativeuploadpath; + // URL hinterlegen und wieder speichern + _DataContext.Glasses.Update(curGlas); + await _DataContext.SaveChangesAsync(); + } + catch (Exception ex) + { + Console.WriteLine($"Error uploading image: {ex.Message}"); + } + } + } + nav.NavigateTo(PrevPageURL); + } + +} diff --git a/CocktailWeb/Pages/Settings/Glasses.razor b/CocktailWeb/Pages/Settings/Glasses.razor index 975bcb4..5bcb5f0 100644 --- a/CocktailWeb/Pages/Settings/Glasses.razor +++ b/CocktailWeb/Pages/Settings/Glasses.razor @@ -1,10 +1,90 @@ @page "/settings/glasses" +@using CocktailWeb.Data @using Microsoft.AspNetCore.Components.Sections +@using Microsoft.EntityFrameworkCore +@inject IDbContextFactory DataContextFactory + +Glas hinzufügen + +
+ + + @foreach (Glas g in GlassList) + { + + + + + + } + +
@g.DisplayName + Bearbeiten + +
+
+ + + Löschen bestätigen + + Willst du das Glas wirklich löschen? + +
+ + +
+
+ @code { + private List GlassList = new(); + private DbDataContext? _DataContext; + + private ModalComponent modal = null!; + private Glas? SelectedGlas; + + protected override async Task OnInitializedAsync() + { + await ShowGlassesAsync(); + } + + private async Task ShowGlassesAsync() + { + _DataContext ??= await DataContextFactory.CreateDbContextAsync(); + if (_DataContext != null) + { + GlassList = _DataContext.Glasses.OrderBy(g => g.Fuellmenge).ToList(); + } + } + + private async Task ConfirmDeleteAsync(Glas g) + { + SelectedGlas = g; + await modal.ShowAsync(); + } + + private async Task DeleteGlassAsync() + { + if (SelectedGlas == null) return; + _DataContext ??= await DataContextFactory.CreateDbContextAsync(); + if (_DataContext != null) + { + _DataContext.Glasses.Remove(SelectedGlas); + await _DataContext.SaveChangesAsync(); + await ShowGlassesAsync(); + } + await CloseDialogAsync(); + } + + private async Task CloseDialogAsync() + { + SelectedGlas = null; + await modal.CloseAsync(); + } + }