Files
recipe-manager/server/routes/groceryList.ts
2023-02-11 16:25:30 -06:00

57 lines
1.6 KiB
TypeScript

import { Express, Router } from "express";
import { GroceryListCtl } from "../controllers";
const groceryinstance = new GroceryListCtl();
const router = Router();
export const groceryListRoute = (app: Express) => {
app.use('/app/grocery-list', router);
router.get('/', async (req, res, next) => {
const userid = req.query.userid as string;
try {
if (userid) {
const { code, data } = await groceryinstance.getByUserID(userid);
res.status(code).send(data);
} else {
const { code, data } = await groceryinstance.getAll();
res.status(code).send(data);
}
} catch(e) {
next(e);
}
})
router.get('/:id', async (req, res, next) => {
const { id } = req.params;
try {
const { code, data } = await groceryinstance.getOne(id);
res.status(code).send(data);
} catch(e) {
next(e);
}
})
router.post('/', async (req, res, next) => {
const data = req.body;
try {
const result = await groceryinstance.post(data);
res.status(result.code).send(result.data);
} catch(e) {
next(e);
}
})
router.put('/:id', async (req, res, next) => {
const { id } = req.params;
const data = req.body;
try {
const result = await groceryinstance.put(id, data);
res.status(result.code).send(result.data);
} catch(e) {
next(e);
}
})
}