商品ID、商品數量、使用哪個payment provider、 使用哪個payment way
OrderModel
,所需要的訂單(Order
)資料格式:import { Base } from "./base";
//PaymentProvider 介面:定義支付平台的結構。
export enum PaymentProvider {
ECPAY = "ECPAY",
PAYPAL = "PAYPAL"
}
//PaymentWay 介面:定義支付方式的結構。
export enum PaymentWay {
CVS = 'CVS',
PAYPAL = 'PAYPAL',
}
//OrderStatus 介面:定義訂單狀態的結構。
export enum OrderStatus {
WAITING = 'WAITING',
SUCCESS = 'SUCCESS',
FAILED = 'FAILED',
CANCEL = 'CANCEL',
}
//訂單的詳細資料
export interface OrderContent {
productId: number;
amount: number;
price: number;
}
//Order 介面定義了訂單的結構
export interface Order {
id: string;
total: number;
createdAt: Date;
updatedAt: Date;
paymentProvider: PaymentProvider;
paymentWay: PaymentWay;
status: OrderStatus;
contents: OrderContent[];
}
import { Knex } from "knex";
import { Base, IBase } from "./base";
Order
介面:export interface Order {
id: string;
total: number;
createdAt: Date;
updatedAt: Date;
paymentProvider: PaymentProvider;
paymentWay: PaymentWay;
status: OrderStatus;
contents: OrderContent[];
}
export interface Order {}
:定義了一個 Order
介面,描述訂單的結構。export enum PaymentProvider {
ECPAY = 'ECPAY',
PAYPAL = 'PAYPAL',
}
export enum PaymentWay {
CVS = 'CVS',
PAYPAL = 'PAYPAL',
}
export enum OrderStatus {
WAITING = 'WAITING',
SUCCESS = 'SUCCESS',
FAILED = 'FAILED',
CANCEL = 'CANCEL',
}
OrderContent
介面:訂單的詳細資料export interface OrderContent {
productId: number;
amount: number;
price: number;
}
export class OrderModel extends Base<Order> implements IBase<Order> {
tableName = 'orders';
//屬性會自動對應到資料庫表中的相應欄位
schema = {
id: 'id',
total: 'total',
createdAt: 'created_at',
updatedAt: 'updated_at',
paymentProvider: 'payment_provider',
paymentWay: 'payment_way',
status: 'status',
contents: 'contents',
}
}
OrderModel
,它繼承了 Base<Order>
。這表示 OrderModel
類別將具有 Base
類別的所有屬性和方法,但它特別針對 Order
這個型別進行操作。createModel
:專門用來生成模型(model)static createModel =
({ knexSql, tableName }: { knexSql: Knex; tableName?: string })
=> {
return new OrderModel({ knexSql, tableName });
}
constructor({ knexSql, tableName }: { knexSql: Knex, tableName?: string }) {
super({ knexSql, tableName });
}
OrderModel
實例:在創建 OrderModel
實例時,可以傳入資料庫連接(knexSql
)和可選的資料表名稱(tableName
),這些參數將被傳遞給基類的建構函數,以正確初始化基類的狀態。super
,確保基類的建構函數被正確調用,使得 OrderModel
繼承了基類的屬性和方法。export interface IOrderModel extends IBase<Order> {
create(orderData: Omit<Order, "id"> & { id?: string },
trx?: Knex.Transaction): Promise<Order | null>; }
export class OrderModel extends Base<Order> implements IOrderModel
import { IOrderModel, OrderContent, PaymentProvider, PaymentWay } from "@/model/order";
import { IProductModel } from "@/model/product";
import { NextFunction, Request, Response } from "express";
import { Knex } from "knex";
export interface IOrderController {
createOrder(
req: Request<any, any, CreateOrderRequestParams, any>,
res: Response,
next: NextFunction
): void;
updateAmount(
req: Request<any, any, any, any>,
res: Response,
next: NextFunction
): void;
}
OrderController
類別:export class OrderController implements IOrderController {
knexSql: Knex;
orderModel: IOrderModel;
productModel: IProductModel;
constructor({ knexSql, orderModel, productModel }: {
knexSql: Knex;
orderModel: IOrderModel;
productModel: IProductModel;
}) {
this.knexSql = knexSql;
this.orderModel = orderModel;
this.productModel = productModel
}
}
OrderController
中,宣告了三個成員變數 knexSql
、orderModel
和 productModel
。這些變數的類型分別是 Knex
、IOrderModel
和 IProductModel
,用來存儲傳遞給建構子函數的值。knexSql
資料庫連接實例賦值給 this.knexSql
屬性。orderModel
實例賦值給 this.orderModel
屬性,這樣可以在 OrderController
中方便地操作訂單資料。createOrder
的公開方法,:public createOrder: IOrderController["createOrder"] = (req, res, _next) => {
let { paymentProvider, paymentWay, contents } = req.body
}
public createOrder
:這表示這個方法是公開的,能夠從類別外部存取。IOrderController["createOrder"]
:這表示這個方法的型別必須符合 IOrderController
介面中的 createOrder
方法的定義。(req, res, _next) => {}
:這是一個箭頭函數,接受三個參數 req
(請求物件)、res
(回應物件)、_next
(下一個中介軟體函數,這裡沒用到,所以用 _
前綴表示未使用)。req.body
中提取 paymentProvider
、paymentWay
和 contents
這三個屬性。paymentProvider
:支付提供者,例如 PayPal、信用卡等。paymentWay
:支付方式,例如在線支付、貨到付款等。contents
:訂單的內容,通常是包含商品詳細資訊的數據。CreateOrderRequestParams
介面:請求物件 req.body 的結構interface CreateOrderRequestParams {
paymentProvider: PaymentProvider;
paymentWay: PaymentWay;
contents: OrderContent[];
}
createOrder(req: Request<any, any, CreateOrderRequestParams, any>, res: Response, next: NextFunction): void;