mirror of
https://github.com/andwati/order-service.git
synced 2026-11-24 10:18:45 +00:00
feat: add validation for product ID in update method and implement placeholder methods in order service
This commit is contained in:
@@ -23,6 +23,10 @@ export class ProductController {
|
|||||||
static async update(req: Request, res: Response, next: NextFunction) {
|
static async update(req: Request, res: Response, next: NextFunction) {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
if (typeof id !== "string") {
|
||||||
|
res.status(400);
|
||||||
|
return next({ statusCode: 400, message: "Invalid product id" });
|
||||||
|
}
|
||||||
const product = await ProductService.update(id, req.body);
|
const product = await ProductService.update(id, req.body);
|
||||||
res.status(200).json(product);
|
res.status(200).json(product);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export class OrderRepository {
|
|||||||
data: Omit<OrderDocument, "_id" | "createdAt" | "updatedAt">,
|
data: Omit<OrderDocument, "_id" | "createdAt" | "updatedAt">,
|
||||||
session: ClientSession,
|
session: ClientSession,
|
||||||
): Promise<OrderDocument> {
|
): Promise<OrderDocument> {
|
||||||
return OrderModel.create([data], { session }).then((res) => res[0]);
|
return OrderModel.create(data, { session });
|
||||||
}
|
}
|
||||||
|
|
||||||
static findById(id: string, session?: ClientSession) {
|
static findById(id: string, session?: ClientSession) {
|
||||||
@@ -21,7 +21,14 @@ export class OrderRepository {
|
|||||||
return OrderModel.find({ userId }).lean();
|
return OrderModel.find({ userId }).lean();
|
||||||
}
|
}
|
||||||
|
|
||||||
static save(order: OrderDocument, session?: ClientSession) {
|
static save(
|
||||||
return order.save({ session });
|
order: OrderDocument & {
|
||||||
|
save: (options?: {
|
||||||
|
session?: ClientSession | null;
|
||||||
|
}) => Promise<OrderDocument>;
|
||||||
|
},
|
||||||
|
session?: ClientSession,
|
||||||
|
) {
|
||||||
|
return order.save({ session: session ?? null });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,15 @@ import { ProductRepository } from "../repositories/product.repo.js";
|
|||||||
import { OrderRepository } from "../repositories/order.repo.js";
|
import { OrderRepository } from "../repositories/order.repo.js";
|
||||||
|
|
||||||
export class OrderService {
|
export class OrderService {
|
||||||
|
static listOrders(userId: string, role: string) {
|
||||||
|
throw new Error("Method not implemented.");
|
||||||
|
}
|
||||||
|
static payOrder(id: string | string[] | undefined) {
|
||||||
|
throw new Error("Method not implemented.");
|
||||||
|
}
|
||||||
|
static cancelOrder(id: string | string[] | undefined) {
|
||||||
|
throw new Error("Method not implemented.");
|
||||||
|
}
|
||||||
static async createOrder(
|
static async createOrder(
|
||||||
userId: string,
|
userId: string,
|
||||||
items: { productId: string; quantity: number }[],
|
items: { productId: string; quantity: number }[],
|
||||||
|
|||||||
Reference in New Issue
Block a user