You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.6 KiB
TypeScript

import express, { Request, Response } from 'express'
import { Order, OrderProduct, OrderStore } from '../models/order'
import { verifyAuthToken } from './utils'
const orderRoutes = (app: express.Application) => {
app.get('/orders', index)
app.get('/orders/:id', read)
app.post('/orders', create)
app.post('/orders/:id/products', addProduct)
}
const store = new OrderStore()
const index = async (req: Request, res: Response) => {
try {
const orders = await store.index()
res.json(orders)
} catch (err) {
res.status(400)
res.json(err)
}
}
const read = async (req: Request, res: Response) => {
try {
const order = await store.read(req.params.id)
res.json(order)
} catch (err) {
res.status(400)
res.json(err)
}
}
const create = async (req: Request, res: Response) => {
try {
const orderInfo: Order = {
status: req.body.status,
userId: req.body.userId
}
const newOrder = await store.create(orderInfo)
res.json(newOrder)
} catch (err) {
res.status(400)
res.json(err)
}
}
const addProduct = async (req: Request, res: Response) => {
try {
const orderProductInfo: OrderProduct = {
quantity: parseInt(req.body.quantity),
orderId: parseInt(req.params.id),
productId: req.body.productId
}
const addedProduct = await store.addProduct(orderProductInfo)
res.json(addedProduct)
} catch (err) {
res.status(400)
res.json(err)
}
}
export default orderRoutes