diff --git a/server/controllers/CollectionCtl.ts b/server/controllers/CollectionCtl.ts index 4f6f6e0..10bf36a 100644 --- a/server/controllers/CollectionCtl.ts +++ b/server/controllers/CollectionCtl.ts @@ -1,46 +1,50 @@ import createError from "http-errors"; import { ICollection } from "../schemas"; import { Collection } from "../models/collection"; +import { StatusCode } from "../util/types"; +import ControllerResponse from "../util/ControllerResponse"; const CollectionInstance = new Collection(); export default class CollectionCtl { async getOne(id: number | string) { const result = await CollectionInstance.getOne(id); - if (!result) throw createError('404', 'Collection not found'); - return result; + + const ok: boolean = result !== null; + const code: StatusCode = ok ? StatusCode.OK : StatusCode.NotFound; + const data: string | ICollection = result || "No collection found with this ID"; + return new ControllerResponse(code, data); } async getAll() { const result = await CollectionInstance.getAll(); - if (!result) throw createError('404', 'No collections found'); - return result; + const ok = result !== null; + const code = ok ? StatusCode.OK : StatusCode.NotFound; + const data = result || "No collections found" + return new ControllerResponse(code, data); } async getUserDefault(id: number | string) { const result = await CollectionInstance.getUserDefault(id); - if (!result) throw createError('404', "No default collection found. Contact an admin, this isn't supposed to happen"); - return result; + const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound; + const data = result || "No default collection found" + return new ControllerResponse(code, data); } - async post(data: ICollection) { - const result = await CollectionInstance.post(data); - if (!result) throw createError('400', 'Bad request'); - return result; + async post(body: ICollection) { + const result = await CollectionInstance.post(body); + const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest; + const data = result || "Something went wrong" + return new ControllerResponse(code, data); } async getSubscriptions(userid: number | string) { const result = await CollectionInstance.getSubscriptions(userid); - if (!result) throw createError('404', 'No subscriptions found'); - return result; + const code = (result !== null) ? StatusCode.OK : StatusCode.NoContent; + return new ControllerResponse(code, (result || "No subscriptions found")); } async postSubscription(collectionid: number | string, userid: number | string) { - const { ok, code, data } = await CollectionInstance.postSubscription(collectionid, userid); - - if (ok) { - return data; - } else { - throw createError(code, data as string); - } + const { code, data } = await CollectionInstance.postSubscription(collectionid, userid); + return new ControllerResponse(code, data); } } \ No newline at end of file diff --git a/server/controllers/CourseCtl.ts b/server/controllers/CourseCtl.ts index 3bcc99b..a44beff 100644 --- a/server/controllers/CourseCtl.ts +++ b/server/controllers/CourseCtl.ts @@ -1,14 +1,16 @@ import createError from 'http-errors'; import { ICourse } from '../schemas'; import Course from '../models/course'; +import ControllerResponse from '../util/ControllerResponse'; +import { StatusCode } from '../util/types'; const CourseInstance = new Course(); export default class CourseCtl { async getAll() { try { const result = await CourseInstance.getAll(); - if (!result) throw createError('404', 'No cuisines found'); - return result; + const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No course data found")); } catch (e: any) { throw new Error(e); } @@ -17,8 +19,8 @@ export default class CourseCtl { async getOne(id: string) { try { const result = await CourseInstance.getOne(id); - if (!result) throw createError('404', 'No cuisine found with id ' + id); - return result; + const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No course found with this ID")) } catch (e: any) { throw new Error(e); } @@ -27,8 +29,8 @@ export default class CourseCtl { async post(data: ICourse) { try { const result = await CourseInstance.post(data); - if (!result) throw createError('400', 'Bad request'); - return result; + const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest; + return new ControllerResponse(code, (result || "Something went wrong")); } catch (e: any) { throw new Error(e); } diff --git a/server/controllers/GroceryListCtl.ts b/server/controllers/GroceryListCtl.ts index 6008a98..6a17184 100644 --- a/server/controllers/GroceryListCtl.ts +++ b/server/controllers/GroceryListCtl.ts @@ -1,14 +1,16 @@ import createError from "http-errors"; import { IGroceryList } from "../schemas"; import { GroceryList } from "../models/groceryList"; +import { StatusCode } from "../util/types"; +import ControllerResponse from "../util/ControllerResponse"; 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; + const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No grocery list data found")); } catch (e: any) { throw new Error(e); } @@ -17,8 +19,8 @@ export default class GroceryListCtl { async getOne(id: string) { try { const result = await GroceryInstance.getOne(id); - if (!result) throw createError('404', 'No results found'); - return result; + const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No grocery list data found with this ID")); } catch (e: any) { throw new Error(e); } @@ -27,8 +29,8 @@ export default class GroceryListCtl { async getByUserID(userid: string) { try { const result = await GroceryInstance.getByUserID(userid); - if (!result) throw createError('404', 'No results found'); - return result; + const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No grocery list data found matching this user ID")); } catch (e: any) { throw new Error(e); } @@ -37,8 +39,8 @@ export default class GroceryListCtl { async post(data: IGroceryList) { try { const result = await GroceryInstance.post(data); - if (!result) throw createError('400', 'Bad request'); - return result; + const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest; + return new ControllerResponse(code, (result || "Something went wrong")); } catch (e: any) { throw new Error(e); } @@ -47,8 +49,8 @@ export default class GroceryListCtl { async put(id: string, data: IGroceryList) { try { const result = await GroceryInstance.put(id, data); - if (!result) throw createError('400', 'Bad request'); - return result; + const code = (result !== null) ? StatusCode.OK : StatusCode.BadRequest; + return new ControllerResponse(code, (result || "Something went wrong")); } catch (e: any) { throw new Error(e); } diff --git a/server/controllers/IngredientCtl.ts b/server/controllers/IngredientCtl.ts index 45eb32c..04ae1d6 100644 --- a/server/controllers/IngredientCtl.ts +++ b/server/controllers/IngredientCtl.ts @@ -1,6 +1,7 @@ import { IIngredient } from "../schemas"; import { Ingredient } from "../models/ingredient"; import ControllerResponse from "../util/ControllerResponse"; +import { StatusCode } from "../util/types"; const IngredientInstance = new Ingredient(); export default class IngredientCtl { @@ -8,7 +9,8 @@ export default class IngredientCtl { try { const result = await IngredientInstance.getAll(); const ok = result !== null; - return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipes found")); + const code = ok ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No ingredients found")); } catch (e: any) { throw new Error(e); } @@ -18,7 +20,8 @@ export default class IngredientCtl { try { const result = await IngredientInstance.getOne(id); const ok = result !== null; - return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipe found with this ID")); + const code = ok ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No ingredient found with this ID")); } catch (e: any) { throw new Error(e); } @@ -28,7 +31,8 @@ export default class IngredientCtl { try { const result = await IngredientInstance.post(data); const ok = result !== null; - return new ControllerResponse(ok, (ok ? 201 : 400), (ok ? result : "Something went wrong")); + const code = ok ? StatusCode.NewContent : StatusCode.BadRequest; + return new ControllerResponse(code, (result || "Something went wrong")); } catch (e: any) { throw new Error(e); } @@ -38,7 +42,8 @@ export default class IngredientCtl { try { const result = await IngredientInstance.put(id, data); const ok = result !== null; - return new ControllerResponse(ok, (ok ? 200 : 400), (ok ? result : "Something went wrong")); + const code = ok ? StatusCode.OK : StatusCode.BadRequest; + return new ControllerResponse(code, (result || "Something went wrong")); } catch (e: any) { throw new Error(e); } diff --git a/server/controllers/RecipeCtl.ts b/server/controllers/RecipeCtl.ts index 53315ba..8993989 100644 --- a/server/controllers/RecipeCtl.ts +++ b/server/controllers/RecipeCtl.ts @@ -1,6 +1,7 @@ import { IRecipe } from "../schemas"; import { Recipe } from "../models/recipe"; import ControllerResponse from "../util/ControllerResponse"; +import { StatusCode } from "../util/types"; const RecipeInstance = new Recipe(); export default class RecipeCtl { @@ -8,7 +9,8 @@ export default class RecipeCtl { try { const result = await RecipeInstance.getOneByID(id); const ok = result !== null; - return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipe found with this ID")); + const code = ok ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No recipe found with this ID")); } catch (error: any) { throw new Error(error); } @@ -18,7 +20,8 @@ export default class RecipeCtl { try { const result = await RecipeInstance.getAllAuthored(id); const ok = result !== null; - return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipes found authored by user with this ID")); + const code = ok ? StatusCode.OK : StatusCode.NotFound; + return new ControllerResponse(code, (result || "No recipes found for this user")); } catch (e: any) { throw new Error(e); } @@ -36,7 +39,8 @@ export default class RecipeCtl { try { const result = await RecipeInstance.updateOneByID(id, data); const ok = result !== null; - return new ControllerResponse(ok, (ok ? 200 : 400), (ok ? result : "Something went wrong")); + const code = ok ? StatusCode.OK : StatusCode.BadRequest; + return new ControllerResponse(code, (result || "Something went wrong")); } catch (error: any) { throw new Error(error); } @@ -46,7 +50,8 @@ export default class RecipeCtl { try { const result = await RecipeInstance.post(userid, data); const ok = result !== null; - return new ControllerResponse(ok, (ok ? 201 : 400), (ok ? result : "Something went wrong")); + const code = ok ? StatusCode.NewContent : StatusCode.BadRequest; + return new ControllerResponse(code, (result || "Something went wrong")); } catch (error: any) { throw new Error(error); } diff --git a/server/controllers/UserCtl.ts b/server/controllers/UserCtl.ts index bbe750a..3cd75b3 100644 --- a/server/controllers/UserCtl.ts +++ b/server/controllers/UserCtl.ts @@ -16,7 +16,7 @@ export default class UserCtl { const data: IUser[] | string = ok ? users! : "No users found."; // send formatted response with either data or informative error message - return new ControllerResponse(ok, code, data) + return new ControllerResponse(code, data, ok) } catch (error: any) { throw new Error(error); } @@ -28,7 +28,7 @@ export default class UserCtl { const ok = response !== null; const code = ok ? StatusCode.NewContent : StatusCode.BadRequest const data = ok ? response : "Bad request" - return new ControllerResponse(ok, code, data); + return new ControllerResponse(code, data); } catch (error: any) { throw new Error(error); } @@ -40,7 +40,7 @@ export default class UserCtl { const ok = user !== null; const code = ok ? StatusCode.OK : StatusCode.NotFound; const data = ok ? user : "User by this ID not found"; - return new ControllerResponse(ok, code, data); + return new ControllerResponse(code, data); } catch (error: any) { throw new Error(error); } @@ -52,7 +52,7 @@ export default class UserCtl { const ok = result !== null; const code = ok ? StatusCode.OK : StatusCode.BadRequest; const data = ok ? result : "Update unsuccessful" - return new ControllerResponse(ok, code, data); + return new ControllerResponse(code, data); } catch (error: any) { throw new Error(error); } @@ -64,7 +64,7 @@ export default class UserCtl { const ok = result !== null const code = ok ? StatusCode.OK : StatusCode.NotFound; const data = ok ? result : "No friends found" - return new ControllerResponse(ok, code, data); + return new ControllerResponse(code, data); } catch (e: any) { throw new Error(e); } @@ -73,7 +73,7 @@ export default class UserCtl { async getFriendshipByID(id: number | string, userid: number | string) { try { const { ok, code, result } = await UserInstance.getFriendshipByID(id, userid); - return new ControllerResponse(ok, code, result); + return new ControllerResponse(code, result); } catch (e: any) { throw new Error(e); } @@ -82,7 +82,7 @@ export default class UserCtl { async getPendingFriendRequests(senderid: string | number) { try { const { ok, code, result } = await UserInstance.getPendingFriendRequests(senderid); - return new ControllerResponse(ok, code, result); + return new ControllerResponse(code, result); } catch (e: any) { throw new Error(e); } @@ -94,7 +94,7 @@ export default class UserCtl { const ok = result !== null; const code = ok ? StatusCode.NewContent : StatusCode.BadRequest; const data = ok ? result : "Something went wrong" - return new ControllerResponse(ok, code, data); + return new ControllerResponse(code, data); } catch (e: any) { throw new Error(e); } @@ -103,7 +103,7 @@ export default class UserCtl { async updateFriendship(id: number | string, userid: number | string, data: { active: boolean, pending: boolean, dateterminated?: string }) { try { const { ok, code, result } = await UserInstance.updateFriendship(id, userid, data); - return new ControllerResponse(ok, code, result); + return new ControllerResponse(code, result); } catch (e: any) { throw new Error(e); } diff --git a/server/models/collection.ts b/server/models/collection.ts index d1b9e86..b15f4f1 100644 --- a/server/models/collection.ts +++ b/server/models/collection.ts @@ -67,7 +67,6 @@ export class Collection { async getSubscriptions(userid: number | string) { try { const sql = fs.readFileSync(appRoot + '/db/sql/get/getsubscriptions.sql').toString(); - console.log(sql); const result = await pool.query(sql, [userid]); if (result.rows.length) return result.rows; return null; diff --git a/server/models/recipe.ts b/server/models/recipe.ts index ccd77cb..6d4b033 100644 --- a/server/models/recipe.ts +++ b/server/models/recipe.ts @@ -2,6 +2,7 @@ import { IRecipe } from "../schemas"; import pool from "../db"; import { CollectionCtl } from "../controllers"; import now from "../util/now"; +import { CtlResponse } from "../util/types"; const CollectionInstance = new CollectionCtl(); export class Recipe { @@ -76,13 +77,18 @@ export class Recipe { if (!result.rows.length) return null; // associate recipe with default collection once created - const collection = await CollectionInstance.getUserDefault(userid); + const collection: CtlResponse = await CollectionInstance.getUserDefault(userid); const associateToCollection = ` INSERT INTO recipin.cmp_recipecollection (recipeid, collectionid) VALUES ($1, $2) RETURNING * ` - const associateResult = await pool.query(associateToCollection, [result.rows[0].id, collection.id]); + + if (typeof collection.data == 'string') { + return null; + } + + const associateResult = await pool.query(associateToCollection, [result.rows[0].id, collection.data.id]); if (!associateResult.rows.length) return null; return { recipe: result.rows[0], collection: associateResult.rows[0] } diff --git a/server/routes/collection.ts b/server/routes/collection.ts index 2dd8356..1c1d14a 100644 --- a/server/routes/collection.ts +++ b/server/routes/collection.ts @@ -11,8 +11,8 @@ export const collectionRoute = (app: Express) => { router.get('/:id', restrictAccess, async (req, res, next) => { const { id } = req.params; try { - const result = await CollectionInstance.getOne(id); - res.status(200).send(result); + const { code, data } = await CollectionInstance.getOne(id); + res.status(code).send(data); } catch(e) { next(e); } @@ -21,8 +21,8 @@ export const collectionRoute = (app: Express) => { // implement is admin on this route router.get('/', restrictAccess, async (req, res, next) => { try { - const result = await CollectionInstance.getAll(); - res.status(200).send(result); + const { code, data } = await CollectionInstance.getAll(); + res.status(code).send(data); } catch(e) { next(e); } @@ -32,7 +32,7 @@ export const collectionRoute = (app: Express) => { const data = req.body; try { const result = await CollectionInstance.post(data); - res.status(201).send(result); + res.status(result.code).send(result.data); } catch(e) { next(e); } diff --git a/server/routes/course.ts b/server/routes/course.ts index c68b385..5882e2f 100644 --- a/server/routes/course.ts +++ b/server/routes/course.ts @@ -8,8 +8,8 @@ export const courseRouter = (app: Express) => { router.get('/', async (req, res, next) => { try { - const result = await CourseInstance.getAll(); - res.status(200).send(result); + const { code, data } = await CourseInstance.getAll(); + res.status(code).send(data); } catch(e) { next(e); } @@ -19,8 +19,8 @@ export const courseRouter = (app: Express) => { const { id } = req.params; try { - const result = await CourseInstance.getOne(id); - res.status(200).send(result); + const { code, data } = await CourseInstance.getOne(id); + res.status(code).send(data); } catch(e) { next(e); } @@ -31,7 +31,7 @@ export const courseRouter = (app: Express) => { try { const result = await CourseInstance.post(data); - res.status(201).send(result); + res.status(result.code).send(result.data); } catch(e) { next(e); } diff --git a/server/routes/friend.ts b/server/routes/friend.ts index 76f5765..701b35c 100644 --- a/server/routes/friend.ts +++ b/server/routes/friend.ts @@ -13,8 +13,8 @@ export const friendRouter = (app: Express) => { const { targetid } = req.params; try { - const result = await UserInstance.addFriendship(user.id, targetid); - res.status(200).send(result); + const { code, data } = await UserInstance.addFriendship(user.id, targetid); + res.status(code).send(data); } catch(e) { next(e); } @@ -27,11 +27,11 @@ export const friendRouter = (app: Express) => { try { if (pending) { - const result = await UserInstance.getPendingFriendRequests(user.id); - res.status(200).send(result); + const { code, data } = await UserInstance.getPendingFriendRequests(user.id); + res.status(code).send(data); } else { - const result = await UserInstance.getFriends(user.id); - res.status(200).send(result); + const { code, data } = await UserInstance.getFriends(user.id); + res.status(code).send(data); } } catch(e) { next(e); @@ -44,8 +44,8 @@ export const friendRouter = (app: Express) => { const { user }: any = req.user; try { - const result = await UserInstance.getFriendshipByID(id, user.id); - res.status(200).send(result); + const { code, data } = await UserInstance.getFriendshipByID(id, user.id); + res.status(code).send(data); } catch(e) { next(e); } @@ -58,8 +58,8 @@ export const friendRouter = (app: Express) => { const { user }: any = req.user; try { - const result = await UserInstance.updateFriendship(id, user.id, data); - res.status(200).send(result); + const response = await UserInstance.updateFriendship(id, user.id, data); + res.status(response.code).send(response.data); } catch(e) { next(e); } diff --git a/server/routes/groceryList.ts b/server/routes/groceryList.ts index 12257fd..b5da130 100644 --- a/server/routes/groceryList.ts +++ b/server/routes/groceryList.ts @@ -12,11 +12,11 @@ export const groceryListRoute = (app: Express) => { try { if (userid) { - const result = await groceryinstance.getByUserID(userid); - res.status(200).send(result); + const { code, data } = await groceryinstance.getByUserID(userid); + res.status(code).send(data); } else { - const result = await groceryinstance.getAll(); - res.status(200).send(result); + const { code, data } = await groceryinstance.getAll(); + res.status(code).send(data); } } catch(e) { next(e); @@ -26,8 +26,8 @@ export const groceryListRoute = (app: Express) => { router.get('/:id', async (req, res, next) => { const { id } = req.params; try { - const result = await groceryinstance.getOne(id); - res.status(200).send(result); + const { code, data } = await groceryinstance.getOne(id); + res.status(code).send(data); } catch(e) { next(e); } @@ -37,7 +37,7 @@ export const groceryListRoute = (app: Express) => { const data = req.body; try { const result = await groceryinstance.post(data); - res.status(201).send(result); + res.status(result.code).send(result.data); } catch(e) { next(e); } @@ -49,7 +49,7 @@ export const groceryListRoute = (app: Express) => { try { const result = await groceryinstance.put(id, data); - res.status(200).send(result); + res.status(result.code).send(result.data); } catch(e) { next(e); } diff --git a/server/routes/index.ts b/server/routes/index.ts index 591dc0e..1306e00 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -17,11 +17,15 @@ export const routes = async (app: Express, passport: PassportStatic) => { authRoute(app, passport); userRoute(app); friendRouter(app); - cuisineRouter(app); - courseRouter(app); recipeRoute(app); + ingredientRoute(app); + + // to do: refactor for ctlresponse collectionRoute(app); subscriptionRoute(app); - ingredientRoute(app); groceryListRoute(app); + courseRouter(app); + + // deprecate? + cuisineRouter(app); } \ No newline at end of file diff --git a/server/routes/recipe.ts b/server/routes/recipe.ts index 797b147..99b9657 100644 --- a/server/routes/recipe.ts +++ b/server/routes/recipe.ts @@ -14,8 +14,8 @@ export const recipeRoute = (app: Express) => { const { id } = req.params; try { - const result = await recipectl.getOne(id); - res.status(200).send(result); + const { code, data } = await recipectl.getOne(id); + res.status(code).send(data); } catch(e) { next(e); } diff --git a/server/util/ControllerResponse.ts b/server/util/ControllerResponse.ts index 87fde86..7bdb4ee 100644 --- a/server/util/ControllerResponse.ts +++ b/server/util/ControllerResponse.ts @@ -5,10 +5,10 @@ export default class ControllerResponse implements CtlResponse { code: StatusCode data: T | string - constructor(ok: boolean, code: StatusCode, data: T | string) { - this.ok = ok + constructor(code: StatusCode, data: T | string, ok?: boolean) { this.code = code this.data = data + this.ok = ok || (this.data !== null) } send() {