商品ID、商品數量、使用哪個payment provider、 使用哪個payment way
OrderModel
,用於操作資料庫中的訂單資料:import { Knex } from "knex";
import { Base, IBase } from "./base";
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;
}
Order
介面:export interface Order {
id: string;
total: number;
createdAt: Date;
updatedAt: Date;
paymentProvider: PaymentProvider;
paymentWay: PaymentWay;
status: OrderStatus;
contents: OrderContent[];
}
IOrderModel
介面:export interface IOrderModel extends IBase<Order> { }
OrderModel
類別:export class OrderModel extends Base<Order> implements IOrderModel
{
tableName = 'orders';
schema = {
id: 'id',
total: 'total',
createdAt: 'created_at',
updatedAt: 'updated_at',
paymentProvider: 'payment_provider',
paymentWay: 'payment_way',
status: 'status',
contents: 'contents',
};
createModel
:static createModel =
({ knexSql, tableName }: { knexSql: Knex; tableName?: string })
=> {
return new OrderModel({ knexSql, tableName });
}
constructor({ knexSql, tableName }: { knexSql: Knex, tableName?: string }) {
super({ knexSql, tableName });
}
import { IOrderModel, OrderContent, PaymentProvider, PaymentWay } from "@/model/order";
import { IProductModel } from "@/model/product";
import { NextFunction, Request, Response } from "express";
import { Knex } from "knex";
CreateOrderRequestParams
介面:interface CreateOrderRequestParams {
paymentProvider: PaymentProvider;
paymentWay: PaymentWay;
contents: OrderContent[];
}
IOrderController
介面: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;
}
knexSql
資料庫連接實例賦值給 this.knexSql
屬性。orderModel
實例賦值給 this.orderModel
屬性,這樣可以在 OrderController
中方便地操作訂單資料。