Created
July 2, 2023 17:35
-
-
Save christopherbauer/12fa7999b364809fd9e68d824648492c to your computer and use it in GitHub Desktop.
orderRouter with repository
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 itemRouter from "./item"; | |
import orderRepository from "./data"; | |
const orderRouter = express.Router(); | |
orderRouter | |
.route("/") | |
.get(async (req: Request<{}, {}, {}, {}>, res) => { | |
const orders = await orderRepository.list(); | |
res.status(200).send(orders); | |
}) | |
.post(async (req: Request<{}, {}, { user_id: number }, {}>, res) => { | |
const { body } = req; | |
const { user_id } = body; | |
const order = await orderRepository.createOrder(user_id); | |
res.status(200).send(order); | |
}); | |
orderRouter.route("/:id").get(async (req: Request<{ id: number }, {}, {}, {}>, res) => { | |
const { params } = req; | |
const { id } = params; | |
const orders = await orderRepository.getOrder(Number(id)); | |
res.status(200).send(orders); | |
}); | |
orderRouter.use(itemRouter); | |
export default orderRouter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment