From 174866e254cffb94bac37de124401a8a90d2d778 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Thu, 8 Dec 2022 14:06:08 -0600 Subject: [PATCH] built cuisine route --- server/controllers/CuisineCtl.ts | 36 ++++++++++++++++++++++++++ server/controllers/index.ts | 3 ++- server/models/cuisine.ts | 44 ++++++++++++++++++++++++++++++++ server/routes/cuisine.ts | 40 +++++++++++++++++++++++++++++ server/routes/index.ts | 2 ++ 5 files changed, 124 insertions(+), 1 deletion(-) diff --git a/server/controllers/CuisineCtl.ts b/server/controllers/CuisineCtl.ts index e69de29..6f855af 100644 --- a/server/controllers/CuisineCtl.ts +++ b/server/controllers/CuisineCtl.ts @@ -0,0 +1,36 @@ +import createError from "http-errors"; +import { ICuisine } from "../schemas"; +import Cuisine from "../models/cuisine"; +const CuisineInstance = new Cuisine(); + +export default class CuisineCtl { + async getAll() { + try { + const result = await CuisineInstance.getAll(); + if (!result) throw createError('404', 'No cuisines found'); + return result; + } catch (e: any) { + throw new Error(e); + } + } + + async getOne(id: string) { + try { + const result = await CuisineInstance.getOne(id); + if (!result) throw createError('404', 'No cuisine found with id ' + id); + return result; + } catch (e: any) { + throw new Error(e); + } + } + + async post(data: ICuisine) { + try { + const result = await CuisineInstance.post(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 index 02cdef4..19c23c0 100644 --- a/server/controllers/index.ts +++ b/server/controllers/index.ts @@ -1,9 +1,10 @@ import CollectionCtl from "./CollectionCtl"; +import CuisineCtl from "./CuisineCtl"; import GroceryListCtl from "./GroceryListCtl"; import IngredientCtl from "./IngredientCtl"; import RecipeCtl from "./RecipeCtl"; import UserCtl from "./UserCtl"; export { - CollectionCtl, GroceryListCtl, IngredientCtl, RecipeCtl, UserCtl + CollectionCtl, CuisineCtl, GroceryListCtl, IngredientCtl, RecipeCtl, UserCtl } \ No newline at end of file diff --git a/server/models/cuisine.ts b/server/models/cuisine.ts index e69de29..77d17b9 100644 --- a/server/models/cuisine.ts +++ b/server/models/cuisine.ts @@ -0,0 +1,44 @@ +import pool from "../db"; +import now from "../util/now"; +import { ICuisine } from "../schemas"; + +export default class Cuisine { + async getOne(id: string) { + try { + const statement = `SELECT * FROM recipin.cuisine WHERE id = $1`; + const values = [id]; + const result = await pool.query(statement, values); + if (result.rows) return result.rows[0]; + return null; + } catch (error: any) { + throw new Error(error); + } + } + + async getAll() { + try { + const statement = `SELECT * FROM recipin.cuisine`; + const result = await pool.query(statement); + if (result.rows.length) return result.rows; + return null; + } catch (e: any) { + throw new Error(e); + } + } + + async post(data: ICuisine) { + try { + const { name } = data; + const statement = ` + INSERT INTO recipin.cuisine + (name, datecreated, datemodified, active) + VALUES ($1, $2, $3, $4) RETURNING *`; + const values = [name, now, now, true]; + 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/cuisine.ts b/server/routes/cuisine.ts index e69de29..1124e51 100644 --- a/server/routes/cuisine.ts +++ b/server/routes/cuisine.ts @@ -0,0 +1,40 @@ +import { Express, Router } from 'express'; +import { CuisineCtl } from '../controllers'; +const CuisineInstance = new CuisineCtl(); + +const router = Router(); + +export const cuisineRouter = (app: Express) => { + app.use('/cuisine', router); + + router.get('/', async (req, res, next) => { + try { + const result = await CuisineInstance.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 CuisineInstance.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 CuisineInstance.post(data); + res.status(201).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 1452a90..301ac85 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -8,6 +8,7 @@ import { groceryListRoute } from "./groceryList"; import { authRoute } from "./auth"; import { subscriptionRoute } from "./subscription"; import { friendRouter } from "./friend"; +import { cuisineRouter } from "./cuisine"; export const routes = async (app: Express, passport: PassportStatic) => { console.log('routes called'); @@ -15,6 +16,7 @@ export const routes = async (app: Express, passport: PassportStatic) => { authRoute(app, passport); userRoute(app); friendRouter(app); + cuisineRouter(app); recipeRoute(app); collectionRoute(app); subscriptionRoute(app);