From bf16841b955cadbcc7e8a007df2ead463f979a6d Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Sat, 19 Nov 2022 12:02:28 -0600 Subject: [PATCH] support on collection route --- server/controllers/CollectionCtl.ts | 21 +++++++++++++++++++++ server/models/collection.ts | 15 ++++++++++++--- server/routes/collection.ts | 26 ++++++++++++++++++++++++-- server/routes/index.ts | 2 ++ server/routes/recipe.ts | 1 - 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/server/controllers/CollectionCtl.ts b/server/controllers/CollectionCtl.ts index 82836f5..bc12576 100644 --- a/server/controllers/CollectionCtl.ts +++ b/server/controllers/CollectionCtl.ts @@ -1,3 +1,24 @@ +import createError from "http-errors"; +import { ICollection } from "../schemas"; +import { Collection } from "../models/collection"; +const CollectionInstance = new Collection(); + export default class CollectionCtl { + async getOne(id: string) { + const result = await CollectionInstance.getOne(id); + if (!result) throw createError('404', 'Collection not found'); + return result; + } + + async getAll() { + const result = await CollectionInstance.getAll(); + if (!result) throw createError('404', 'No collections found'); + return result; + } + async post(data: ICollection) { + const result = await CollectionInstance.post(data); + if (!result) throw createError('400', 'Bad request'); + return result; + } } \ No newline at end of file diff --git a/server/models/collection.ts b/server/models/collection.ts index de932d0..c485042 100644 --- a/server/models/collection.ts +++ b/server/models/collection.ts @@ -7,7 +7,8 @@ export class Collection { const statement = `SELECT * FROM recipin.collection WHERE id = $1`; const values = [id]; const result = await pool.query(statement, values); - return result; + if (result.rows.length) return result.rows[0]; + return null; } catch (e: any) { throw new Error(e); } @@ -18,18 +19,26 @@ export class Collection { try { const statement = `SELECT * FROM recipin.collection`; const result = await pool.query(statement); - return result; + if (result.rows.length) return result.rows; + return null; } catch (e: any) { throw new Error(e); } } async post(data: ICollection) { + const { name, active, ismaincollection, ownerid } = data; try { const statement = ` INSERT INTO recipin.collection - () + (name, active, ismaincollection, ownerid) + VALUES ($1, $2, $3, $4) + RETURNING *; ` + const values = [name, active, ismaincollection, ownerid]; + const result = await pool.query(statement, values); + if (result.rows.length) return result.rows; + return null; } catch (e: any) { throw new Error(e); } diff --git a/server/routes/collection.ts b/server/routes/collection.ts index 70aca1b..2ac45fd 100644 --- a/server/routes/collection.ts +++ b/server/routes/collection.ts @@ -1,14 +1,36 @@ import { Express, Router } from "express"; +import CollectionCtl from "../controllers/CollectionCtl"; +const CollectionInstance = new CollectionCtl(); const router = Router(); -const collectionRoute = (app: Express) => { +export const collectionRoute = (app: Express) => { app.use('/collection', router); router.get('/:id', async (req, res, next) => { const { id } = req.params; try { - + const result = await CollectionInstance.getOne(id); + res.status(200).send(result); + } catch(e) { + next(e); + } + }) + + router.get('/', async (req, res, next) => { + try { + const result = await CollectionInstance.getAll(); + res.status(200).send(result); + } catch(e) { + next(e); + } + }) + + router.post('/', async (req, res, next) => { + const data = req.body; + try { + const result = await CollectionInstance.post(data); + res.status(201).send(result); } catch(e) { next(e); } diff --git a/server/routes/index.ts b/server/routes/index.ts index 0486924..584a031 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -1,12 +1,14 @@ import { Express } from "express" import { userRoute } from "./users"; import { recipeRoute } from "./recipe"; +import { collectionRoute } from "./collection"; export const routes = (app: Express, passport?: any) => { console.log('routes called'); userRoute(app); recipeRoute(app); + collectionRoute(app); app.get('/hello', (req, res) => { res.send({ message: "hello from the server!!" }); diff --git a/server/routes/recipe.ts b/server/routes/recipe.ts index 24ad5ca..9ec4274 100644 --- a/server/routes/recipe.ts +++ b/server/routes/recipe.ts @@ -9,7 +9,6 @@ export const recipeRoute = (app: Express) => { router.get('/:id', async (req, res, next) => { const { id } = req.params; - console.log('/recipe/' + id + ' called'); try { const result = await recipectl.getOne(id);