zaklad administrace
This commit is contained in:
@@ -1,5 +1,140 @@
|
|||||||
<h3>CategoriEdit</h3>
|
@page "/admin/kategorie/edit"
|
||||||
|
@page "/admin/kategorie/edit/{Id:int}"
|
||||||
|
@rendermode InteractiveServer
|
||||||
|
@using System.ComponentModel.DataAnnotations
|
||||||
|
@using System.Globalization
|
||||||
|
@using System.Text
|
||||||
|
@using System.Text.RegularExpressions
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
|
@using Web_Jarmark.Data
|
||||||
|
@using Web_Jarmark.Data.Models
|
||||||
|
@inject ApplicationDbContext Db
|
||||||
|
@inject NavigationManager Nav
|
||||||
|
|
||||||
|
<PageTitle>@(Id is null ? "Nová kategorie" : "Upravit kategorii")</PageTitle>
|
||||||
|
|
||||||
|
<h3>@(Id is null ? "Nová kategorie" : "Upravit kategorii")</h3>
|
||||||
|
|
||||||
|
@if (notFound)
|
||||||
|
{
|
||||||
|
<div class="alert alert-warning">Kategorie nebyla nalezena.</div>
|
||||||
|
<a class="btn btn-secondary" href="/admin/kategorie">Zpět na seznam</a>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<EditForm Model="form" OnValidSubmit="Save" FormName="categoryForm" style="max-width: 500px;">
|
||||||
|
<DataAnnotationsValidator />
|
||||||
|
<ValidationSummary class="text-danger" />
|
||||||
|
|
||||||
|
@if (!string.IsNullOrEmpty(error))
|
||||||
|
{
|
||||||
|
<div class="alert alert-danger">@error</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label class="form-label" for="name">Název</label>
|
||||||
|
<InputText id="name" class="form-control" @bind-Value="form.Name" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label class="form-label" for="slug">Slug (adresa)</label>
|
||||||
|
<InputText id="slug" class="form-control" @bind-Value="form.Slug" />
|
||||||
|
<div class="form-text">Když necháš prázdné, vytvoří se automaticky z názvu.</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-check mb-3">
|
||||||
|
<InputCheckbox id="active" class="form-check-input" @bind-Value="form.IsActive" />
|
||||||
|
<label class="form-check-label" for="active">Aktivní</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-primary">Uložit</button>
|
||||||
|
<a class="btn btn-secondary" href="/admin/kategorie">Zrušit</a>
|
||||||
|
</EditForm>
|
||||||
|
}
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
|
[Parameter] public int? Id { get; set; }
|
||||||
|
|
||||||
|
private CategoryForm form = new();
|
||||||
|
private string? error;
|
||||||
|
private bool notFound;
|
||||||
|
|
||||||
|
protected override async Task OnParametersSetAsync()
|
||||||
|
{
|
||||||
|
if (Id is null) return;
|
||||||
|
|
||||||
|
var cat = await Db.Set<Category>().AsNoTracking().FirstOrDefaultAsync(c => c.Id == Id);
|
||||||
|
if (cat is null)
|
||||||
|
{
|
||||||
|
notFound = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
form = new CategoryForm { Name = cat.Name, Slug = cat.Slug, IsActive = cat.IsActive };
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task Save()
|
||||||
|
{
|
||||||
|
error = null;
|
||||||
|
|
||||||
|
var name = form.Name.Trim();
|
||||||
|
var slug = Slugify(string.IsNullOrWhiteSpace(form.Slug) ? name : form.Slug);
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(slug))
|
||||||
|
{
|
||||||
|
error = "Ze zadaného názvu nelze vytvořit slug.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var slugTaken = await Db.Set<Category>().AnyAsync(c => c.Slug == slug && c.Id != (Id ?? 0));
|
||||||
|
if (slugTaken)
|
||||||
|
{
|
||||||
|
error = $"Slug „{slug}“ už používá jiná kategorie.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Id is null)
|
||||||
|
{
|
||||||
|
Db.Set<Category>().Add(new Category { Name = name, Slug = slug, IsActive = form.IsActive });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var cat = await Db.Set<Category>().FindAsync(Id);
|
||||||
|
if (cat is null)
|
||||||
|
{
|
||||||
|
notFound = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cat.Name = name;
|
||||||
|
cat.Slug = slug;
|
||||||
|
cat.IsActive = form.IsActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
await Db.SaveChangesAsync();
|
||||||
|
Nav.NavigateTo("/admin/kategorie");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string Slugify(string text)
|
||||||
|
{
|
||||||
|
var normalized = text.Trim().ToLowerInvariant().Normalize(NormalizationForm.FormD);
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
foreach (var ch in normalized)
|
||||||
|
{
|
||||||
|
if (CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark)
|
||||||
|
sb.Append(ch);
|
||||||
|
}
|
||||||
|
var noDiacritics = sb.ToString().Normalize(NormalizationForm.FormC);
|
||||||
|
return Regex.Replace(noDiacritics, "[^a-z0-9]+", "-").Trim('-');
|
||||||
|
}
|
||||||
|
|
||||||
|
private class CategoryForm
|
||||||
|
{
|
||||||
|
[Required(ErrorMessage = "Zadej název kategorie.")]
|
||||||
|
[StringLength(100, ErrorMessage = "Název může mít nejvýše 100 znaků.")]
|
||||||
|
public string Name { get; set; } = "";
|
||||||
|
|
||||||
|
public string? Slug { get; set; }
|
||||||
|
|
||||||
|
public bool IsActive { get; set; } = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,112 @@
|
|||||||
<h3>CategoriList</h3>
|
@page "/admin/kategorie"
|
||||||
|
@rendermode InteractiveServer
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
|
@using Web_Jarmark.Data
|
||||||
|
@using Web_Jarmark.Data.Models
|
||||||
|
@inject ApplicationDbContext Db
|
||||||
|
@inject NavigationManager Nav
|
||||||
|
|
||||||
|
<PageTitle>Kategorie</PageTitle>
|
||||||
|
|
||||||
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||||
|
<h3 class="mb-0">Kategorie</h3>
|
||||||
|
<a class="btn btn-primary" href="/admin/kategorie/edit">+ Nová kategorie</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if (!string.IsNullOrEmpty(error))
|
||||||
|
{
|
||||||
|
<div class="alert alert-danger">@error</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
@if (categories is null)
|
||||||
|
{
|
||||||
|
<p><em>Načítám…</em></p>
|
||||||
|
}
|
||||||
|
else if (categories.Count == 0)
|
||||||
|
{
|
||||||
|
<p class="text-muted">Zatím tu nejsou žádné kategorie.</p>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<table class="table table-striped align-middle">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Název</th>
|
||||||
|
<th>Slug</th>
|
||||||
|
<th class="text-center">Počet produktů</th>
|
||||||
|
<th class="text-center">Aktivní</th>
|
||||||
|
<th class="text-end">Akce</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach (var c in categories)
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td>@c.Name</td>
|
||||||
|
<td><code>@c.Slug</code></td>
|
||||||
|
<td class="text-center">@c.ProductCount</td>
|
||||||
|
<td class="text-center">
|
||||||
|
<input type="checkbox" class="form-check-input"
|
||||||
|
checked="@c.IsActive"
|
||||||
|
@onchange="() => ToggleActive(c.Id)" />
|
||||||
|
</td>
|
||||||
|
<td class="text-end">
|
||||||
|
<a class="btn btn-sm btn-outline-secondary" href="/admin/kategorie/edit/@c.Id">Upravit</a>
|
||||||
|
<button class="btn btn-sm btn-outline-danger"
|
||||||
|
disabled="@(c.ProductCount > 0)"
|
||||||
|
title="@(c.ProductCount > 0 ? "Kategorie obsahuje produkty" : "Smazat")"
|
||||||
|
@onclick="() => Delete(c.Id)">
|
||||||
|
Smazat
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
}
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
|
private List<CategoryRow>? categories;
|
||||||
|
private string? error;
|
||||||
|
|
||||||
|
protected override async Task OnInitializedAsync() => await Load();
|
||||||
|
|
||||||
|
private async Task Load()
|
||||||
|
{
|
||||||
|
categories = await Db.Set<Category>()
|
||||||
|
.AsNoTracking()
|
||||||
|
.OrderBy(c => c.Name)
|
||||||
|
.Select(c => new CategoryRow(c.Id, c.Name, c.Slug, c.IsActive, c.Products.Count))
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ToggleActive(int id)
|
||||||
|
{
|
||||||
|
var cat = await Db.Set<Category>().FindAsync(id);
|
||||||
|
if (cat is null) return;
|
||||||
|
cat.IsActive = !cat.IsActive;
|
||||||
|
await Db.SaveChangesAsync();
|
||||||
|
await Load();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task Delete(int id)
|
||||||
|
{
|
||||||
|
error = null;
|
||||||
|
var cat = await Db.Set<Category>()
|
||||||
|
.Include(c => c.Products)
|
||||||
|
.FirstOrDefaultAsync(c => c.Id == id);
|
||||||
|
if (cat is null) return;
|
||||||
|
|
||||||
|
if (cat.Products.Count > 0)
|
||||||
|
{
|
||||||
|
error = "Kategorii nelze smazat, obsahuje produkty.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Db.Set<Category>().Remove(cat);
|
||||||
|
await Db.SaveChangesAsync();
|
||||||
|
await Load();
|
||||||
|
}
|
||||||
|
|
||||||
|
private record CategoryRow(int Id, string Name, string Slug, bool IsActive, int ProductCount);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,186 @@
|
|||||||
<h3>ProductEdit</h3>
|
@page "/admin/zbozi/edit"
|
||||||
|
@page "/admin/zbozi/edit/{Id:int}"
|
||||||
|
@rendermode InteractiveServer
|
||||||
|
@using System.ComponentModel.DataAnnotations
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
|
@using Web_Jarmark.Data
|
||||||
|
@using Web_Jarmark.Data.Models
|
||||||
|
@inject ApplicationDbContext Db
|
||||||
|
@inject NavigationManager Nav
|
||||||
|
|
||||||
|
<PageTitle>@(Id is null ? "Nové zboží" : "Upravit zboží")</PageTitle>
|
||||||
|
|
||||||
|
<h3>@(Id is null ? "Nové zboží" : "Upravit zboží")</h3>
|
||||||
|
|
||||||
|
@if (notFound)
|
||||||
|
{
|
||||||
|
<div class="alert alert-warning">Zboží nebylo nalezeno.</div>
|
||||||
|
<a class="btn btn-secondary" href="/admin/zbozi">Zpět na seznam</a>
|
||||||
|
}
|
||||||
|
else if (categories is null)
|
||||||
|
{
|
||||||
|
<p><em>Načítám…</em></p>
|
||||||
|
}
|
||||||
|
else if (categories.Count == 0)
|
||||||
|
{
|
||||||
|
<div class="alert alert-warning">
|
||||||
|
Nejdřív musíš vytvořit aspoň jednu kategorii.
|
||||||
|
<a href="/admin/kategorie/edit">Přidat kategorii</a>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<EditForm Model="form" OnValidSubmit="Save" FormName="productForm" style="max-width: 500px;">
|
||||||
|
<DataAnnotationsValidator />
|
||||||
|
<ValidationSummary class="text-danger" />
|
||||||
|
|
||||||
|
@if (!string.IsNullOrEmpty(error))
|
||||||
|
{
|
||||||
|
<div class="alert alert-danger">@error</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label class="form-label" for="name">Název</label>
|
||||||
|
<InputText id="name" class="form-control" @bind-Value="form.Name" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label class="form-label" for="cat">Kategorie</label>
|
||||||
|
<InputSelect id="cat" class="form-select" @bind-Value="form.CategoryId">
|
||||||
|
<option value="0">– vyber kategorii –</option>
|
||||||
|
@foreach (var c in categories)
|
||||||
|
{
|
||||||
|
<option value="@c.Id">@c.Name@(c.IsActive ? "" : " (neaktivní)")</option>
|
||||||
|
}
|
||||||
|
</InputSelect>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label class="form-label" for="desc">Popis</label>
|
||||||
|
<InputTextArea id="desc" class="form-control" rows="3" @bind-Value="form.Description" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="col mb-3">
|
||||||
|
<label class="form-label" for="price">Cena (Kč)</label>
|
||||||
|
<InputNumber id="price" class="form-control" @bind-Value="form.Price" />
|
||||||
|
</div>
|
||||||
|
<div class="col mb-3">
|
||||||
|
<label class="form-label" for="stock">Skladem (ks)</label>
|
||||||
|
<InputNumber id="stock" class="form-control" @bind-Value="form.StockQuantity" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label class="form-label" for="img">Adresa obrázku</label>
|
||||||
|
<InputText id="img" class="form-control" @bind-Value="form.ImageUrl" />
|
||||||
|
<div class="form-text">Např. /images/cukrovi.jpg (soubor v wwwroot) nebo celá adresa.</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-check mb-3">
|
||||||
|
<InputCheckbox id="visible" class="form-check-input" @bind-Value="form.IsVisible" />
|
||||||
|
<label class="form-check-label" for="visible">Viditelné v katalogu</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-primary">Uložit</button>
|
||||||
|
<a class="btn btn-secondary" href="/admin/zbozi">Zrušit</a>
|
||||||
|
</EditForm>
|
||||||
|
}
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
|
[Parameter] public int? Id { get; set; }
|
||||||
|
|
||||||
|
private ProductForm form = new();
|
||||||
|
private List<Category>? categories;
|
||||||
|
private string? error;
|
||||||
|
private bool notFound;
|
||||||
|
|
||||||
|
protected override async Task OnParametersSetAsync()
|
||||||
|
{
|
||||||
|
categories = await Db.Set<Category>().AsNoTracking().OrderBy(c => c.Name).ToListAsync();
|
||||||
|
|
||||||
|
if (Id is null) return;
|
||||||
|
|
||||||
|
var p = await Db.Set<Product>().AsNoTracking().FirstOrDefaultAsync(x => x.Id == Id);
|
||||||
|
if (p is null)
|
||||||
|
{
|
||||||
|
notFound = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
form = new ProductForm
|
||||||
|
{
|
||||||
|
Name = p.Name,
|
||||||
|
CategoryId = p.CategoryId,
|
||||||
|
Description = p.Description,
|
||||||
|
Price = p.Price,
|
||||||
|
StockQuantity = p.StockQuantity,
|
||||||
|
ImageUrl = p.ImageUrl,
|
||||||
|
IsVisible = p.IsVisible
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task Save()
|
||||||
|
{
|
||||||
|
error = null;
|
||||||
|
|
||||||
|
if (form.CategoryId == 0)
|
||||||
|
{
|
||||||
|
error = "Vyber kategorii.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Id is null)
|
||||||
|
{
|
||||||
|
var p = new Product();
|
||||||
|
Apply(p);
|
||||||
|
Db.Set<Product>().Add(p);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var p = await Db.Set<Product>().FindAsync(Id);
|
||||||
|
if (p is null)
|
||||||
|
{
|
||||||
|
notFound = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Apply(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Db.SaveChangesAsync();
|
||||||
|
Nav.NavigateTo("/admin/zbozi");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Apply(Product p)
|
||||||
|
{
|
||||||
|
p.Name = form.Name.Trim();
|
||||||
|
p.CategoryId = form.CategoryId;
|
||||||
|
p.Description = form.Description?.Trim() ?? "";
|
||||||
|
p.Price = form.Price;
|
||||||
|
p.StockQuantity = form.StockQuantity;
|
||||||
|
p.ImageUrl = form.ImageUrl?.Trim() ?? "";
|
||||||
|
p.IsVisible = form.IsVisible;
|
||||||
|
p.UpdatedAt = DateTime.Now;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ProductForm
|
||||||
|
{
|
||||||
|
[Required(ErrorMessage = "Zadej název zboží.")]
|
||||||
|
[StringLength(100, ErrorMessage = "Název může mít nejvýše 100 znaků.")]
|
||||||
|
public string Name { get; set; } = "";
|
||||||
|
|
||||||
|
public int CategoryId { get; set; }
|
||||||
|
|
||||||
|
public string? Description { get; set; }
|
||||||
|
|
||||||
|
[Range(0, 100000, ErrorMessage = "Cena musí být 0 až 100 000 Kč.")]
|
||||||
|
public decimal Price { get; set; }
|
||||||
|
|
||||||
|
[Range(0, 100000, ErrorMessage = "Počet kusů musí být 0 nebo víc.")]
|
||||||
|
public int StockQuantity { get; set; }
|
||||||
|
|
||||||
|
public string? ImageUrl { get; set; }
|
||||||
|
|
||||||
|
public bool IsVisible { get; set; } = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,109 @@
|
|||||||
<h3>ProductList</h3>
|
@page "/admin/zbozi"
|
||||||
|
@rendermode InteractiveServer
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
|
@using Web_Jarmark.Data
|
||||||
|
@using Web_Jarmark.Data.Models
|
||||||
|
@inject ApplicationDbContext Db
|
||||||
|
|
||||||
|
<PageTitle>Zboží</PageTitle>
|
||||||
|
|
||||||
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||||
|
<h3 class="mb-0">Zboží</h3>
|
||||||
|
<a class="btn btn-primary" href="/admin/zbozi/edit">+ Nové zboží</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if (!string.IsNullOrEmpty(error))
|
||||||
|
{
|
||||||
|
<div class="alert alert-danger">@error</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
@if (products is null)
|
||||||
|
{
|
||||||
|
<p><em>Načítám…</em></p>
|
||||||
|
}
|
||||||
|
else if (products.Count == 0)
|
||||||
|
{
|
||||||
|
<p class="text-muted">Zatím tu není žádné zboží.</p>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<table class="table table-striped align-middle">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Název</th>
|
||||||
|
<th>Kategorie</th>
|
||||||
|
<th class="text-end">Cena</th>
|
||||||
|
<th class="text-center">Skladem</th>
|
||||||
|
<th class="text-center">Viditelné</th>
|
||||||
|
<th class="text-end">Akce</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach (var p in products)
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td>@p.Name</td>
|
||||||
|
<td>@p.CategoryName</td>
|
||||||
|
<td class="text-end">@p.Price.ToString("N2") Kč</td>
|
||||||
|
<td class="text-center">@p.StockQuantity</td>
|
||||||
|
<td class="text-center">
|
||||||
|
<input type="checkbox" class="form-check-input"
|
||||||
|
checked="@p.IsVisible"
|
||||||
|
@onchange="() => ToggleVisible(p.Id)" />
|
||||||
|
</td>
|
||||||
|
<td class="text-end">
|
||||||
|
<a class="btn btn-sm btn-outline-secondary" href="/admin/zbozi/edit/@p.Id">Upravit</a>
|
||||||
|
<button class="btn btn-sm btn-outline-danger" @onclick="() => Delete(p.Id)">Smazat</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
}
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
|
private List<ProductRow>? products;
|
||||||
|
private string? error;
|
||||||
|
|
||||||
|
protected override async Task OnInitializedAsync() => await Load();
|
||||||
|
|
||||||
|
private async Task Load()
|
||||||
|
{
|
||||||
|
products = await Db.Set<Product>()
|
||||||
|
.AsNoTracking()
|
||||||
|
.OrderBy(p => p.Name)
|
||||||
|
.Select(p => new ProductRow(p.Id, p.Name, p.Category.Name, p.Price, p.StockQuantity, p.IsVisible))
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ToggleVisible(int id)
|
||||||
|
{
|
||||||
|
var p = await Db.Set<Product>().FindAsync(id);
|
||||||
|
if (p is null) return;
|
||||||
|
p.IsVisible = !p.IsVisible;
|
||||||
|
p.UpdatedAt = DateTime.Now;
|
||||||
|
await Db.SaveChangesAsync();
|
||||||
|
await Load();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task Delete(int id)
|
||||||
|
{
|
||||||
|
error = null;
|
||||||
|
var p = await Db.Set<Product>()
|
||||||
|
.Include(x => x.OrderItems)
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == id);
|
||||||
|
if (p is null) return;
|
||||||
|
|
||||||
|
if (p.OrderItems.Count > 0)
|
||||||
|
{
|
||||||
|
error = $"Zboží „{p.Name}“ už je v objednávkách, nelze ho smazat. Odškrtni raději „Viditelné“.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Db.Set<Product>().Remove(p);
|
||||||
|
await Db.SaveChangesAsync();
|
||||||
|
await Load();
|
||||||
|
}
|
||||||
|
|
||||||
|
private record ProductRow(int Id, string Name, string CategoryName, decimal Price, int StockQuantity, bool IsVisible);
|
||||||
}
|
}
|
||||||
BIN
Web-Jarmark/Web-Jarmark/jarmark.db-shm
Normal file
BIN
Web-Jarmark/Web-Jarmark/jarmark.db-shm
Normal file
Binary file not shown.
BIN
Web-Jarmark/Web-Jarmark/jarmark.db-wal
Normal file
BIN
Web-Jarmark/Web-Jarmark/jarmark.db-wal
Normal file
Binary file not shown.
Reference in New Issue
Block a user