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

[Back] Use TypeId | ProductGetTypes | ProductGetVariants | ProductPatchType | GuestVisit - [Front] AdminProductComboDetail | AdminProductNewCombo | useVariants | useTypes | useTypeList | useVariantName | useType | useVariant #113

Merged
merged 6 commits into from
Mar 2, 2024
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## AwaiShop

A Simple E-Commerce System with C# & SignalR & FluentUI3 & AzureAD

## Basic Requirements

- .NET 8
- Node.js 17+
- Visual Studio 2022 Preview
- Visual Studio Code
6 changes: 3 additions & 3 deletions SoarCraft.AwaiShop/AdminHub/Product/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ private async Task deleteType(Type type) {
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 0.2.0
* @version 0.3.0
* </remarks>
*/
public async Task<bool> ProductDeleteType(uint variantId, string reqType) {
public async Task<bool> ProductDeleteType(uint typeId) {
await this.deleteType(
await this.Db.Types
.Where(x => x.VariantId == variantId && x.Name == reqType)
.Where(x => x.TypeId == typeId)
.IncludeOptimized(x => x.Combos)
.SingleAsync()
);
Expand Down
24 changes: 17 additions & 7 deletions SoarCraft.AwaiShop/AdminHub/Product/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,25 @@ await this.Db.Products
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 1.0.0
* @version 1.1.0
* </remarks>
*/
public async Task<dynamic[]> ProductGetVariants(uint prodId) =>
await this.Db.Variants
public Task<uint[]> ProductGetVariants(uint prodId) =>
this.Db.Variants
.Where(x => x.ProductId == prodId)
.Select(x => new {
x.VariantId,
Types = x.Types.Select(t => t.TypeId).ToArray()
})
.Select(x => x.VariantId)
.ToArrayAsync();

/**
* <remarks>
* @author Aloento
* @since 1.3.0
* @version 0.1.0
* </remarks>
*/
public Task<uint[]> ProductGetTypes(uint variantId) =>
this.Db.Types
.Where(x => x.VariantId == variantId)
.Select(x => x.TypeId)
.ToArrayAsync();
}
6 changes: 3 additions & 3 deletions SoarCraft.AwaiShop/AdminHub/Product/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ private async Task<List<Combo>> archiveCombos(ICollection<Combo> oldCombos) {
* <remarks>
* @author Aloento
* @since 0.1.0
* @version 1.0.0
* @version 1.1.0
* </remarks>
*/
public async Task<bool> ProductPatchType(uint variantId, string oldName, string newName) {
public async Task<bool> ProductPatchType(uint typeId, string newName) {
var valid = typeof(Type)
.GetProperty(nameof(Type.Name))!
.GetCustomAttribute<StringLengthAttribute>()!;
Expand All @@ -240,7 +240,7 @@ public async Task<bool> ProductPatchType(uint variantId, string oldName, string
throw new HubException(valid.FormatErrorMessage("Name"));

var type = this.Db.Types
.Where(x => x.VariantId == variantId && x.Name == oldName);
.Where(x => x.TypeId == typeId);

var any = await type
.SelectMany(x => x.Combos)
Expand Down
8 changes: 4 additions & 4 deletions SoarCraft.AwaiShop/AdminHub/Product/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Task<uint> ProductPostPhoto(uint prodId, IAsyncEnumerable<byte[]> i
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.1.1
* </remarks>
*/
public async Task<uint> ProductPostVariant(uint prodId, string name) {
Expand Down Expand Up @@ -138,14 +138,14 @@ public async Task<uint> ProductPostVariant(uint prodId, string name) {
});

await this.Db.SaveChangesAsync();
return temp.Entity.ProductId;
return temp.Entity.VariantId;
}

/**
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.1.1
* </remarks>
*/
public async Task<uint> ProductPostType(uint variantId, string name) {
Expand Down Expand Up @@ -173,7 +173,7 @@ public async Task<uint> ProductPostType(uint variantId, string name) {
});

await this.Db.SaveChangesAsync();
return temp.Entity.VariantId;
return temp.Entity.TypeId;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion SoarCraft.AwaiShop/Helpers/LoggerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ namespace SoarCraft.AwaiShop.Helpers;
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.2.0
* </remarks>
*/
internal static partial class LoggerExtension {
[LoggerMessage(
EventId = 1001,
Level = LogLevel.Debug,
Message = "Guest : Visit from [{ip}]"
)]
private static partial void guestVisit(ILogger logger, string? ip);

public static void GuestVisit(this ILogger logger, HubCallerContext ctx) =>
guestVisit(logger, ctx.GetHttpContext()?.Connection.RemoteIpAddress?.ToString());

[LoggerMessage(
EventId = 2001,
Level = LogLevel.Information,
Message = "User {name} : [{uid}] Logged from [{ip}]"
)]
private static partial void userLogin(ILogger logger, string? name, string? uid, string? ip);
Expand Down
5 changes: 3 additions & 2 deletions SoarCraft.AwaiShop/Hub/ShopHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal partial class ShopHub(ShopContext db, ILogger<ShopHub> logger) : CraftH
* <remarks>
* @author Aloento
* @since 0.5.0
* @version 0.1.0
* @version 0.1.1
* </remarks>
*/
public override async Task OnConnectedAsync() {
Expand All @@ -34,7 +34,8 @@ public override async Task OnConnectedAsync() {
await this.Clients.Caller.OnNewUser();
this.Context.Items.TryAdd("NewUser", true);
}
}
} else
this.Logger.GuestVisit(this.Context);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "awaishop",
"private": true,
"version": "1.4.0",
"version": "1.4.5",
"type": "module",
"author": {
"name": "Aloento",
Expand Down
10 changes: 7 additions & 3 deletions src/Pages/Admin/Order/Ship.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Field, Input, Toast, ToastTitle } from "@fluentui/react-components";
import { EditRegular, SendRegular } from "@fluentui/react-icons";
import { useBoolean } from "ahooks";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useOrder } from "~/Components/Order/useOrder";
import { Logger } from "~/Helpers/Logger";
import { useErrorToast } from "~/Helpers/useToast";
Expand All @@ -12,14 +12,18 @@ const log = new Logger("Admin", "Order", "Detail", "Shipment");
/**
* @author Aloento
* @since 0.5.0
* @version 0.3.0
* @version 0.3.1
*/
export function Shipment({ OrderId }: { OrderId: number }) {
const [edit, { setTrue, setFalse }] = useBoolean();
const { dispatch, dispatchToast } = useErrorToast(log);

const { data: order, mutate } = useOrder(OrderId, true);
const [track, setTrack] = useState(order?.TrackingNumber);
const [track, setTrack] = useState<string>("");

useEffect(() => {
order?.TrackingNumber && setTrack(order?.TrackingNumber);
}, [order]);

const { run } = AdminHub.Order.Post.useShip({
manual: true,
Expand Down
52 changes: 43 additions & 9 deletions src/Pages/Admin/Product/Combo/Detail.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Button, Combobox, DataGridCell, DataGridHeaderCell, Dialog, DialogBody, DialogContent, DialogSurface, DialogTitle, DialogTrigger, Label, Option, SpinButton, TableColumnDefinition, Toast, ToastTitle, createTableColumn, makeStyles, tokens } from "@fluentui/react-components";
import { DismissRegular, EditRegular } from "@fluentui/react-icons";
import { useBoolean, useRequest } from "ahooks";
import { useAsyncEffect, useBoolean } from "ahooks";
import { useState } from "react";
import { DelegateDataGrid } from "~/Components/DataGrid";
import { Logger } from "~/Helpers/Logger";
import { Flex } from "~/Helpers/Styles";
import { useErrorToast } from "~/Helpers/useToast";
import { Hub } from "~/ShopNet";
import { AdminHub } from "~/ShopNet/Admin";
import { IComboItem } from ".";
import { IVariantItem } from "../Variant";
import { IUpdateComboItem, IVariantItem } from "./New";

/**
* @author Aloento
* @since 0.5.0
* @version 0.1.0
*/
interface IEditComboItem extends IVariantItem {
interface IEditComboItem extends IUpdateComboItem {
Current: string;
Update: (type: string) => void;
}

/**
Expand Down Expand Up @@ -80,6 +80,7 @@ const useStyles = makeStyles({
*/
export interface IDetailComboItem extends IComboItem {
ProdId: number;
/** @deprecated */
Refresh: () => void;
}

Expand All @@ -88,21 +89,48 @@ const log = new Logger("Admin", "Product", "Detail", "Combo", "Detail");
/**
* @author Aloento
* @since 0.5.0
* @version 0.2.3
* @version 0.3.0
*/
export function AdminProductComboDetail({ Id, ProdId, Combo, Stock, Refresh }: IDetailComboItem) {
const [open, { toggle }] = useBoolean();
const [combo, setCombo] = useState(Combo);
const [stock, setStock] = useState(Stock);

const { data: varis } = useRequest(() => AdminHub.Product.Get.Variants(ProdId, log), {
const [varis, setVaris] = useState<IVariantItem[]>([]);
const { data: varIds } = AdminHub.Product.Get.useVariants(ProdId, {
onError: log.error
});

useAsyncEffect(async () => {
if (!varIds)
return;

const varis: IVariantItem[] = [];

for (const i of varIds) {
const typeIds = await AdminHub.Product.Get.Types(i);
const types = [];

for (const typeId of typeIds) {
const type = await Hub.Product.Get.Type(typeId);
types.push(type);
}

const { Name } = await Hub.Product.Get.Variant(i);

varis.push({
Id: i,
Name: Name,
Types: types.map(x => x.Name)
});
}

setVaris(varis);
}, [varIds]);

const { dispatch, dispatchToast } = useErrorToast(log);

const { run } = AdminHub.Product.Patch.useCombo({
manual: true,
const { run, loading } = AdminHub.Product.Patch.useCombo({
onError(e, req) {
dispatch({
Message: "Failed Update Combo",
Expand Down Expand Up @@ -167,7 +195,13 @@ export function AdminProductComboDetail({ Id, ProdId, Combo, Stock, Refresh }: I
setStock(val);
}} />

<Button appearance="primary" onClick={() => run(Id, combo, stock)}>Submit</Button>
<Button
disabled={loading}
appearance="primary"
onClick={() => run(Id, combo, stock)}
>
Submit
</Button>
</div>
</DialogContent>
</DialogBody>
Expand Down
Loading
Loading