113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
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
|
|
{
|
|
public partial class CocktailHinzufuegen
|
|
{
|
|
public List<Flasche> FlaschenListe { get; set; } = new();
|
|
|
|
public Cocktail EditCocktail = new();
|
|
|
|
private IBrowserFile? CocktaiLBildDatei;
|
|
private DbDataContext? _DataContext;
|
|
|
|
private int MaxAllowedUploadSizeInMB = 10;
|
|
|
|
private string ImageUploadDir = "cocktails";
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await FillFlaschenListe();
|
|
}
|
|
|
|
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(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: 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");
|
|
}
|
|
}
|
|
}
|