From 2583163fbb0d5f9ffb83fc91ab98c45de957910a Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Sat, 19 Nov 2022 13:31:23 -0600 Subject: [PATCH] ingredient route --- server/controllers/IngredientCtl.ts | 33 +++++++++++++++++--- server/controllers/index.ts | 9 ++++++ server/models/ingredient.ts | 48 ++++++++++++++++++++++++++--- server/routes/groceryList.ts | 3 +- server/routes/index.ts | 2 ++ server/routes/ingredient.ts | 34 ++++++++++++++++++-- 6 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 server/controllers/index.ts diff --git a/server/controllers/IngredientCtl.ts b/server/controllers/IngredientCtl.ts index 21b16eb..0dca755 100644 --- a/server/controllers/IngredientCtl.ts +++ b/server/controllers/IngredientCtl.ts @@ -1,21 +1,46 @@ +import createError from "http-errors"; import { IIngredient } from "../schemas"; import { Ingredient } from "../models/ingredient"; const IngredientInstance = new Ingredient(); export default class IngredientCtl { async getAll() { - + try { + const result = await IngredientInstance.getAll(); + if (!result) throw createError('404', 'No ingredients found'); + return result; + } catch (e: any) { + throw new Error(e); + } } async getOne(id: string) { - + try { + const result = await IngredientInstance.getOne(id); + if (!result) throw createError('404', 'No ingredient found with id ' + id); + return result; + } catch (e: any) { + throw new Error(e); + } } async post(data: IIngredient) { - + try { + const result = await IngredientInstance.post(data); + if (!result) throw createError('400', 'Bad request'); + return result; + } catch (e: any) { + throw new Error(e); + } } async put(id: string, data: IIngredient) { - + try { + const result = await IngredientInstance.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/controllers/index.ts b/server/controllers/index.ts new file mode 100644 index 0000000..02cdef4 --- /dev/null +++ b/server/controllers/index.ts @@ -0,0 +1,9 @@ +import CollectionCtl from "./CollectionCtl"; +import GroceryListCtl from "./GroceryListCtl"; +import IngredientCtl from "./IngredientCtl"; +import RecipeCtl from "./RecipeCtl"; +import UserCtl from "./UserCtl"; + +export { + CollectionCtl, GroceryListCtl, IngredientCtl, RecipeCtl, UserCtl +} \ No newline at end of file diff --git a/server/models/ingredient.ts b/server/models/ingredient.ts index 737e8b0..5dfcf24 100644 --- a/server/models/ingredient.ts +++ b/server/models/ingredient.ts @@ -3,18 +3,58 @@ import pool from "../db"; export class Ingredient { async getAll() { - + try { + const statement = `SELECT * FROM recipin.ingredient`; + 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.ingredient 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 post(data: IIngredient) { - + try { + const statement = ` + INSERT INTO recipin.ingredient + (name, description) + VALUES ($1, $2) RETURNING *`; + const values = [data.name, data.description]; + 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: IIngredient) { - + try { + const statement = ` + UPDATE recipin.ingredient + SET name = $1, + description = $2 + WHERE id = $3 + RETURNING * + ` + + const values = [data.name, data.description, 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 1ba4bf3..2718ae1 100644 --- a/server/routes/groceryList.ts +++ b/server/routes/groceryList.ts @@ -1,4 +1,5 @@ import { Express, Router } from "express"; +import { GroceryListCtl } from "../controllers"; const router = Router(); @@ -18,6 +19,6 @@ export const groceryListRoute = (app: Express) => { }) router.put('/:id', async (req, res, next) => { - + }) } \ No newline at end of file diff --git a/server/routes/index.ts b/server/routes/index.ts index 584a031..770a932 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -2,6 +2,7 @@ import { Express } from "express" import { userRoute } from "./users"; import { recipeRoute } from "./recipe"; import { collectionRoute } from "./collection"; +import { ingredientRoute } from "./ingredient"; export const routes = (app: Express, passport?: any) => { console.log('routes called'); @@ -9,6 +10,7 @@ export const routes = (app: Express, passport?: any) => { userRoute(app); recipeRoute(app); collectionRoute(app); + ingredientRoute(app); app.get('/hello', (req, res) => { res.send({ message: "hello from the server!!" }); diff --git a/server/routes/ingredient.ts b/server/routes/ingredient.ts index 03942d5..a91e340 100644 --- a/server/routes/ingredient.ts +++ b/server/routes/ingredient.ts @@ -1,22 +1,52 @@ import { Express, Router } from "express"; +import { IngredientCtl } from "../controllers"; +const IngredientInstance = new IngredientCtl(); + const router = Router(); export const ingredientRoute = (app: Express) => { app.use('/ingredient', router); router.get('/', async (req, res, next) => { - + try { + const result = await IngredientInstance.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 IngredientInstance.getOne(id); + res.status(200).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 IngredientInstance.put(id, data); + res.status(200).send(result); + } catch(e) { + next(e); + } }) router.post('/', async (req, res, next) => { - + const data = req.body; + + try { + const result = await IngredientInstance.post(data); + res.status(201).send(result); + } catch(e) { + next(e); + } }) } \ No newline at end of file