Created
July 2, 2023 02:23
-
-
Save christopherbauer/33d8f717eede1f6d18a4f4c97ae5bc61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express, { Request } from "express"; | |
import * as db from "zapatos/db"; | |
import pool from "../pool"; | |
const orderRouter = express.Router(); | |
orderRouter | |
.route("/") | |
.get(async (req: Request<{}, {}, {}, {}>, res) => { | |
const orders = await db | |
.select( | |
"Order", | |
{}, | |
{ | |
lateral: { | |
items: db.select("OrderItem", { | |
order_id: db.parent("order_id"), | |
}), | |
}, | |
} | |
) | |
.run(pool); | |
res.status(200).send(orders); | |
}) | |
.post( | |
async ( | |
req: Request< | |
{}, | |
{}, | |
{ | |
user_id: number; | |
}, | |
{} | |
>, | |
res | |
) => { | |
const { body } = req; | |
const { user_id } = body; | |
const order = await db | |
.insert("Order", { | |
user_id: user_id, | |
status: "open", | |
order_date: new Date(), | |
total_amount: 0, | |
}) | |
.run(pool) | |
.catch((ex) => { | |
res.status(400).send(ex); | |
}); | |
res.status(200).send(order); | |
} | |
); | |
export default orderRouter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment