139 lines
4.8 KiB
Plaintext
139 lines
4.8 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)
|
|
{
|
|
<i class="bi bi-stars text-success"></i>
|
|
}
|
|
</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.ShowAsync();
|
|
}
|
|
else
|
|
{
|
|
SelectedIngredient = fl;
|
|
await dlgDelete.ShowAsync();
|
|
}
|
|
}
|
|
|
|
|
|
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.CloseAsync();
|
|
}
|
|
}
|