grocery list route

This commit is contained in:
Mikayla Dobson
2022-11-19 13:56:35 -06:00
parent 2583163fbb
commit 45f3584af0
5 changed files with 133 additions and 8 deletions

View File

@@ -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);
}
})
}

View File

@@ -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!!" });