From 5234e54bcc0e64adf9b9259b44da36718285c30f Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Mon, 12 Dec 2022 20:32:22 -0600 Subject: [PATCH] few more routes added. process of refactoring controllers still underway --- server/controllers/IngredientCtl.ts | 18 +++++++++--------- server/jest/tests/routes/recipe.test.ts | 23 +++++++++++++++++++++++ server/routes/ingredient.ts | 18 ++++++++++-------- server/routes/recipe.ts | 16 +++++++++------- server/routes/users.ts | 4 ++-- 5 files changed, 53 insertions(+), 26 deletions(-) diff --git a/server/controllers/IngredientCtl.ts b/server/controllers/IngredientCtl.ts index 0dca755..45eb32c 100644 --- a/server/controllers/IngredientCtl.ts +++ b/server/controllers/IngredientCtl.ts @@ -1,14 +1,14 @@ -import createError from "http-errors"; import { IIngredient } from "../schemas"; import { Ingredient } from "../models/ingredient"; +import ControllerResponse from "../util/ControllerResponse"; 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; + const ok = result !== null; + return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipes found")); } catch (e: any) { throw new Error(e); } @@ -17,8 +17,8 @@ export default class IngredientCtl { async getOne(id: string) { try { const result = await IngredientInstance.getOne(id); - if (!result) throw createError('404', 'No ingredient found with id ' + id); - return result; + const ok = result !== null; + return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipe found with this ID")); } catch (e: any) { throw new Error(e); } @@ -27,8 +27,8 @@ export default class IngredientCtl { async post(data: IIngredient) { try { const result = await IngredientInstance.post(data); - if (!result) throw createError('400', 'Bad request'); - return result; + const ok = result !== null; + return new ControllerResponse(ok, (ok ? 201 : 400), (ok ? result : "Something went wrong")); } catch (e: any) { throw new Error(e); } @@ -37,8 +37,8 @@ export default class IngredientCtl { async put(id: string, data: IIngredient) { try { const result = await IngredientInstance.put(id, data); - if (!result) throw createError('400', 'Bad request'); - return result; + const ok = result !== null; + return new ControllerResponse(ok, (ok ? 200 : 400), (ok ? result : "Something went wrong")); } catch (e: any) { throw new Error(e); } diff --git a/server/jest/tests/routes/recipe.test.ts b/server/jest/tests/routes/recipe.test.ts index e69de29..d221eda 100644 --- a/server/jest/tests/routes/recipe.test.ts +++ b/server/jest/tests/routes/recipe.test.ts @@ -0,0 +1,23 @@ +import request from 'supertest' +import { IRecipe } from '../../../schemas' + +const server = request.agent('localhost:8080'); + +describe('/recipe', () => { + beforeAll(async () => { + // to do: create session user, + // use it to log in on this test, + // use the authenticated session to view recipes + + // await server.post('/auth/login') + // .body() + }) + + describe('GET /', () => { + test('gets an array of recipes', async () => { + const result = await request('localhost:8080').get('/recipe'); + const data = JSON.parse(result.text); + expect(data.length).toBeGreaterThan(0); + }) + }) +}) \ No newline at end of file diff --git a/server/routes/ingredient.ts b/server/routes/ingredient.ts index a91e340..220cb1a 100644 --- a/server/routes/ingredient.ts +++ b/server/routes/ingredient.ts @@ -1,5 +1,7 @@ import { Express, Router } from "express"; import { IngredientCtl } from "../controllers"; +import { IIngredient } from "../schemas"; +import { CtlResponse } from "../util/types"; const IngredientInstance = new IngredientCtl(); const router = Router(); @@ -9,8 +11,8 @@ export const ingredientRoute = (app: Express) => { router.get('/', async (req, res, next) => { try { - const result = await IngredientInstance.getAll(); - res.status(200).send(result); + const result: CtlResponse = await IngredientInstance.getAll(); + res.status(result.code).send(result.data); } catch(e) { next(e); } @@ -20,8 +22,8 @@ export const ingredientRoute = (app: Express) => { const { id } = req.params; try { - const result = await IngredientInstance.getOne(id); - res.status(200).send(result); + const result: CtlResponse = await IngredientInstance.getOne(id); + res.status(result.code).send(result.data); } catch(e) { next(e); } @@ -32,8 +34,8 @@ export const ingredientRoute = (app: Express) => { const data = req.body; try { - const result = await IngredientInstance.put(id, data); - res.status(200).send(result); + const result: CtlResponse = await IngredientInstance.put(id, data); + res.status(result.code).send(result.data); } catch(e) { next(e); } @@ -43,8 +45,8 @@ export const ingredientRoute = (app: Express) => { const data = req.body; try { - const result = await IngredientInstance.post(data); - res.status(201).send(result); + const result: CtlResponse = await IngredientInstance.post(data); + res.status(result.code).send(result.data); } catch(e) { next(e); } diff --git a/server/routes/recipe.ts b/server/routes/recipe.ts index 96df63f..2db554f 100644 --- a/server/routes/recipe.ts +++ b/server/routes/recipe.ts @@ -1,6 +1,8 @@ import { Express, Router } from "express" import { restrictAccess } from "../auth/middlewares"; import RecipeCtl from "../controllers/RecipeCtl"; +import { IRecipe } from "../schemas"; +import { CtlResponse } from "../util/types"; const recipectl = new RecipeCtl(); const router = Router(); @@ -24,17 +26,17 @@ export const recipeRoute = (app: Express) => { const { filterby } = req.query; try { - let result; + let result: CtlResponse; switch (filterby) { case "allaccessible": - result = await recipectl.getAllAccessible(user.id); - break; + // result = await recipectl.getAllAccessible(user.id); + // break; default: result = await recipectl.getAllAuthored(user.id); break; } - res.status(200).send(result); + res.status(result.code).send(result.data); } catch(e) { next(e); } @@ -45,8 +47,8 @@ export const recipeRoute = (app: Express) => { const { id } = req.params; try { - const result = await recipectl.updateOne(id, data); - res.status(200).send(result); + const result: CtlResponse = await recipectl.updateOne(id, data); + res.status(result.code).send(result.data); } catch(e) { next(e); } @@ -58,7 +60,7 @@ export const recipeRoute = (app: Express) => { try { const result = await recipectl.post(user.id, data); - res.status(201).send(result); + res.status(result.code).send(result.data); } catch(e) { next(e); } diff --git a/server/routes/users.ts b/server/routes/users.ts index 49477da..878f6e5 100644 --- a/server/routes/users.ts +++ b/server/routes/users.ts @@ -50,8 +50,8 @@ export const userRoute = (app: Express) => { // create user router.post('/', async (req, res) => { const data = req.body; - const response = userCtl.post(data); - res.status(200).send(response); + const response = await userCtl.post(data); + res.status(response.code).send(response.data); }) // get friendships by requester ID