refactoring on back end

This commit is contained in:
Mikayla Dobson
2022-12-17 13:30:55 -06:00
parent be375aad8f
commit 9a43f58ee3
15 changed files with 117 additions and 90 deletions

View File

@@ -1,46 +1,50 @@
import createError from "http-errors"; import createError from "http-errors";
import { ICollection } from "../schemas"; import { ICollection } from "../schemas";
import { Collection } from "../models/collection"; import { Collection } from "../models/collection";
import { StatusCode } from "../util/types";
import ControllerResponse from "../util/ControllerResponse";
const CollectionInstance = new Collection(); const CollectionInstance = new Collection();
export default class CollectionCtl { export default class CollectionCtl {
async getOne(id: number | string) { async getOne(id: number | string) {
const result = await CollectionInstance.getOne(id); 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() { async getAll() {
const result = await CollectionInstance.getAll(); const result = await CollectionInstance.getAll();
if (!result) throw createError('404', 'No collections found'); const ok = result !== null;
return result; const code = ok ? StatusCode.OK : StatusCode.NotFound;
const data = result || "No collections found"
return new ControllerResponse(code, data);
} }
async getUserDefault(id: number | string) { async getUserDefault(id: number | string) {
const result = await CollectionInstance.getUserDefault(id); const result = await CollectionInstance.getUserDefault(id);
if (!result) throw createError('404', "No default collection found. Contact an admin, this isn't supposed to happen"); const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return result; const data = result || "No default collection found"
return new ControllerResponse(code, data);
} }
async post(data: ICollection) { async post(body: ICollection) {
const result = await CollectionInstance.post(data); const result = await CollectionInstance.post(body);
if (!result) throw createError('400', 'Bad request'); const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest;
return result; const data = result || "Something went wrong"
return new ControllerResponse(code, data);
} }
async getSubscriptions(userid: number | string) { async getSubscriptions(userid: number | string) {
const result = await CollectionInstance.getSubscriptions(userid); const result = await CollectionInstance.getSubscriptions(userid);
if (!result) throw createError('404', 'No subscriptions found'); const code = (result !== null) ? StatusCode.OK : StatusCode.NoContent;
return result; return new ControllerResponse(code, (result || "No subscriptions found"));
} }
async postSubscription(collectionid: number | string, userid: number | string) { async postSubscription(collectionid: number | string, userid: number | string) {
const { ok, code, data } = await CollectionInstance.postSubscription(collectionid, userid); const { code, data } = await CollectionInstance.postSubscription(collectionid, userid);
return new ControllerResponse(code, data);
if (ok) {
return data;
} else {
throw createError(code, data as string);
}
} }
} }

View File

@@ -1,14 +1,16 @@
import createError from 'http-errors'; import createError from 'http-errors';
import { ICourse } from '../schemas'; import { ICourse } from '../schemas';
import Course from '../models/course'; import Course from '../models/course';
import ControllerResponse from '../util/ControllerResponse';
import { StatusCode } from '../util/types';
const CourseInstance = new Course(); const CourseInstance = new Course();
export default class CourseCtl { export default class CourseCtl {
async getAll() { async getAll() {
try { try {
const result = await CourseInstance.getAll(); const result = await CourseInstance.getAll();
if (!result) throw createError('404', 'No cuisines found'); const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return result; return new ControllerResponse(code, (result || "No course data found"));
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -17,8 +19,8 @@ export default class CourseCtl {
async getOne(id: string) { async getOne(id: string) {
try { try {
const result = await CourseInstance.getOne(id); const result = await CourseInstance.getOne(id);
if (!result) throw createError('404', 'No cuisine found with id ' + id); const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return result; return new ControllerResponse(code, (result || "No course found with this ID"))
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -27,8 +29,8 @@ export default class CourseCtl {
async post(data: ICourse) { async post(data: ICourse) {
try { try {
const result = await CourseInstance.post(data); const result = await CourseInstance.post(data);
if (!result) throw createError('400', 'Bad request'); const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest;
return result; return new ControllerResponse(code, (result || "Something went wrong"));
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }

View File

@@ -1,14 +1,16 @@
import createError from "http-errors"; import createError from "http-errors";
import { IGroceryList } from "../schemas"; import { IGroceryList } from "../schemas";
import { GroceryList } from "../models/groceryList"; import { GroceryList } from "../models/groceryList";
import { StatusCode } from "../util/types";
import ControllerResponse from "../util/ControllerResponse";
const GroceryInstance = new GroceryList(); const GroceryInstance = new GroceryList();
export default class GroceryListCtl { export default class GroceryListCtl {
async getAll() { async getAll() {
try { try {
const result = await GroceryInstance.getAll(); const result = await GroceryInstance.getAll();
if (!result) throw createError('404', 'No results found'); const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return result; return new ControllerResponse(code, (result || "No grocery list data found"));
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -17,8 +19,8 @@ export default class GroceryListCtl {
async getOne(id: string) { async getOne(id: string) {
try { try {
const result = await GroceryInstance.getOne(id); const result = await GroceryInstance.getOne(id);
if (!result) throw createError('404', 'No results found'); const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return result; return new ControllerResponse(code, (result || "No grocery list data found with this ID"));
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -27,8 +29,8 @@ export default class GroceryListCtl {
async getByUserID(userid: string) { async getByUserID(userid: string) {
try { try {
const result = await GroceryInstance.getByUserID(userid); const result = await GroceryInstance.getByUserID(userid);
if (!result) throw createError('404', 'No results found'); const code = (result !== null) ? StatusCode.OK : StatusCode.NotFound;
return result; return new ControllerResponse(code, (result || "No grocery list data found matching this user ID"));
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -37,8 +39,8 @@ export default class GroceryListCtl {
async post(data: IGroceryList) { async post(data: IGroceryList) {
try { try {
const result = await GroceryInstance.post(data); const result = await GroceryInstance.post(data);
if (!result) throw createError('400', 'Bad request'); const code = (result !== null) ? StatusCode.NewContent : StatusCode.BadRequest;
return result; return new ControllerResponse(code, (result || "Something went wrong"));
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -47,8 +49,8 @@ export default class GroceryListCtl {
async put(id: string, data: IGroceryList) { async put(id: string, data: IGroceryList) {
try { try {
const result = await GroceryInstance.put(id, data); const result = await GroceryInstance.put(id, data);
if (!result) throw createError('400', 'Bad request'); const code = (result !== null) ? StatusCode.OK : StatusCode.BadRequest;
return result; return new ControllerResponse(code, (result || "Something went wrong"));
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }

View File

@@ -1,6 +1,7 @@
import { IIngredient } from "../schemas"; import { IIngredient } from "../schemas";
import { Ingredient } from "../models/ingredient"; import { Ingredient } from "../models/ingredient";
import ControllerResponse from "../util/ControllerResponse"; import ControllerResponse from "../util/ControllerResponse";
import { StatusCode } from "../util/types";
const IngredientInstance = new Ingredient(); const IngredientInstance = new Ingredient();
export default class IngredientCtl { export default class IngredientCtl {
@@ -8,7 +9,8 @@ export default class IngredientCtl {
try { try {
const result = await IngredientInstance.getAll(); const result = await IngredientInstance.getAll();
const ok = result !== null; 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) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -18,7 +20,8 @@ export default class IngredientCtl {
try { try {
const result = await IngredientInstance.getOne(id); const result = await IngredientInstance.getOne(id);
const ok = result !== null; 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) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -28,7 +31,8 @@ export default class IngredientCtl {
try { try {
const result = await IngredientInstance.post(data); const result = await IngredientInstance.post(data);
const ok = result !== null; 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) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -38,7 +42,8 @@ export default class IngredientCtl {
try { try {
const result = await IngredientInstance.put(id, data); const result = await IngredientInstance.put(id, data);
const ok = result !== null; 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) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }

View File

@@ -1,6 +1,7 @@
import { IRecipe } from "../schemas"; import { IRecipe } from "../schemas";
import { Recipe } from "../models/recipe"; import { Recipe } from "../models/recipe";
import ControllerResponse from "../util/ControllerResponse"; import ControllerResponse from "../util/ControllerResponse";
import { StatusCode } from "../util/types";
const RecipeInstance = new Recipe(); const RecipeInstance = new Recipe();
export default class RecipeCtl { export default class RecipeCtl {
@@ -8,7 +9,8 @@ export default class RecipeCtl {
try { try {
const result = await RecipeInstance.getOneByID(id); const result = await RecipeInstance.getOneByID(id);
const ok = result !== null; 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) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
@@ -18,7 +20,8 @@ export default class RecipeCtl {
try { try {
const result = await RecipeInstance.getAllAuthored(id); const result = await RecipeInstance.getAllAuthored(id);
const ok = result !== null; 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) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -36,7 +39,8 @@ export default class RecipeCtl {
try { try {
const result = await RecipeInstance.updateOneByID(id, data); const result = await RecipeInstance.updateOneByID(id, data);
const ok = result !== null; 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) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
@@ -46,7 +50,8 @@ export default class RecipeCtl {
try { try {
const result = await RecipeInstance.post(userid, data); const result = await RecipeInstance.post(userid, data);
const ok = result !== null; 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) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }

View File

@@ -16,7 +16,7 @@ export default class UserCtl {
const data: IUser[] | string = ok ? users! : "No users found."; const data: IUser[] | string = ok ? users! : "No users found.";
// send formatted response with either data or informative error message // send formatted response with either data or informative error message
return new ControllerResponse<IUser[] | string>(ok, code, data) return new ControllerResponse<IUser[] | string>(code, data, ok)
} catch (error: any) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
@@ -28,7 +28,7 @@ export default class UserCtl {
const ok = response !== null; const ok = response !== null;
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest const code = ok ? StatusCode.NewContent : StatusCode.BadRequest
const data = ok ? response : "Bad request" const data = ok ? response : "Bad request"
return new ControllerResponse(ok, code, data); return new ControllerResponse(code, data);
} catch (error: any) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
@@ -40,7 +40,7 @@ export default class UserCtl {
const ok = user !== null; const ok = user !== null;
const code = ok ? StatusCode.OK : StatusCode.NotFound; const code = ok ? StatusCode.OK : StatusCode.NotFound;
const data = ok ? user : "User by this ID not found"; const data = ok ? user : "User by this ID not found";
return new ControllerResponse(ok, code, data); return new ControllerResponse(code, data);
} catch (error: any) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
@@ -52,7 +52,7 @@ export default class UserCtl {
const ok = result !== null; const ok = result !== null;
const code = ok ? StatusCode.OK : StatusCode.BadRequest; const code = ok ? StatusCode.OK : StatusCode.BadRequest;
const data = ok ? result : "Update unsuccessful" const data = ok ? result : "Update unsuccessful"
return new ControllerResponse(ok, code, data); return new ControllerResponse(code, data);
} catch (error: any) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
@@ -64,7 +64,7 @@ export default class UserCtl {
const ok = result !== null const ok = result !== null
const code = ok ? StatusCode.OK : StatusCode.NotFound; const code = ok ? StatusCode.OK : StatusCode.NotFound;
const data = ok ? result : "No friends found" const data = ok ? result : "No friends found"
return new ControllerResponse(ok, code, data); return new ControllerResponse(code, data);
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -73,7 +73,7 @@ export default class UserCtl {
async getFriendshipByID(id: number | string, userid: number | string) { async getFriendshipByID(id: number | string, userid: number | string) {
try { try {
const { ok, code, result } = await UserInstance.getFriendshipByID(id, userid); const { ok, code, result } = await UserInstance.getFriendshipByID(id, userid);
return new ControllerResponse(ok, code, result); return new ControllerResponse(code, result);
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -82,7 +82,7 @@ export default class UserCtl {
async getPendingFriendRequests(senderid: string | number) { async getPendingFriendRequests(senderid: string | number) {
try { try {
const { ok, code, result } = await UserInstance.getPendingFriendRequests(senderid); const { ok, code, result } = await UserInstance.getPendingFriendRequests(senderid);
return new ControllerResponse(ok, code, result); return new ControllerResponse(code, result);
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }
@@ -94,7 +94,7 @@ export default class UserCtl {
const ok = result !== null; const ok = result !== null;
const code = ok ? StatusCode.NewContent : StatusCode.BadRequest; const code = ok ? StatusCode.NewContent : StatusCode.BadRequest;
const data = ok ? result : "Something went wrong" const data = ok ? result : "Something went wrong"
return new ControllerResponse(ok, code, data); return new ControllerResponse(code, data);
} catch (e: any) { } catch (e: any) {
throw new Error(e); 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 }) { async updateFriendship(id: number | string, userid: number | string, data: { active: boolean, pending: boolean, dateterminated?: string }) {
try { try {
const { ok, code, result } = await UserInstance.updateFriendship(id, userid, data); const { ok, code, result } = await UserInstance.updateFriendship(id, userid, data);
return new ControllerResponse(ok, code, result); return new ControllerResponse(code, result);
} catch (e: any) { } catch (e: any) {
throw new Error(e); throw new Error(e);
} }

View File

@@ -67,7 +67,6 @@ export class Collection {
async getSubscriptions(userid: number | string) { async getSubscriptions(userid: number | string) {
try { try {
const sql = fs.readFileSync(appRoot + '/db/sql/get/getsubscriptions.sql').toString(); const sql = fs.readFileSync(appRoot + '/db/sql/get/getsubscriptions.sql').toString();
console.log(sql);
const result = await pool.query(sql, [userid]); const result = await pool.query(sql, [userid]);
if (result.rows.length) return result.rows; if (result.rows.length) return result.rows;
return null; return null;

View File

@@ -2,6 +2,7 @@ import { IRecipe } from "../schemas";
import pool from "../db"; import pool from "../db";
import { CollectionCtl } from "../controllers"; import { CollectionCtl } from "../controllers";
import now from "../util/now"; import now from "../util/now";
import { CtlResponse } from "../util/types";
const CollectionInstance = new CollectionCtl(); const CollectionInstance = new CollectionCtl();
export class Recipe { export class Recipe {
@@ -76,13 +77,18 @@ export class Recipe {
if (!result.rows.length) return null; if (!result.rows.length) return null;
// associate recipe with default collection once created // associate recipe with default collection once created
const collection = await CollectionInstance.getUserDefault(userid); const collection: CtlResponse<IRecipe> = await CollectionInstance.getUserDefault(userid);
const associateToCollection = ` const associateToCollection = `
INSERT INTO recipin.cmp_recipecollection INSERT INTO recipin.cmp_recipecollection
(recipeid, collectionid) (recipeid, collectionid)
VALUES ($1, $2) RETURNING * 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; if (!associateResult.rows.length) return null;
return { recipe: result.rows[0], collection: associateResult.rows[0] } return { recipe: result.rows[0], collection: associateResult.rows[0] }

View File

@@ -11,8 +11,8 @@ export const collectionRoute = (app: Express) => {
router.get('/:id', restrictAccess, async (req, res, next) => { router.get('/:id', restrictAccess, async (req, res, next) => {
const { id } = req.params; const { id } = req.params;
try { try {
const result = await CollectionInstance.getOne(id); const { code, data } = await CollectionInstance.getOne(id);
res.status(200).send(result); res.status(code).send(data);
} catch(e) { } catch(e) {
next(e); next(e);
} }
@@ -21,8 +21,8 @@ export const collectionRoute = (app: Express) => {
// implement is admin on this route // implement is admin on this route
router.get('/', restrictAccess, async (req, res, next) => { router.get('/', restrictAccess, async (req, res, next) => {
try { try {
const result = await CollectionInstance.getAll(); const { code, data } = await CollectionInstance.getAll();
res.status(200).send(result); res.status(code).send(data);
} catch(e) { } catch(e) {
next(e); next(e);
} }
@@ -32,7 +32,7 @@ export const collectionRoute = (app: Express) => {
const data = req.body; const data = req.body;
try { try {
const result = await CollectionInstance.post(data); const result = await CollectionInstance.post(data);
res.status(201).send(result); res.status(result.code).send(result.data);
} catch(e) { } catch(e) {
next(e); next(e);
} }

View File

@@ -8,8 +8,8 @@ export const courseRouter = (app: Express) => {
router.get('/', async (req, res, next) => { router.get('/', async (req, res, next) => {
try { try {
const result = await CourseInstance.getAll(); const { code, data } = await CourseInstance.getAll();
res.status(200).send(result); res.status(code).send(data);
} catch(e) { } catch(e) {
next(e); next(e);
} }
@@ -19,8 +19,8 @@ export const courseRouter = (app: Express) => {
const { id } = req.params; const { id } = req.params;
try { try {
const result = await CourseInstance.getOne(id); const { code, data } = await CourseInstance.getOne(id);
res.status(200).send(result); res.status(code).send(data);
} catch(e) { } catch(e) {
next(e); next(e);
} }
@@ -31,7 +31,7 @@ export const courseRouter = (app: Express) => {
try { try {
const result = await CourseInstance.post(data); const result = await CourseInstance.post(data);
res.status(201).send(result); res.status(result.code).send(result.data);
} catch(e) { } catch(e) {
next(e); next(e);
} }

View File

@@ -13,8 +13,8 @@ export const friendRouter = (app: Express) => {
const { targetid } = req.params; const { targetid } = req.params;
try { try {
const result = await UserInstance.addFriendship(user.id, targetid); const { code, data } = await UserInstance.addFriendship(user.id, targetid);
res.status(200).send(result); res.status(code).send(data);
} catch(e) { } catch(e) {
next(e); next(e);
} }
@@ -27,11 +27,11 @@ export const friendRouter = (app: Express) => {
try { try {
if (pending) { if (pending) {
const result = await UserInstance.getPendingFriendRequests(user.id); const { code, data } = await UserInstance.getPendingFriendRequests(user.id);
res.status(200).send(result); res.status(code).send(data);
} else { } else {
const result = await UserInstance.getFriends(user.id); const { code, data } = await UserInstance.getFriends(user.id);
res.status(200).send(result); res.status(code).send(data);
} }
} catch(e) { } catch(e) {
next(e); next(e);
@@ -44,8 +44,8 @@ export const friendRouter = (app: Express) => {
const { user }: any = req.user; const { user }: any = req.user;
try { try {
const result = await UserInstance.getFriendshipByID(id, user.id); const { code, data } = await UserInstance.getFriendshipByID(id, user.id);
res.status(200).send(result); res.status(code).send(data);
} catch(e) { } catch(e) {
next(e); next(e);
} }
@@ -58,8 +58,8 @@ export const friendRouter = (app: Express) => {
const { user }: any = req.user; const { user }: any = req.user;
try { try {
const result = await UserInstance.updateFriendship(id, user.id, data); const response = await UserInstance.updateFriendship(id, user.id, data);
res.status(200).send(result); res.status(response.code).send(response.data);
} catch(e) { } catch(e) {
next(e); next(e);
} }

View File

@@ -12,11 +12,11 @@ export const groceryListRoute = (app: Express) => {
try { try {
if (userid) { if (userid) {
const result = await groceryinstance.getByUserID(userid); const { code, data } = await groceryinstance.getByUserID(userid);
res.status(200).send(result); res.status(code).send(data);
} else { } else {
const result = await groceryinstance.getAll(); const { code, data } = await groceryinstance.getAll();
res.status(200).send(result); res.status(code).send(data);
} }
} catch(e) { } catch(e) {
next(e); next(e);
@@ -26,8 +26,8 @@ export const groceryListRoute = (app: Express) => {
router.get('/:id', async (req, res, next) => { router.get('/:id', async (req, res, next) => {
const { id } = req.params; const { id } = req.params;
try { try {
const result = await groceryinstance.getOne(id); const { code, data } = await groceryinstance.getOne(id);
res.status(200).send(result); res.status(code).send(data);
} catch(e) { } catch(e) {
next(e); next(e);
} }
@@ -37,7 +37,7 @@ export const groceryListRoute = (app: Express) => {
const data = req.body; const data = req.body;
try { try {
const result = await groceryinstance.post(data); const result = await groceryinstance.post(data);
res.status(201).send(result); res.status(result.code).send(result.data);
} catch(e) { } catch(e) {
next(e); next(e);
} }
@@ -49,7 +49,7 @@ export const groceryListRoute = (app: Express) => {
try { try {
const result = await groceryinstance.put(id, data); const result = await groceryinstance.put(id, data);
res.status(200).send(result); res.status(result.code).send(result.data);
} catch(e) { } catch(e) {
next(e); next(e);
} }

View File

@@ -17,11 +17,15 @@ export const routes = async (app: Express, passport: PassportStatic) => {
authRoute(app, passport); authRoute(app, passport);
userRoute(app); userRoute(app);
friendRouter(app); friendRouter(app);
cuisineRouter(app);
courseRouter(app);
recipeRoute(app); recipeRoute(app);
ingredientRoute(app);
// to do: refactor for ctlresponse
collectionRoute(app); collectionRoute(app);
subscriptionRoute(app); subscriptionRoute(app);
ingredientRoute(app);
groceryListRoute(app); groceryListRoute(app);
courseRouter(app);
// deprecate?
cuisineRouter(app);
} }

View File

@@ -14,8 +14,8 @@ export const recipeRoute = (app: Express) => {
const { id } = req.params; const { id } = req.params;
try { try {
const result = await recipectl.getOne(id); const { code, data } = await recipectl.getOne(id);
res.status(200).send(result); res.status(code).send(data);
} catch(e) { } catch(e) {
next(e); next(e);
} }

View File

@@ -5,10 +5,10 @@ export default class ControllerResponse<T> implements CtlResponse<T> {
code: StatusCode code: StatusCode
data: T | string data: T | string
constructor(ok: boolean, code: StatusCode, data: T | string) { constructor(code: StatusCode, data: T | string, ok?: boolean) {
this.ok = ok
this.code = code this.code = code
this.data = data this.data = data
this.ok = ok || (this.data !== null)
} }
send() { send() {