|
|
|
@ -5,9 +5,9 @@ import { verifyAuthToken } from './utils'
|
|
|
|
|
const productRoutes = (app: express.Application) => {
|
|
|
|
|
app.get('/products', index)
|
|
|
|
|
app.get('/products/:id', read)
|
|
|
|
|
app.post('/products/create', verifyAuthToken, create)
|
|
|
|
|
app.put('/products/:id', verifyAuthToken, update)
|
|
|
|
|
app.delete('/products/:id', verifyAuthToken, destroy)
|
|
|
|
|
app.post('/products', create)
|
|
|
|
|
app.put('/products/:id', update)
|
|
|
|
|
app.delete('/products/:id', destroy)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const store = new ProductStore()
|
|
|
|
@ -41,7 +41,6 @@ const create = async (req: Request, res: Response) => {
|
|
|
|
|
name: req.body.name,
|
|
|
|
|
price: req.body.price,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newProduct = await store.create(productInfo)
|
|
|
|
|
res.json(newProduct)
|
|
|
|
|
} catch (err) {
|
|
|
|
@ -53,11 +52,13 @@ const create = async (req: Request, res: Response) => {
|
|
|
|
|
|
|
|
|
|
const update = async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
|
|
|
|
const product = await store.update(req.body)
|
|
|
|
|
res.json({
|
|
|
|
|
const productInfo: Product = {
|
|
|
|
|
id: parseInt(req.params.id),
|
|
|
|
|
name: req.body.name,
|
|
|
|
|
price: req.body.price,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
const updatedProduct = await store.update(productInfo)
|
|
|
|
|
res.json(updatedProduct)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.status(400)
|
|
|
|
|
res.json(err)
|
|
|
|
@ -66,7 +67,7 @@ const update = async (req: Request, res: Response) => {
|
|
|
|
|
|
|
|
|
|
const destroy = async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
|
|
|
|
const deleted = await store.delete(req.body.id)
|
|
|
|
|
const deleted = await store.delete(parseInt(req.params.id))
|
|
|
|
|
res.json(deleted)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.status(400)
|
|
|
|
|