From 45f3584af0c1d0fdf916f72e9af07c9ae7739f4a Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Sat, 19 Nov 2022 13:56:35 -0600 Subject: [PATCH] grocery list route --- server/controllers/GroceryListCtl.ts | 41 +++++++++++++++++-- server/models/groceryList.ts | 59 +++++++++++++++++++++++++++- server/routes/groceryList.ts | 37 ++++++++++++++++- server/routes/index.ts | 2 + server/schemas/index.ts | 2 +- 5 files changed, 133 insertions(+), 8 deletions(-) diff --git a/server/controllers/GroceryListCtl.ts b/server/controllers/GroceryListCtl.ts index 2f788de..6008a98 100644 --- a/server/controllers/GroceryListCtl.ts +++ b/server/controllers/GroceryListCtl.ts @@ -1,21 +1,56 @@ +import createError from "http-errors"; import { IGroceryList } from "../schemas"; import { GroceryList } from "../models/groceryList"; const GroceryInstance = new GroceryList(); export default class GroceryListCtl { async getAll() { - + try { + const result = await GroceryInstance.getAll(); + if (!result) throw createError('404', 'No results found'); + return result; + } catch (e: any) { + throw new Error(e); + } } async getOne(id: string) { + try { + const result = await GroceryInstance.getOne(id); + if (!result) throw createError('404', 'No results found'); + return result; + } catch (e: any) { + throw new Error(e); + } + } + async getByUserID(userid: string) { + try { + const result = await GroceryInstance.getByUserID(userid); + if (!result) throw createError('404', 'No results found'); + return result; + } catch (e: any) { + throw new Error(e); + } } async post(data: IGroceryList) { - + try { + const result = await GroceryInstance.post(data); + if (!result) throw createError('400', 'Bad request'); + return result; + } catch (e: any) { + throw new Error(e); + } } async put(id: string, data: IGroceryList) { - + try { + const result = await GroceryInstance.put(id, data); + if (!result) throw createError('400', 'Bad request'); + return result; + } catch (e: any) { + throw new Error(e); + } } } \ No newline at end of file diff --git a/server/models/groceryList.ts b/server/models/groceryList.ts index 99b1777..55b63de 100644 --- a/server/models/groceryList.ts +++ b/server/models/groceryList.ts @@ -3,18 +3,73 @@ import pool from "../db"; export class GroceryList { async getAll() { - + try { + const statement = 'SELECT * FROM recipin.grocerylist'; + const result = await pool.query(statement); + if (result.rows.length) return result.rows; + return null; + } catch (e: any) { + throw new Error(e); + } } async getOne(id: string) { + try { + const statement = `SELECT * FROM recipin.grocerylist WHERE id = $1` + const result = await pool.query(statement, [id]); + if (result.rows.length) return result.rows[0]; + return null; + } catch (e: any) { + throw new Error(e); + } + } + async getByUserID(userid: string) { + try { + const statement = `SELECT * FROM recipin.grocerylist WHERE ownerid = $1` + const result = await pool.query(statement, [userid]); + if (result.rows.length) return result.rows; + return null; + } catch (e: any) { + throw new Error(e); + } } async post(data: IGroceryList) { - + try { + // assumes that any new list will be active on creation + const statement = ` + INSERT INTO recipin.grocerylist + (name, active, ownerid) + VALUES ($1, true, $2) + RETURNING *; + ` + const values = [data.name, data.ownerid]; + const result = await pool.query(statement, values); + if (result.rows.length) return result.rows[0]; + return null; + } catch (e: any) { + throw new Error(e); + } } async put(id: string, data: IGroceryList) { + try { + const statement = ` + UPDATE recipin.grocerylist + SET name = $1, + active = $2, + ownerid = $3 + WHERE id = $4 + RETURNING * + ` + const values = [data.name, data.active, data.ownerid, id]; + const result = await pool.query(statement, values); + if (result.rows.length) return result.rows[0]; + return null; + } catch (e: any) { + throw new Error(e); + } } } \ No newline at end of file diff --git a/server/routes/groceryList.ts b/server/routes/groceryList.ts index 2718ae1..12257fd 100644 --- a/server/routes/groceryList.ts +++ b/server/routes/groceryList.ts @@ -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); + } }) } \ No newline at end of file diff --git a/server/routes/index.ts b/server/routes/index.ts index 770a932..7d8bb3f 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -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!!" }); diff --git a/server/schemas/index.ts b/server/schemas/index.ts index 7809fb8..e5d55cf 100644 --- a/server/schemas/index.ts +++ b/server/schemas/index.ts @@ -33,7 +33,7 @@ export interface ICollection { export interface IGroceryList { id: number - listname: string + name: string recipes?: IRecipe["id"][] active: boolean ownerid: IUser["id"]