2024-02-28 19:49:59 +01:00

141 lines
5.9 KiB
Plaintext

@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@using CocktailWeb.Data
@page "/settings/ingredients"
@inject IDbContextFactory<DbDataContext> FlascheDataContextFactory;
<PageTitle>Zutaten</PageTitle>
<SectionContent SectionId="TopRow.Title">
<label>Einstellungen - Zutaten</label>
</SectionContent>
<a class="btn btn-primary mb-3" href="/settings/ingredients/edit">Neue Zutat</a>
@if (IngredientsList != null && IngredientsList.Count > 0)
{
<div class="table-responsive mt-3">
<table class="table table-striped table-hover table-bordered table-dark border-dark">
<tbody>
@foreach (var fl in IngredientsList)
{
<tr>
<td class="p-0" style="width:64px"><img src="@fl.ImageURL" style="max-width:100%; max-height:auto;" /></td>
<td style="vertical-align:middle;">
@fl.Name
@if (fl.Alkoholisch)
{
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-stars text-success" style="margin-left:15px;" viewBox="0 0 16 16">
<path d="M7.657 6.247c.11-.33.576-.33.686 0l.645 1.937a2.89 2.89 0 0 0 1.829 1.828l1.936.645c.33.11.33.576 0 .686l-1.937.645a2.89 2.89 0 0 0-1.828 1.829l-.645 1.936a.361.361 0 0 1-.686 0l-.645-1.937a2.89 2.89 0 0 0-1.828-1.828l-1.937-.645a.361.361 0 0 1 0-.686l1.937-.645a2.89 2.89 0 0 0 1.828-1.828zM3.794 1.148a.217.217 0 0 1 .412 0l.387 1.162c.173.518.579.924 1.097 1.097l1.162.387a.217.217 0 0 1 0 .412l-1.162.387A1.73 1.73 0 0 0 4.593 5.69l-.387 1.162a.217.217 0 0 1-.412 0L3.407 5.69A1.73 1.73 0 0 0 2.31 4.593l-1.162-.387a.217.217 0 0 1 0-.412l1.162-.387A1.73 1.73 0 0 0 3.407 2.31zM10.863.099a.145.145 0 0 1 .274 0l.258.774c.115.346.386.617.732.732l.774.258a.145.145 0 0 1 0 .274l-.774.258a1.16 1.16 0 0 0-.732.732l-.258.774a.145.145 0 0 1-.274 0l-.258-.774a1.16 1.16 0 0 0-.732-.732L9.1 2.137a.145.145 0 0 1 0-.274l.774-.258c.346-.115.617-.386.732-.732z" />
</svg>
}
</td>
<td style="text-align:right; vertical-align:middle;">
<a class="btn btn-primary" href="/settings/ingredients/edit/@fl.Id">Bearbeiten</a>
<button name="submit" type="submit" class="btn btn-outline-danger" @onclick="() => ConfirmDelete(fl)">Löschen</button>
@*
<a class="btn btn-primary" href="/settings/cocktails/edit/@c.Id">Bearbeiten</a>
<button name="submit" type="submit" class="btn btn-outline-danger" @onclick="() => ConfirmDelete(c)">Löschen</button>
*@
</td>
</tr>
}
</tbody>
</table>
</div>
}
<ModalComponent @ref="dlgDelete">
<Title>Löschen bestätigen</Title>
<Body>
Willst du die Zutat wirklich löschen?<br />
Achtung, die Zutat wird auch aus den Maschineneinstellungen entfernt, falls sie dort verwendet worden sein sollte.
</Body>
<Footer>
<button class="btn btn-danger" @onclick="DeleteIngredient">Jo</button>
<button class="btn btn-primary" @onclick="() => CloseDialog(dlgDelete)">Nee</button>
</Footer>
</ModalComponent>
<ModalComponent @ref="dlgInfo">
<Title>Löschen nicht möglich</Title>
<Body>
Die Zutat kann nicht gelöscht werden, da Sie noch in Cocktails verwendet wird:<br />
@InfoText
</Body>
<Footer>
<button class="btn btn-primary" @onclick="() => CloseDialog(dlgInfo)">OK</button>
</Footer>
</ModalComponent>
@code {
public List<Flasche>? 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();
}
}