Refactoring; GeneralSettings-Klasse hinzugefügt

This commit is contained in:
BuildTools committed 2024-02-05 17:58:53 +01:00
1 parent 531c833e76
commit 6cd0f31ef9
21 files changed
+227 -71

No files matched your search

@@ -0,0 +1,107 @@
@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@using CocktailWeb.Data
@using Microsoft.Extensions.Options
@page "/settings/cocktails/edit"
@page "/settings/cocktails/edit/{id}"
@inject IDbContextFactory<DbDataContext> DataContextFactory;
@inject NavigationManager nav;
@inject IWebHostEnvironment env;
@inject IOptions<GeneralSettings> Config;
<SectionContent SectionId="TopRow.Title">
<label>Cocktail hinzufügen</label>
</SectionContent>
<div class="d-flex gap-3">
<div class="border border-2 rounded p-3 w-100">
<h4>Mein Cocktail</h4>
<div class="mb-3">
<label class="form-label" for="cocktailname">Cocktailname</label>
<input id="cocktailname" name="cocktailname" type="text" class="form-control" @bind="@EditCocktail.Name" />
</div>
<div class="mb-3">
<label class="form-label" for="cocktailimage">Bild</label>
<InputFile OnChange="Bild_OnChange" class="form-control" id="cocktailimage" name="cocktailimage" accept=".jpg,.png,.jpeg,.gif,.webp" />
</div>
<h5>Cocktail-Zutaten</h5>
<table class="table align-middle">
<tbody>
@if (EditCocktail.Cocktailflaschen.Count == 0)
{
<tr>
<td class="disabled" aria-disabled="true">Noch keine Zutaten hinzugefügt</td>
</tr>
}
else
{
@foreach (CocktailFlasche cf in EditCocktail.Cocktailflaschen.OrderBy(cf => cf.Reihenfolge))
{
<tr>
<th>@cf.Reihenfolge</th>
<th>@cf.Flasche.Name</th>
<td>
<div class="input-group">
<input class="form-control" id="Menge" name="menge" type="text" @bind="@cf.Menge" />
<span class="input-group-text">ml</span>
</div>
</td>
<td>
<div class="btn-group">
<button class="btn btn-outline-secondary" @onclick="args => FlascheReihenfolge(args, cf,-2)">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-up-fill" viewBox="0 0 16 16">
<path d="m7.247 4.86-4.796 5.481c-.566.647-.106 1.659.753 1.659h9.592a1 1 0 0 0 .753-1.659l-4.796-5.48a1 1 0 0 0-1.506 0z" />
</svg>
</button>
<button class="btn btn-outline-secondary" @onclick="args => FlascheReihenfolge(args, cf ,2)">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down-fill" viewBox="0 0 16 16">
<path d="M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z" />
</svg>
</button>
</div>
</td>
<td>
<button class="btn btn-outline-danger" @onclick="args => FlascheEntfernen(args, cf)">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-dash-circle" viewBox="0 0 16 16">
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16" />
<path d="M4 8a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7A.5.5 0 0 1 4 8" />
</svg>
</button>
</td>
</tr>
}
}
</tbody>
</table>
<button @onclick="OnSubmit" class="btn btn-primary">Speichern</button>
<button @onclick="@(() => nav.NavigateTo("/settings/cocktails"))" class="btn btn-secondary">Abbrechen</button>
</div>
<div class="border border-2 rounded p-3 w-100">
<h4>Verfügbare Zutaten</h4>
<ul class="list-group">
@if (FlaschenListe.Count == 0)
{
<li class="list-group-item list-group-item-action align-middle">
Es wurden noch keine Zutaten angelegt
</li>
}
else
{
@foreach (Flasche fl in FlaschenListe)
{
<li class="list-group-item list-group-item-action align-middle" @onclick="args => FlascheHinzufuegen(args,fl)">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-circle text-success" viewBox="0 0 16 16">
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16" />
<path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4" />
</svg>
<label class="form-check-label stretched-link">@fl.Name</label>
</li>
}
}
</ul>
</div>
</div>
@@ -0,0 +1,124 @@
using CocktailWeb.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.EntityFrameworkCore;
namespace CocktailWeb.Pages.Settings
{
public partial class CocktailHinzufuegen
{
[Parameter]
public string? id { get; set; }
public List<Flasche> FlaschenListe { get; set; } = new();
public Cocktail? EditCocktail;
private IBrowserFile? CocktaiLBildDatei;
private DbDataContext? _DataContext;
protected override async Task OnInitializedAsync()
{
if (id != null)
{
_DataContext ??= await DataContextFactory.CreateDbContextAsync();
if (_DataContext != null)
{
EditCocktail = _DataContext.Cocktails.Include(c => c.Cocktailflaschen).ThenInclude(cf => cf.Flasche).Single(c => c.Id == Convert.ToInt32(id));
}
}
// Falls keine ID angegeben wurde oder der Cocktail in der Datenbank nicht gefunden wurde, gehen wir davon aus dass ein neuer Cocktail erstellt wird.
if (EditCocktail == null)
{
EditCocktail = new();
}
await FillFlaschenListe();
await InvokeAsync(StateHasChanged);
}
public async Task FillFlaschenListe()
{
_DataContext ??= await DataContextFactory.CreateDbContextAsync();
if (_DataContext != null)
{
FlaschenListe = await _DataContext.Flaschen.OrderBy(f => f.Name).ToListAsync();
}
}
public void FlascheHinzufuegen(MouseEventArgs e, Flasche f)
{
EditCocktail?.Cocktailflaschen.Add(new CocktailFlasche() { Flasche = f, Reihenfolge = EditCocktail.Cocktailflaschen.Count + 1 });
CocktailFlaschenSortieren();
}
public void FlascheEntfernen(MouseEventArgs e, CocktailFlasche cf)
{
EditCocktail?.Cocktailflaschen.Remove(cf);
CocktailFlaschenSortieren();
}
/// <summary>
/// Geht die Liste durch und setzt die Reihenfolge neu
/// </summary>
void CocktailFlaschenSortieren()
{
// Reihenfolge neu sortieren, damit keine Lücken entstehen
int i = 1;
foreach (var c in EditCocktail.Cocktailflaschen.OrderBy(cf => cf.Reihenfolge))
{
c.Reihenfolge = i;
i++;
}
}
public void FlascheReihenfolge(MouseEventArgs e, CocktailFlasche cf, int Richtung)
{
cf.Reihenfolge += Richtung;
CocktailFlaschenSortieren();
}
public void Bild_OnChange(InputFileChangeEventArgs e)
{
CocktaiLBildDatei = e.GetMultipleFiles().FirstOrDefault();
//this.StateHasChanged(); War in nem Stackexchange.. Nötig?
}
private async Task OnSubmit(EventArgs e)
{
_DataContext ??= await DataContextFactory.CreateDbContextAsync();
if (EditCocktail != null && _DataContext != null)
{
_DataContext.Cocktails.Add(EditCocktail);
await _DataContext.SaveChangesAsync();
if (CocktaiLBildDatei != null)
{
try
{
// Bild hochladen
var relativeuploadpath = Path.Join(Config.Value.ImageUploadDir, $"{EditCocktail.Id}{Path.GetExtension(CocktaiLBildDatei.Name)}");
var fullpath = Path.Join(env.WebRootPath, relativeuploadpath);
string? folder = Path.GetDirectoryName(fullpath);
if (folder != null && !Path.Exists(folder)) Directory.CreateDirectory(folder);
Stream stream = CocktaiLBildDatei.OpenReadStream(maxAllowedSize: Config.Value.MaxAllowedUploadSizeInMB * 1024 * 1024);
FileStream fs = File.Create(fullpath);
await stream.CopyToAsync(fs);
stream.Close();
fs.Close();
EditCocktail.ImageURL = relativeuploadpath;
// URL hinterlegen und wieder speichern
_DataContext.Cocktails.Update(EditCocktail);
await _DataContext.SaveChangesAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Error uploading image: {ex.Message}");
}
}
}
nav.NavigateTo("/cocktails");
}
}
}
@@ -0,0 +1,30 @@
@page "/settings/cocktails"
@using CocktailWeb.Data
@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DbDataContext> DataContextFactory;
@inject IWebHostEnvironment env;
<!--layout SideScrollLayout-->
<!--<h3>Cocktails</h3>-->
<SectionContent SectionId="TopRow.Title">
<label>Einstellungen - Cocktails</label>
</SectionContent>
<a class="btn btn-primary mb-3" href="/settings/cocktails/edit">Cocktail hinzufügen</a>
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-light border-dark">
<tbody>
@foreach (Cocktail c in CocktailListe)
{
<tr>
<td class="p-0"><img src="@c.ImageURL" style="max-height:40px;" /></td>
<td>@c.Name</td>
<td> <a class="btn btn-primary" href="/settings/cocktails/edit/@c.Id">Bearbeiten</a></td>
</tr>
}
</tbody>
</table>
</div>
@@ -0,0 +1,30 @@
using CocktailWeb;
using CocktailWeb.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.EntityFrameworkCore;
namespace CocktailWeb.Pages.Settings
{
public partial class Cocktails
{
private DbDataContext? _DataContext;
private List<Cocktail> CocktailListe { get; set; } = new();
protected override async Task OnInitializedAsync()
{
await ShowCocktails();
}
private async Task ShowCocktails()
{
_DataContext ??= await DataContextFactory.CreateDbContextAsync();
if (_DataContext != null)
{
CocktailListe = await _DataContext.Cocktails.OrderBy(f => f.Name).ToListAsync();
}
}
}
}
+79
View File
@@ -0,0 +1,79 @@
@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@using CocktailWeb.Data
@page "/settings/flaschen"
@inject IDbContextFactory<DbDataContext> FlascheDataContextFactory;
<PageTitle>Flaschen</PageTitle>
<SectionContent SectionId="TopRow.Title">
<label>Einstellungen - Zutaten</label>
</SectionContent>
@if (CreateFormVisible && FlascheToCreate != null)
{
<h3>Flasche hinzufügen</h3>
<div class="row">
<label for="Name" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="Name" name="Name" type="text" class="form-control" @bind="@FlascheToCreate.Name" />
</div>
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary" @onclick="CreateNewFlasche">Hinzufügen</button>
</div>
</div>
}
else
{
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary" @onclick="ShowCreateForm">Flasche hinzufügen</button>
</div>
</div>
}
@if (FlaschenListe != null && FlaschenListe.Count > 0)
{
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-light border-dark">
<thead>
<tr>
<!--<th scope="col">ID</th>-->
<th scope="col">Name</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach (var flasche in FlaschenListe)
{
@if (EditFormVisible && FlascheToUpdate != null && FlascheToUpdate.Id == flasche.Id)
{
<tr>
<!-- <th scope="row">@FlascheToUpdate.Id</th>-->
<td> <input id="Name" name="Name" type="text" class="form-control" @bind="@FlascheToUpdate.Name" /></td>
<td><button name="submit" type="submit" class="btn btn-primary" @onclick="() => UpdateEmployee(FlascheToUpdate)">Speichern</button></td>
</tr>
}
else
{
<tr>
<!-- <th scope="row">@flasche.Id</th> -->
<td>@flasche.Name</td>
<td>
<button name="submit" type="submit" class="btn btn-primary" @onclick="() => ShowEditForm(flasche)">Bearbeiten</button>
<button name="submit" type="submit" class="btn btn-primary" @onclick="() => DeleteFlasche(flasche)">Löschen</button>
</td>
</tr>
}
}
</tbody>
</table>
</div>
}
@code {
}
@@ -0,0 +1,84 @@
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<Flasche>? 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();
}
}
}
+13
View File
@@ -0,0 +1,13 @@
@page "/settings/maschine";
@using Microsoft.EntityFrameworkCore
@using CocktailWeb.Data
@using Microsoft.AspNetCore.Components.Sections
@inject IDbContextFactory<DbDataContext> FlascheDataContextFactory;
<PageTitle>Maschine</PageTitle>
<SectionContent SectionId="TopRow.Title">
<label>Einstellungen - Maschine</label>
</SectionContent>
@@ -0,0 +1,6 @@
namespace CocktailWeb.Pages.Settings
{
partial class Maschine
{
}
}