refactoring controller responses (in progress)

This commit is contained in:
Mikayla Dobson
2022-12-12 20:07:09 -06:00
parent 362cf752d0
commit d2b06ced7a
6 changed files with 83 additions and 44 deletions

View File

@@ -1,14 +1,14 @@
import createError from "http-errors";
import { IRecipe } from "../schemas";
import { Recipe } from "../models/recipe";
import ControllerResponse from "../util/ControllerResponse";
const RecipeInstance = new Recipe();
export default class RecipeCtl {
async getOne(id: string) {
try {
const result = await RecipeInstance.getOneByID(id);
if (!result) throw createError('404', "Recipe not found");
return result;
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipe found with this ID"));
} catch (error: any) {
throw new Error(error);
}
@@ -17,8 +17,8 @@ export default class RecipeCtl {
async getAllAuthored(id: string) {
try {
const result = await RecipeInstance.getAllAuthored(id);
if (!result) throw createError('404', "No recipes found");
return result;
const ok = result !== null;
return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipes found authored by user with this ID"));
} catch (e: any) {
throw new Error(e);
}
@@ -35,8 +35,8 @@ export default class RecipeCtl {
async updateOne(id: string, data: IRecipe) {
try {
const result = await RecipeInstance.updateOneByID(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 (error: any) {
throw new Error(error);
}
@@ -45,8 +45,8 @@ export default class RecipeCtl {
async post(userid: string, data: IRecipe) {
try {
const result = await RecipeInstance.post(userid, 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 (error: any) {
throw new Error(error);
}