diff --git a/SoarCraft.AwaiShop/AdminHub/Product/Patch.cs b/SoarCraft.AwaiShop/AdminHub/Product/Patch.cs index 3312a1e..4abe83a 100644 --- a/SoarCraft.AwaiShop/AdminHub/Product/Patch.cs +++ b/SoarCraft.AwaiShop/AdminHub/Product/Patch.cs @@ -35,7 +35,7 @@ public async Task ProductPatchName(uint prodId, string name) { * * @author Aloento * @since 0.1.0 - * @version 1.0.0 + * @version 1.1.0 * */ public async Task ProductPatchCategory(uint prodId, string name) { @@ -46,23 +46,34 @@ public async Task ProductPatchCategory(uint prodId, string name) { if (!valid.IsValid(name)) throw new HubException(valid.FormatErrorMessage("Name")); - var newCate = await this.Db.Categories - .Where(x => x.Name == name) - .SingleOrDefaultAsync() - ?? (await this.Db.Categories.AddAsync(new() { - Name = name - })).Entity; - var prod = await this.Db.Products - .Include(x => x.Category) .SingleAsync(x => x.ProductId == prodId); - var inUse = await this.Db.Products - .Where(x => x.CategoryId == prod.CategoryId && x.ProductId != prod.ProductId) - .AnyAsync(); - - if (!inUse) - this.Db.Categories.Remove(prod.Category!); + var newCate = await this.Db.Categories + .Where(x => x.Name == name) + .SingleOrDefaultAsync(); + + if (newCate is null) + newCate = (await this.Db.Categories.AddAsync(new() { + Name = name + })).Entity; + else { + if (prod.CategoryId == newCate.CategoryId) + return true; + + if (prod.CategoryId is not null) { + var inUse = await this.Db.Products + .Where(x => + x.CategoryId == prod.CategoryId && + x.ProductId != prod.ProductId) + .AnyAsync(); + + if (!inUse) + await this.Db.Categories + .Where(x => x.CategoryId == prod.CategoryId) + .ExecuteDeleteAsync(); + } + } prod.Category = newCate; diff --git a/src/Pages/Admin/Product/Category.tsx b/src/Pages/Admin/Product/Category.tsx index 6abedf2..84f4dda 100644 --- a/src/Pages/Admin/Product/Category.tsx +++ b/src/Pages/Admin/Product/Category.tsx @@ -119,7 +119,13 @@ export function AdminProductCategory({ ProdId }: { ProdId: number; }) { {edit - ?