125 lines
4.7 KiB
Plaintext
125 lines
4.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">
|
|
<i class="bi bi-stars text-success"></i>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");
|
|
}
|
|
|
|
}
|