Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
The most significant changes involve the refactoring of the `ProductP…
Browse files Browse the repository at this point in the history
…atchCategory` method in `Patch.cs` and the modification of the `AdminProductCategory` function in `Category.tsx`. The `ProductPatchCategory` method now includes a more comprehensive check and handling of product categories, including the creation of new categories, checking if the current category is in use, and deleting unused categories. The `AdminProductCategory` function has been updated to include a check for the truthiness of `cate` before calling the `run` function.

Changes:

1. Refactored the `ProductPatchCategory` method in `Patch.cs` to include a more comprehensive check and handling of product categories. The method now checks if the new category exists, creates a new one if it doesn't, checks if the product's current category is the same as the new one, checks if the current category is in use if they are not the same and the current category is not null, deletes the current category if it's not in use, assigns the new category to the product, and saves the changes to the database.

2. Modified the `AdminProductCategory` function in `Category.tsx` to check if `cate` is truthy before calling the `run` function. If `cate` is falsy, the function does nothing.

3. Imported the `Subtitle2` component from `@fluentui/react-components` in `Card.tsx` and updated the `GalleryCard` function to wrap the `Link` component inside a `Subtitle2` component in the `CardFooter`.

4. Updated the `version` in the author comment for the `GalleryCard` function in `Card.tsx` from `0.1.4` to `0.1.5`.
  • Loading branch information
Aloento committed Jan 17, 2024
1 parent 170165a commit ed07695
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
41 changes: 26 additions & 15 deletions SoarCraft.AwaiShop/AdminHub/Product/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task<bool> ProductPatchName(uint prodId, string name) {
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 1.0.0
* @version 1.1.0
* </remarks>
*/
public async Task<bool> ProductPatchCategory(uint prodId, string name) {
Expand All @@ -46,23 +46,34 @@ public async Task<bool> 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;

Expand Down
8 changes: 7 additions & 1 deletion src/Pages/Admin/Product/Category.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ export function AdminProductCategory({ ProdId }: { ProdId: number; }) {
</Combobox>

{edit
? <Button appearance="subtle" icon={<SendRegular />} onClick={() => cate && run(ProdId, cate)} />
? <Button appearance="subtle" icon={<SendRegular />} onClick={() => {
if (cate)
run(ProdId, cate)
else {

}
}} />
: <Button appearance="subtle" icon={<EditRegular />} onClick={setTrue} />}
</div>
);
Expand Down
8 changes: 5 additions & 3 deletions src/Pages/Gallery/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Card, CardFooter, CardPreview, Link, makeStyles, tokens } from "@fluentui/react-components";
import { Card, CardFooter, CardPreview, Link, Subtitle2, makeStyles, tokens } from "@fluentui/react-components";
import { useRequest } from "ahooks";
import { GuidImage } from "~/Helpers/GuidImage";
import { Logger } from "~/Helpers/Logger";
Expand Down Expand Up @@ -28,7 +28,7 @@ const log = new Logger("Gallery", "Category", "Card");
/**
* @author Aloento
* @since 0.5.0
* @version 0.1.4
* @version 0.1.5
*/
export function GalleryCard({ Id }: { Id: number }) {
const style = useStyles();
Expand All @@ -43,7 +43,9 @@ export function GalleryCard({ Id }: { Id: number }) {
</CardPreview>

<CardFooter>
<Link href={`/Product/${Id}`}>{data?.Name || "Loading..."}</Link>
<Subtitle2>
<Link href={`/Product/${Id}`}>{data?.Name || "Loading..."}</Link>
</Subtitle2>
</CardFooter>
</Card>
)
Expand Down

0 comments on commit ed07695

Please sign in to comment.