Review this generated create-order endpoint before it is connected to the published contract.
Implement POST /orders for the authenticated customer, accept a positive integer quantity, create one durable order under retries, and return 201 with Location after recording an event.
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}`,
});
}
generated code is illustrative, not from any one model