在这段生成的创建订单端点接入已发布契约前,对它进行审查。
为已认证客户实现 POST /orders,接受正整数数量,在重试时只持久创建一个订单,并在记录事件后返回带 Location 的 201 响应。
JavaScript
async function createOrder(request, db, events) {
const body = await request.json();
const product = await db.products.find(body.sku);
const inventory = await db.inventory.find(body.sku);
if (!product || !inventory) {
return json(404, { title: "Product not found" });
}
const order = {
id: crypto.randomUUID(),
customerId: body.customerId,
sku: body.sku,
quantity: Number(body.quantity),
};
await db.orders.insert(order);
await events.publish("order.created", order);
return json(201, order, {
Location: `/orders/${order.id}`,
});
}
生成代码仅作示例,不代表任何特定模型