few more routes added. process of refactoring controllers still underway

This commit is contained in:
Mikayla Dobson
2022-12-12 20:32:22 -06:00
parent d2b06ced7a
commit 5234e54bcc
5 changed files with 53 additions and 26 deletions

View File

@@ -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);
}

View File

@@ -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);
})
})
})

View File

@@ -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<IIngredient[] | string> = 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<IIngredient | string> = 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<IIngredient | string> = 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<IIngredient | string> = await IngredientInstance.post(data);
res.status(result.code).send(result.data);
} catch(e) {
next(e);
}

View File

@@ -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<IRecipe | string>;
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<IRecipe | string> = 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);
}

View File

@@ -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