128 lines
5.7 KiB
Plaintext
128 lines
5.7 KiB
Plaintext
@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<DbDataContext> DataContextFactory;
|
|
@inject NavigationManager nav;
|
|
@inject IWebHostEnvironment env;
|
|
@inject IOptions<GeneralSettings> Config;
|
|
|
|
<SectionContent SectionId="TopRow.Title">
|
|
<label>Zutat bearbeiten</label>
|
|
</SectionContent>
|
|
|
|
<div class="d-flex gap-3">
|
|
<div class="border border-2 rounded p-3 w-100">
|
|
<div class="mb-3">
|
|
<label class="form-label" for="name">Name</label>
|
|
<input id="cocktailname" name="name" type="text" class="form-control" @bind="@curFlasche.Name" />
|
|
</div>
|
|
<div class="form-check mb-3">
|
|
<InputCheckbox class="form-check-input" id="IsAlcoholic" @bind-Value=@curFlasche.Alkoholisch>Alkoholisch</InputCheckbox>
|
|
<label class="form-check-label" for="IsAlcoholic">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-stars text-success" 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>
|
|
Alkoholisch
|
|
</label>
|
|
</div>
|
|
<button @onclick="OnSubmit" class="btn btn-primary">Speichern</button>
|
|
<button @onclick="@(() => nav.NavigateTo("/settings/ingredients"))" class="btn btn-secondary">Abbrechen</button>
|
|
</div>
|
|
<div class="border border-2 rounded p-3 w-100">
|
|
<div class="mb-3">
|
|
<label class="form-label" for="image">Bild</label>
|
|
<div class="mb-2">
|
|
@if (curFlasche.ImageURL != null)
|
|
{
|
|
<img style="max-width:128px;" src="@curFlasche.ImageURL" />
|
|
}
|
|
else
|
|
{
|
|
<div>Kein Bild vorhanden</div>
|
|
}
|
|
</div>
|
|
<label class="form-label" for="image">Neues Bild hochladen</label>
|
|
<InputFile OnChange="Bild_OnChange" class="form-control" id="image" name="image" accept=".jpg,.png,.jpeg,.gif,.webp" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
@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");
|
|
}
|
|
|
|
}
|