diff --git a/CocktailWeb/Pages/Settings/Flaschen.razor b/CocktailWeb/Pages/Settings/Flaschen.razor deleted file mode 100644 index a1b0985..0000000 --- a/CocktailWeb/Pages/Settings/Flaschen.razor +++ /dev/null @@ -1,73 +0,0 @@ -@using Microsoft.AspNetCore.Components.Sections -@using Microsoft.EntityFrameworkCore -@using CocktailWeb.Data -@page "/settings/flaschen" -@inject IDbContextFactory FlascheDataContextFactory; - -Flaschen - - - - - - -@if (CreateFormVisible && FlascheToCreate != null) -{ -

Flasche hinzufügen

-
- -
- -
-
-
-
- -
-
-} -else -{ - - -} - -@if (FlaschenListe != null && FlaschenListe.Count > 0) -{ -
- - - - - - - - - @foreach (var flasche in FlaschenListe) - { - @if (EditFormVisible && FlascheToUpdate != null && FlascheToUpdate.Id == flasche.Id) - { - - - - - } - else - { - - - - - } - } - -
NameAction
@flasche.Name - - -
-
-} - -@code { - -} diff --git a/CocktailWeb/Pages/Settings/Flaschen.razor.cs b/CocktailWeb/Pages/Settings/Flaschen.razor.cs deleted file mode 100644 index f1c8ca1..0000000 --- a/CocktailWeb/Pages/Settings/Flaschen.razor.cs +++ /dev/null @@ -1,84 +0,0 @@ -using SQLitePCL; -using Microsoft.EntityFrameworkCore; -using CocktailWeb.Data; - -namespace CocktailWeb.Pages.Settings -{ - public partial class Flaschen - { - public bool CreateFormVisible { get; set; } - public bool EditFormVisible { get; set; } - - public Flasche? FlascheToCreate { get; set; } - public Flasche? FlascheToUpdate { get; set; } - - public List? FlaschenListe { get; set; } - - - private DbDataContext? _DataContext; - protected override async Task OnInitializedAsync() - { - CreateFormVisible = false; - await ShowFlaschen(); - } - - public void ShowCreateForm() - { - FlascheToCreate = new Flasche(); - CreateFormVisible = true; - } - - public async Task CreateNewFlasche() - { - _DataContext ??= await FlascheDataContextFactory.CreateDbContextAsync(); - - if (FlascheToCreate != null && _DataContext != null) - { - _DataContext.Flaschen.Add(FlascheToCreate); - await _DataContext.SaveChangesAsync(); - } - CreateFormVisible = false; - await ShowFlaschen(); - } - - public async Task ShowFlaschen() - { - _DataContext ??= await FlascheDataContextFactory.CreateDbContextAsync(); - if (_DataContext != null) - { - FlaschenListe = await _DataContext.Flaschen.OrderBy(f => f.Name).ToListAsync(); - } - - } - - public async Task ShowEditForm(Flasche flasche) - { - _DataContext ??= await FlascheDataContextFactory.CreateDbContextAsync(); - FlascheToUpdate = _DataContext.Flaschen.FirstOrDefault(f => f.Id == flasche.Id); - EditFormVisible = true; - } - - public async Task UpdateEmployee(Flasche flasche) - { - _DataContext ??= await FlascheDataContextFactory.CreateDbContextAsync(); - if (_DataContext != null && flasche != null) - { - _DataContext.Flaschen.Update(flasche); - await _DataContext.SaveChangesAsync(); - } - EditFormVisible = false; - } - - public async Task DeleteFlasche(Flasche flasche) - { - _DataContext ??= await FlascheDataContextFactory.CreateDbContextAsync(); - if (_DataContext != null && flasche != null) - { - _DataContext.Flaschen.Remove(flasche); - await _DataContext.SaveChangesAsync(); - } - await ShowFlaschen(); - } - - } -} diff --git a/CocktailWeb/Pages/Settings/IngredientEdit.razor b/CocktailWeb/Pages/Settings/IngredientEdit.razor new file mode 100644 index 0000000..c28bdd3 --- /dev/null +++ b/CocktailWeb/Pages/Settings/IngredientEdit.razor @@ -0,0 +1,127 @@ +@page "/settings/ingredients/edit" +@page "/settings/ingredients/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; + + + + + +
+
+
+ + +
+
+ Alkoholisch + +
+ + +
+
+
+ +
+ @if (curFlasche.ImageURL != null) + { + + } + else + { +
Kein Bild vorhanden
+ } +
+ + +
+
+
+ +@code { + [Parameter] + public string? id { get; set; } + + public Flasche curFlasche = null!; + + public IBrowserFile? NewImage; + + private DbDataContext? _DataContext; + + protected override async Task OnInitializedAsync() + { + if (id != null) + { + _DataContext ??= await DataContextFactory.CreateDbContextAsync(); + if (_DataContext != null) curFlasche = _DataContext.Flaschen.Single(f => f.Id == Convert.ToInt32(id)); + } + // Falls keine ID angegeben wurde oder der Eintrag in der Datenbank nicht gefunden wurde, gehen wir davon aus dass ein neuer Eintrag erstellt wird. + if (curFlasche == null) curFlasche = new(); + + //await InvokeAsync(StateHasChanged); + } + + public void Bild_OnChange(InputFileChangeEventArgs e) + { + NewImage = e.GetMultipleFiles().FirstOrDefault(); + //this.StateHasChanged(); War in nem Stackexchange.. Nötig? + } + + private async Task OnSubmit(EventArgs e) + { + _DataContext ??= await DataContextFactory.CreateDbContextAsync(); + + if (curFlasche != null && _DataContext != null) + { + if (id != null) + { + _DataContext.Flaschen.Update(curFlasche); + } + else + { + _DataContext.Flaschen.Add(curFlasche); + } + await _DataContext.SaveChangesAsync(); + + if (NewImage != null) + { + try + { + // Bild hochladen + var relativeuploadpath = Path.Join(Config.Value.ImageUploadDir, $"ingredient_{curFlasche.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(); + curFlasche.ImageURL = relativeuploadpath; + // URL hinterlegen und wieder speichern + _DataContext.Flaschen.Update(curFlasche); + await _DataContext.SaveChangesAsync(); + } + catch (Exception ex) + { + Console.WriteLine($"Error uploading image: {ex.Message}"); + } + } + } + nav.NavigateTo("/settings/ingredients"); + } + +} diff --git a/CocktailWeb/Pages/Settings/Ingredients.razor b/CocktailWeb/Pages/Settings/Ingredients.razor new file mode 100644 index 0000000..2d68f68 --- /dev/null +++ b/CocktailWeb/Pages/Settings/Ingredients.razor @@ -0,0 +1,140 @@ +@using Microsoft.AspNetCore.Components.Sections +@using Microsoft.EntityFrameworkCore +@using CocktailWeb.Data +@page "/settings/ingredients" +@inject IDbContextFactory FlascheDataContextFactory; + +Zutaten + + + + + +Neue Zutat + +@if (IngredientsList != null && IngredientsList.Count > 0) +{ +
+ + + @foreach (var fl in IngredientsList) + { + + + + + + } + +
+ @fl.Name + @if (fl.Alkoholisch) + { + + + + } + + Bearbeiten + + @* + Bearbeiten + + *@ +
+
+} + + + + Löschen bestätigen + + Willst du die Zutat wirklich löschen?
+ Achtung, die Zutat wird auch aus den Maschineneinstellungen entfernt, falls sie dort verwendet worden sein sollte. + +
+ + +
+
+ + + Löschen nicht möglich + + Die Zutat kann nicht gelöscht werden, da Sie noch in Cocktails verwendet wird:
+ @InfoText + +
+ +
+
+ +@code { + + public List? IngredientsList { get; set; } + + private DbDataContext? _DataContext; + + private Flasche? SelectedIngredient; + + private ModalComponent dlgDelete = null!; + private ModalComponent dlgInfo = null!; + + private string InfoText; + + protected override async Task OnInitializedAsync() + { + await FillIngredientsList(); + } + + public async Task FillIngredientsList() + { + _DataContext ??= await FlascheDataContextFactory.CreateDbContextAsync(); + if (_DataContext != null) + { + IngredientsList = await _DataContext.Flaschen.OrderBy(f => f.Name).ToListAsync(); + } + + } + + private async Task ConfirmDelete(Flasche fl) + { + _DataContext ??= await FlascheDataContextFactory.CreateDbContextAsync(); + if (_DataContext == null) throw new Exception("Error creating DataContext"); + + var CocktailsWithIngredient = _DataContext.Cocktails.Include(c => c.Cocktailflaschen).Where(cf => cf.Cocktailflaschen.Any(cff => cff.Flasche == fl)).OrderBy(c => c.Name).ToList(); + + if (CocktailsWithIngredient.Count > 0) + { + InfoText = String.Join(", ", CocktailsWithIngredient.Select(c => c.Name).ToArray()); + await dlgInfo.OpenModal(); + } + else + { + SelectedIngredient = fl; + await dlgDelete.OpenModal(); + } + } + + + public async Task DeleteIngredient() + { + _DataContext ??= await FlascheDataContextFactory.CreateDbContextAsync(); + if (_DataContext != null && SelectedIngredient != null) + { + // Zutat aus Maschine rausnehmen (alle Filler, die die Flasche enthalten, auf null setzen) + _DataContext.Fillers.Where(f => f.Flasche == SelectedIngredient)?.ForEachAsync(fil => fil.Flasche = null); + + _DataContext.Flaschen.Remove(SelectedIngredient); + await _DataContext.SaveChangesAsync(); + await FillIngredientsList(); + } + await CloseDialog(dlgDelete); + } + + private async Task CloseDialog(ModalComponent dlg) + { + SelectedIngredient = null; + await dlg.Close(); + } +}