From 03b026f1f30cc8bda507716e1d5370057d70b629 Mon Sep 17 00:00:00 2001 From: Ondur Date: Wed, 28 Oct 2026 18:06:46 +0100 Subject: [PATCH] zaklad administrace --- .../Components/Pages/Admin/CategoriEdit.razor | 139 ++++++++++++- .../Components/Pages/Admin/CategoriList.razor | 111 ++++++++++- .../Components/Pages/Admin/ProductEdit.razor | 185 +++++++++++++++++- .../Components/Pages/Admin/ProductList.razor | 108 +++++++++- Web-Jarmark/Web-Jarmark/jarmark.db-shm | Bin 0 -> 32768 bytes Web-Jarmark/Web-Jarmark/jarmark.db-wal | Bin 0 -> 28872 bytes 6 files changed, 535 insertions(+), 8 deletions(-) create mode 100644 Web-Jarmark/Web-Jarmark/jarmark.db-shm create mode 100644 Web-Jarmark/Web-Jarmark/jarmark.db-wal diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriEdit.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriEdit.razor index d126f53..b995ff1 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriEdit.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriEdit.razor @@ -1,5 +1,140 @@ -

CategoriEdit

+@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 + +@(Id is null ? "Nová kategorie" : "Upravit kategorii") + +

@(Id is null ? "Nová kategorie" : "Upravit kategorii")

+ +@if (notFound) +{ +
Kategorie nebyla nalezena.
+ Zpět na seznam +} +else +{ + + + + + @if (!string.IsNullOrEmpty(error)) + { +
@error
+ } + +
+ + +
+ +
+ + +
Když necháš prázdné, vytvoří se automaticky z názvu.
+
+ +
+ + +
+ + + Zrušit +
+} @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().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().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().Add(new Category { Name = name, Slug = slug, IsActive = form.IsActive }); + } + else + { + var cat = await Db.Set().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; + } +} \ No newline at end of file diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriList.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriList.razor index df91277..023e11e 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriList.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/CategoriList.razor @@ -1,5 +1,112 @@ -

CategoriList

+@page "/admin/kategorie" +@rendermode InteractiveServer +@using Microsoft.EntityFrameworkCore +@using Web_Jarmark.Data +@using Web_Jarmark.Data.Models +@inject ApplicationDbContext Db +@inject NavigationManager Nav + +Kategorie + +
+

Kategorie

+ + Nová kategorie +
+ +@if (!string.IsNullOrEmpty(error)) +{ +
@error
+} + +@if (categories is null) +{ +

Načítám…

+} +else if (categories.Count == 0) +{ +

Zatím tu nejsou žádné kategorie.

+} +else +{ + + + + + + + + + + + + @foreach (var c in categories) + { + + + + + + + + } + +
NázevSlugPočet produktůAktivníAkce
@c.Name@c.Slug@c.ProductCount + + + Upravit + +
+} @code { + private List? categories; + private string? error; -} + protected override async Task OnInitializedAsync() => await Load(); + + private async Task Load() + { + categories = await Db.Set() + .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().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() + .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().Remove(cat); + await Db.SaveChangesAsync(); + await Load(); + } + + private record CategoryRow(int Id, string Name, string Slug, bool IsActive, int ProductCount); +} \ No newline at end of file diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductEdit.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductEdit.razor index af33527..78be197 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductEdit.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductEdit.razor @@ -1,5 +1,186 @@ -

ProductEdit

+@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 + +@(Id is null ? "Nové zboží" : "Upravit zboží") + +

@(Id is null ? "Nové zboží" : "Upravit zboží")

+ +@if (notFound) +{ +
Zboží nebylo nalezeno.
+ Zpět na seznam +} +else if (categories is null) +{ +

Načítám…

+} +else if (categories.Count == 0) +{ +
+ Nejdřív musíš vytvořit aspoň jednu kategorii. + Přidat kategorii +
+} +else +{ + + + + + @if (!string.IsNullOrEmpty(error)) + { +
@error
+ } + +
+ + +
+ +
+ + + + @foreach (var c in categories) + { + + } + +
+ +
+ + +
+ +
+
+ + +
+
+ + +
+
+ +
+ + +
Např. /images/cukrovi.jpg (soubor v wwwroot) nebo celá adresa.
+
+ +
+ + +
+ + + Zrušit +
+} @code { + [Parameter] public int? Id { get; set; } -} + private ProductForm form = new(); + private List? categories; + private string? error; + private bool notFound; + + protected override async Task OnParametersSetAsync() + { + categories = await Db.Set().AsNoTracking().OrderBy(c => c.Name).ToListAsync(); + + if (Id is null) return; + + var p = await Db.Set().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().Add(p); + } + else + { + var p = await Db.Set().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; + } +} \ No newline at end of file diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductList.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductList.razor index da784f0..3c0ad75 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductList.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/ProductList.razor @@ -1,5 +1,109 @@ -

ProductList

+@page "/admin/zbozi" +@rendermode InteractiveServer +@using Microsoft.EntityFrameworkCore +@using Web_Jarmark.Data +@using Web_Jarmark.Data.Models +@inject ApplicationDbContext Db + +Zboží + +
+

Zboží

+ + Nové zboží +
+ +@if (!string.IsNullOrEmpty(error)) +{ +
@error
+} + +@if (products is null) +{ +

Načítám…

+} +else if (products.Count == 0) +{ +

Zatím tu není žádné zboží.

+} +else +{ + + + + + + + + + + + + + @foreach (var p in products) + { + + + + + + + + + } + +
NázevKategorieCenaSklademViditelnéAkce
@p.Name@p.CategoryName@p.Price.ToString("N2") Kč@p.StockQuantity + + + Upravit + +
+} @code { + private List? products; + private string? error; -} + protected override async Task OnInitializedAsync() => await Load(); + + private async Task Load() + { + products = await Db.Set() + .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().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() + .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().Remove(p); + await Db.SaveChangesAsync(); + await Load(); + } + + private record ProductRow(int Id, string Name, string CategoryName, decimal Price, int StockQuantity, bool IsVisible); +} \ No newline at end of file diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-shm b/Web-Jarmark/Web-Jarmark/jarmark.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..6ef8a1d6c0e73eada4212dec3c51117e2487ff24 GIT binary patch literal 32768 zcmeI*FA4%d6bIn-KbVZ0#9$Lm%N8$T@C0^?X1moB7`%eT>QTIbRfEo`h)J|8EPOwB z%KwOynl zQZul9ly`}≷=W!wD`F)4f3B{3)j5I_I{1Q0*~ z0R#|0009IL=of(z*>gZi(-oPFla59jis7v75#2u zK@F4~H}96sfN58Q(S080@?dr5`#hj0R#|0009ILKmY**5I_KdJ{0hH6y+zU Cc&Rr4 literal 0 HcmV?d00001