grocery list route
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Express, Router } from "express";
|
||||
import { GroceryListCtl } from "../controllers";
|
||||
const groceryinstance = new GroceryListCtl();
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -7,18 +8,50 @@ export const groceryListRoute = (app: Express) => {
|
||||
app.use('/grocery-list', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
const userid = req.query.userid as string;
|
||||
|
||||
try {
|
||||
if (userid) {
|
||||
const result = await groceryinstance.getByUserID(userid);
|
||||
res.status(200).send(result);
|
||||
} else {
|
||||
const result = await groceryinstance.getAll();
|
||||
res.status(200).send(result);
|
||||
}
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
|
||||
const { id } = req.params;
|
||||
try {
|
||||
const result = await groceryinstance.getOne(id);
|
||||
res.status(200).send(result);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/', async (req, res, next) => {
|
||||
|
||||
const data = req.body;
|
||||
try {
|
||||
const result = await groceryinstance.post(data);
|
||||
res.status(201).send(result);
|
||||
} 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(200).send(result);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { userRoute } from "./users";
|
||||
import { recipeRoute } from "./recipe";
|
||||
import { collectionRoute } from "./collection";
|
||||
import { ingredientRoute } from "./ingredient";
|
||||
import { groceryListRoute } from "./groceryList";
|
||||
|
||||
export const routes = (app: Express, passport?: any) => {
|
||||
console.log('routes called');
|
||||
@@ -11,6 +12,7 @@ export const routes = (app: Express, passport?: any) => {
|
||||
recipeRoute(app);
|
||||
collectionRoute(app);
|
||||
ingredientRoute(app);
|
||||
groceryListRoute(app);
|
||||
|
||||
app.get('/hello', (req, res) => {
|
||||
res.send({ message: "hello from the server!!" });
|
||||
|
||||
Reference in New Issue
Block a user