Refactoring; GeneralSettings-Klasse hinzugefügt

This commit is contained in:
BuildTools 2024-02-05 17:58:53 +01:00
parent 531c833e76
commit 6cd0f31ef9
21 changed files with 227 additions and 71 deletions

View File

@ -16,6 +16,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Sharer" Version="1.0.0" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
</ItemGroup>

View File

@ -5,6 +5,6 @@
public int Id { get; set; }
public string Name { get; set; } = "";
public string? ImageURL { get; set; }
public List<CocktailFlasche> Cocktailflaschen { get; } = new();
public List<CocktailFlasche> Cocktailflaschen { get; set; } = new();
}
}

View File

@ -4,7 +4,6 @@
{
public int Id { get; set; }
public string Name { get; set; } = "";
public int? Pos { get; set; }
public Flasche()
{

Binary file not shown.

View File

@ -0,0 +1,13 @@
namespace CocktailWeb
{
public class GeneralSettings
{
public int MaxAllowedUploadSizeInMB { get; set; } = 10;
public string ImageUploadDir { get; set; } = "images";
public string SerialPort { get; set; } = "COM5";
public int Baudrate { get; set; } = 115200;
}
}

View File

@ -1,16 +1,46 @@
@page "/cocktails/{CocktailId}"
@using CocktailWeb.Data
@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DbDataContext> DataContextFactory;
@if(SelectedCocktail != null)
{
<h3>@SelectedCocktail.Name</h3>
} else
{
<h3>Cocktail konnte nicht gefunden werden</h3>
<SectionContent SectionId="TopRow.Title">
<a class="btn btn-primary" href="/cocktails">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left-circle" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a7 7 0 1 0 14 0A7 7 0 0 0 1 8m15 0A8 8 0 1 1 0 8a8 8 0 0 1 16 0m-4.5-.5a.5.5 0 0 1 0 1H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5z" />
</svg>
Zurück
</a>
</SectionContent>
@if (SelectedCocktail == null)
{
<h3>Cocktail konnte nicht gefunden werden</h3>
}
else
{
<div class="card mb-3" style="max-width: 540px;">
<div class="row g-0">
<div class="col-md-4">
<img src="@SelectedCocktail.ImageURL" class="img-fluid rounded-start" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">@SelectedCocktail.Name</h5>
<p class="card-text">
<h6>Zutaten:</h6>
<ul>
@foreach (var Zutat in SelectedCocktail.Cocktailflaschen)
{
<li>@Zutat.Flasche?.Name (@Zutat.Menge ml)</li>
}
</ul>
</p>
</div>
</div>
</div>
</div>
}
@ -28,7 +58,7 @@
_DataContext ??= await DataContextFactory.CreateDbContextAsync();
if (_DataContext != null)
{
SelectedCocktail = _DataContext.Cocktails.FirstOrDefault(c => c.Id == id);
SelectedCocktail = _DataContext.Cocktails.Include(c => c.Cocktailflaschen).ThenInclude(cf => cf.Flasche).Single(c => c.Id == id);
}
}
}

View File

@ -1,23 +1,14 @@
@page "/cocktails"
@page "/"
@using CocktailWeb.Data
@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DbDataContext> DataContextFactory;
@inject IWebHostEnvironment env;
<!--layout SideScrollLayout-->
<!--<h3>Cocktails</h3>-->
<PageTitle>Cocktails</PageTitle>
<SectionContent SectionId="TopRow.Title">
<label>Cocktails</label>
</SectionContent>
<!--
<p>
<a href="/cocktails/add">Cocktail hinzufügen</a>
</p>
-->
<!-- Flex Container für Horizontalen Scroll - klappt aber nicht so richtig->
<!--<div class="container py-2 border border-5 overflow-auto d-inline-block">
<div class="row flex-nowrap">-->
<div class="card-group gap-3 flex-nowrap">
@foreach (Cocktail c in CocktailListe)
@ -47,3 +38,5 @@
}
</div>

View File

@ -1,4 +1,4 @@
@page "/"
@page "/index"
<PageTitle>Index</PageTitle>
@ -6,6 +6,8 @@
Welcome to your new app.
<a href="/cocktails">
Cocktails</a>
@code {

View File

@ -1,10 +1,10 @@
@page "/serialtest"
@using System.IO.Ports
@using Microsoft.Extensions.Options
@inject IOptions<GeneralSettings> Config;
<PageTitle>Serial Test</PageTitle>
<label for="portname">Port:</label>
<InputText id="portname" @bind-Value="portname"></InputText>
<label for="inputdata">Data:</label>
<InputText id="inputdata" @bind-Value="inputdata"></InputText>
<button @onclick="SendData">Send</button>
@ -16,7 +16,6 @@
@code {
SerialPort? port;
string? portname = "COM5";
string? receivedText;
string? inputdata ="LED";
@ -24,15 +23,14 @@
bool Sending = false;
bool Receiving = false;
public async Task SendData()
{
if (Sending) return; // nichts senden, wenn bereits am Senden
Sending = true;
if (port == null && portname != null)
if (port == null && Config.Value.SerialPort != null)
{
port = new SerialPort(portname);
port.BaudRate = 115200; //9600
port = new SerialPort(Config.Value.SerialPort);
port.BaudRate = Config.Value.Baudrate; //9600
port.DataReceived += port_DataReceived;
//port.DataBits = 8;
//port.StopBits = StopBits.One;

View File

@ -1,10 +1,17 @@
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@using CocktailWeb.Data
@page "/cocktails/add"
@using Microsoft.Extensions.Options
@page "/settings/cocktails/edit"
@page "/settings/cocktails/edit/{id}"
@inject IDbContextFactory<DbDataContext> DataContextFactory;
@inject NavigationManager nav;
@inject IWebHostEnvironment env;
<h3>Cocktail hinzufügen</h3>
@inject IOptions<GeneralSettings> Config;
<SectionContent SectionId="TopRow.Title">
<label>Cocktail hinzufügen</label>
</SectionContent>
<div class="d-flex gap-3">
@ -69,7 +76,7 @@
</tbody>
</table>
<button @onclick="OnSubmit" class="btn btn-primary">Speichern</button>
<button @onclick="@(() => nav.NavigateTo("/cocktails"))" class="btn btn-secondary">Abbrechen</button>
<button @onclick="@(() => nav.NavigateTo("/settings/cocktails"))" class="btn btn-secondary">Abbrechen</button>
</div>
<div class="border border-2 rounded p-3 w-100">

View File

@ -1,28 +1,40 @@
using CocktailWeb;
using CocktailWeb.Data;
using CocktailWeb.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.EntityFrameworkCore;
namespace CocktailWeb.Pages
namespace CocktailWeb.Pages.Settings
{
public partial class CocktailHinzufuegen
{
[Parameter]
public string? id { get; set; }
public List<Flasche> FlaschenListe { get; set; } = new();
public Cocktail EditCocktail = new();
public Cocktail? EditCocktail;
private IBrowserFile? CocktaiLBildDatei;
private DbDataContext? _DataContext;
private int MaxAllowedUploadSizeInMB = 10;
private string ImageUploadDir = "cocktails";
protected override async Task OnInitializedAsync()
{
if (id != null)
{
_DataContext ??= await DataContextFactory.CreateDbContextAsync();
if (_DataContext != null)
{
EditCocktail = _DataContext.Cocktails.Include(c => c.Cocktailflaschen).ThenInclude(cf => cf.Flasche).Single(c => c.Id == Convert.ToInt32(id));
}
}
// Falls keine ID angegeben wurde oder der Cocktail in der Datenbank nicht gefunden wurde, gehen wir davon aus dass ein neuer Cocktail erstellt wird.
if (EditCocktail == null)
{
EditCocktail = new();
}
await FillFlaschenListe();
await InvokeAsync(StateHasChanged);
}
public async Task FillFlaschenListe()
@ -36,13 +48,13 @@ namespace CocktailWeb.Pages
public void FlascheHinzufuegen(MouseEventArgs e, Flasche f)
{
EditCocktail.Cocktailflaschen.Add(new CocktailFlasche() { Flasche = f, Reihenfolge = EditCocktail.Cocktailflaschen.Count + 1 });
EditCocktail?.Cocktailflaschen.Add(new CocktailFlasche() { Flasche = f, Reihenfolge = EditCocktail.Cocktailflaschen.Count + 1 });
CocktailFlaschenSortieren();
}
public void FlascheEntfernen(MouseEventArgs e, CocktailFlasche cf)
{
EditCocktail.Cocktailflaschen.Remove(cf);
EditCocktail?.Cocktailflaschen.Remove(cf);
CocktailFlaschenSortieren();
}
@ -85,12 +97,12 @@ namespace CocktailWeb.Pages
try
{
// Bild hochladen
var relativeuploadpath = Path.Join(ImageUploadDir, $"{EditCocktail.Id}{Path.GetExtension(CocktaiLBildDatei.Name)}");
var relativeuploadpath = Path.Join(Config.Value.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);
Stream stream = CocktaiLBildDatei.OpenReadStream(maxAllowedSize: Config.Value.MaxAllowedUploadSizeInMB * 1024 * 1024);
FileStream fs = File.Create(fullpath);
await stream.CopyToAsync(fs);
stream.Close();

View File

@ -0,0 +1,30 @@
@page "/settings/cocktails"
@using CocktailWeb.Data
@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DbDataContext> DataContextFactory;
@inject IWebHostEnvironment env;
<!--layout SideScrollLayout-->
<!--<h3>Cocktails</h3>-->
<SectionContent SectionId="TopRow.Title">
<label>Einstellungen - Cocktails</label>
</SectionContent>
<a class="btn btn-primary mb-3" href="/settings/cocktails/edit">Cocktail hinzufügen</a>
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-light border-dark">
<tbody>
@foreach (Cocktail c in CocktailListe)
{
<tr>
<td class="p-0"><img src="@c.ImageURL" style="max-height:40px;" /></td>
<td>@c.Name</td>
<td> <a class="btn btn-primary" href="/settings/cocktails/edit/@c.Id">Bearbeiten</a></td>
</tr>
}
</tbody>
</table>
</div>

View File

@ -0,0 +1,30 @@
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.Settings
{
public partial class Cocktails
{
private DbDataContext? _DataContext;
private List<Cocktail> CocktailListe { get; set; } = new();
protected override async Task OnInitializedAsync()
{
await ShowCocktails();
}
private async Task ShowCocktails()
{
_DataContext ??= await DataContextFactory.CreateDbContextAsync();
if (_DataContext != null)
{
CocktailListe = await _DataContext.Cocktails.OrderBy(f => f.Name).ToListAsync();
}
}
}
}

View File

@ -1,10 +1,16 @@
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.Sections
@using Microsoft.EntityFrameworkCore
@using CocktailWeb.Data
@page "/flaschen"
@page "/settings/flaschen"
@inject IDbContextFactory<DbDataContext> FlascheDataContextFactory;
<PageTitle>Flaschen</PageTitle>
<SectionContent SectionId="TopRow.Title">
<label>Einstellungen - Zutaten</label>
</SectionContent>
@if (CreateFormVisible && FlascheToCreate != null)
{
<h3>Flasche hinzufügen</h3>
@ -16,7 +22,7 @@
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary" @onclick="CreateNewFlasche">Submit</button>
<button name="submit" type="submit" class="btn btn-primary" @onclick="CreateNewFlasche">Hinzufügen</button>
</div>
</div>
}
@ -29,15 +35,13 @@ else
</div>
}
<h3>Flaschen</h3>
@if (FlaschenListe != null && FlaschenListe.Count > 0)
{
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered table-light border-dark">
<thead>
<tr>
<th scope="col">ID</th>
<!--<th scope="col">ID</th>-->
<th scope="col">Name</th>
<th scope="col">Action</th>
</tr>
@ -48,19 +52,19 @@ else
@if (EditFormVisible && FlascheToUpdate != null && FlascheToUpdate.Id == flasche.Id)
{
<tr>
<th scope="row">@FlascheToUpdate.Id</th>
<!-- <th scope="row">@FlascheToUpdate.Id</th>-->
<td> <input id="Name" name="Name" type="text" class="form-control" @bind="@FlascheToUpdate.Name" /></td>
<td><button name="submit" type="submit" class="btn btn-primary" @onclick="() => UpdateEmployee(FlascheToUpdate)">Save</button></td>
<td><button name="submit" type="submit" class="btn btn-primary" @onclick="() => UpdateEmployee(FlascheToUpdate)">Speichern</button></td>
</tr>
}
else
{
<tr>
<th scope="row">@flasche.Id</th>
<!-- <th scope="row">@flasche.Id</th> -->
<td>@flasche.Name</td>
<td>
<button name="submit" type="submit" class="btn btn-primary" @onclick="() => ShowEditForm(flasche)">Edit</button>
<button name="submit" type="submit" class="btn btn-primary" @onclick="() => DeleteFlasche(flasche)">Delete</button>
<button name="submit" type="submit" class="btn btn-primary" @onclick="() => ShowEditForm(flasche)">Bearbeiten</button>
<button name="submit" type="submit" class="btn btn-primary" @onclick="() => DeleteFlasche(flasche)">Löschen</button>
</td>
</tr>
}

View File

@ -2,7 +2,7 @@
using Microsoft.EntityFrameworkCore;
using CocktailWeb.Data;
namespace CocktailWeb.Pages
namespace CocktailWeb.Pages.Settings
{
public partial class Flaschen
{

View File

@ -0,0 +1,13 @@
@page "/settings/maschine";
@using Microsoft.EntityFrameworkCore
@using CocktailWeb.Data
@using Microsoft.AspNetCore.Components.Sections
@inject IDbContextFactory<DbDataContext> FlascheDataContextFactory;
<PageTitle>Maschine</PageTitle>
<SectionContent SectionId="TopRow.Title">
<label>Einstellungen - Maschine</label>
</SectionContent>

View File

@ -0,0 +1,6 @@
namespace CocktailWeb.Pages.Settings
{
partial class Maschine
{
}
}

View File

@ -1,3 +1,4 @@
using CocktailWeb;
using CocktailWeb.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
@ -11,6 +12,7 @@ var connectionString = builder.Configuration.GetConnectionString("CocktailDB");
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddDbContextFactory<DbDataContext>(options => options.UseSqlite(connectionString));
builder.Services.Configure<GeneralSettings>(builder.Configuration.GetSection(nameof(GeneralSettings)));
var app = builder.Build();

View File

@ -2,9 +2,17 @@
<div class="d-flex fixed-top justify-content-between px-4" style="background-color: #f7f7f7; border-bottom: 1px solid #d6d5d5; height: 56px; align-items: center; top: 0; z-index: 1;">
<div class="d-flex gap-3">
<a class="btn btn-primary" href="/">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-house" viewBox="0 0 16 16">
<path d="M8.707 1.5a1 1 0 0 0-1.414 0L.646 8.146a.5.5 0 0 0 .708.708L2 8.207V13.5A1.5 1.5 0 0 0 3.5 15h9a1.5 1.5 0 0 0 1.5-1.5V8.207l.646.647a.5.5 0 0 0 .708-.708L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293zM13 7.207V13.5a.5.5 0 0 1-.5.5h-9a.5.5 0 0 1-.5-.5V7.207l5-5z" />
</svg>
</a>
<h3>
<SectionOutlet SectionId="Title" />
</h3>
</div>
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-gear-fill" viewBox="0 0 16 16">
@ -13,8 +21,9 @@
Einstellungen
</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="/flaschen">Zutaten</a>
<a class="dropdown-item" href="/cocktails">Cocktails</a>
<a class="dropdown-item" href="/settings/maschine">Maschine</a>
<a class="dropdown-item" href="/settings/flaschen">Zutaten</a>
<a class="dropdown-item" href="/settings/cocktails">Cocktails</a>
<a class="dropdown-item" href="/serialtest">Serial Test</a>
</div>
</div>

View File

@ -8,3 +8,4 @@
@using Microsoft.JSInterop
@using CocktailWeb
@using CocktailWeb.Shared
@using CocktailWeb.Pages

View File

@ -8,5 +8,11 @@
"ConnectionStrings": {
"CocktailDB": "Data Source=Data\\cocktails.db"
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"GeneralSettings": {
"MaxAllowedUploadSizeInMB": 10,
"ImageUploadDir": "images",
"SerialPort": "COM5",
"Baudrate": 115200
}
}