few more routes added. process of refactoring controllers still underway
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
import createError from "http-errors";
|
|
||||||
import { IIngredient } from "../schemas";
|
import { IIngredient } from "../schemas";
|
||||||
import { Ingredient } from "../models/ingredient";
|
import { Ingredient } from "../models/ingredient";
|
||||||
|
import ControllerResponse from "../util/ControllerResponse";
|
||||||
const IngredientInstance = new Ingredient();
|
const IngredientInstance = new Ingredient();
|
||||||
|
|
||||||
export default class IngredientCtl {
|
export default class IngredientCtl {
|
||||||
async getAll() {
|
async getAll() {
|
||||||
try {
|
try {
|
||||||
const result = await IngredientInstance.getAll();
|
const result = await IngredientInstance.getAll();
|
||||||
if (!result) throw createError('404', 'No ingredients found');
|
const ok = result !== null;
|
||||||
return result;
|
return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipes found"));
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
@@ -17,8 +17,8 @@ export default class IngredientCtl {
|
|||||||
async getOne(id: string) {
|
async getOne(id: string) {
|
||||||
try {
|
try {
|
||||||
const result = await IngredientInstance.getOne(id);
|
const result = await IngredientInstance.getOne(id);
|
||||||
if (!result) throw createError('404', 'No ingredient found with id ' + id);
|
const ok = result !== null;
|
||||||
return result;
|
return new ControllerResponse(ok, (ok ? 200 : 404), (ok ? result : "No recipe found with this ID"));
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
@@ -27,8 +27,8 @@ export default class IngredientCtl {
|
|||||||
async post(data: IIngredient) {
|
async post(data: IIngredient) {
|
||||||
try {
|
try {
|
||||||
const result = await IngredientInstance.post(data);
|
const result = await IngredientInstance.post(data);
|
||||||
if (!result) throw createError('400', 'Bad request');
|
const ok = result !== null;
|
||||||
return result;
|
return new ControllerResponse(ok, (ok ? 201 : 400), (ok ? result : "Something went wrong"));
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
@@ -37,8 +37,8 @@ export default class IngredientCtl {
|
|||||||
async put(id: string, data: IIngredient) {
|
async put(id: string, data: IIngredient) {
|
||||||
try {
|
try {
|
||||||
const result = await IngredientInstance.put(id, data);
|
const result = await IngredientInstance.put(id, data);
|
||||||
if (!result) throw createError('400', 'Bad request');
|
const ok = result !== null;
|
||||||
return result;
|
return new ControllerResponse(ok, (ok ? 200 : 400), (ok ? result : "Something went wrong"));
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
import { Express, Router } from "express";
|
import { Express, Router } from "express";
|
||||||
import { IngredientCtl } from "../controllers";
|
import { IngredientCtl } from "../controllers";
|
||||||
|
import { IIngredient } from "../schemas";
|
||||||
|
import { CtlResponse } from "../util/types";
|
||||||
const IngredientInstance = new IngredientCtl();
|
const IngredientInstance = new IngredientCtl();
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
@@ -9,8 +11,8 @@ export const ingredientRoute = (app: Express) => {
|
|||||||
|
|
||||||
router.get('/', async (req, res, next) => {
|
router.get('/', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const result = await IngredientInstance.getAll();
|
const result: CtlResponse<IIngredient[] | string> = await IngredientInstance.getAll();
|
||||||
res.status(200).send(result);
|
res.status(result.code).send(result.data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
@@ -20,8 +22,8 @@ export const ingredientRoute = (app: Express) => {
|
|||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await IngredientInstance.getOne(id);
|
const result: CtlResponse<IIngredient | string> = await IngredientInstance.getOne(id);
|
||||||
res.status(200).send(result);
|
res.status(result.code).send(result.data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
@@ -32,8 +34,8 @@ export const ingredientRoute = (app: Express) => {
|
|||||||
const data = req.body;
|
const data = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await IngredientInstance.put(id, data);
|
const result: CtlResponse<IIngredient | string> = await IngredientInstance.put(id, data);
|
||||||
res.status(200).send(result);
|
res.status(result.code).send(result.data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
@@ -43,8 +45,8 @@ export const ingredientRoute = (app: Express) => {
|
|||||||
const data = req.body;
|
const data = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await IngredientInstance.post(data);
|
const result: CtlResponse<IIngredient | string> = await IngredientInstance.post(data);
|
||||||
res.status(201).send(result);
|
res.status(result.code).send(result.data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { Express, Router } from "express"
|
import { Express, Router } from "express"
|
||||||
import { restrictAccess } from "../auth/middlewares";
|
import { restrictAccess } from "../auth/middlewares";
|
||||||
import RecipeCtl from "../controllers/RecipeCtl";
|
import RecipeCtl from "../controllers/RecipeCtl";
|
||||||
|
import { IRecipe } from "../schemas";
|
||||||
|
import { CtlResponse } from "../util/types";
|
||||||
const recipectl = new RecipeCtl();
|
const recipectl = new RecipeCtl();
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
@@ -24,17 +26,17 @@ export const recipeRoute = (app: Express) => {
|
|||||||
const { filterby } = req.query;
|
const { filterby } = req.query;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let result;
|
let result: CtlResponse<IRecipe | string>;
|
||||||
switch (filterby) {
|
switch (filterby) {
|
||||||
case "allaccessible":
|
case "allaccessible":
|
||||||
result = await recipectl.getAllAccessible(user.id);
|
// result = await recipectl.getAllAccessible(user.id);
|
||||||
break;
|
// break;
|
||||||
default:
|
default:
|
||||||
result = await recipectl.getAllAuthored(user.id);
|
result = await recipectl.getAllAuthored(user.id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).send(result);
|
res.status(result.code).send(result.data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
@@ -45,8 +47,8 @@ export const recipeRoute = (app: Express) => {
|
|||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await recipectl.updateOne(id, data);
|
const result: CtlResponse<IRecipe | string> = await recipectl.updateOne(id, data);
|
||||||
res.status(200).send(result);
|
res.status(result.code).send(result.data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
@@ -58,7 +60,7 @@ export const recipeRoute = (app: Express) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await recipectl.post(user.id, data);
|
const result = await recipectl.post(user.id, data);
|
||||||
res.status(201).send(result);
|
res.status(result.code).send(result.data);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ export const userRoute = (app: Express) => {
|
|||||||
// create user
|
// create user
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
const data = req.body;
|
const data = req.body;
|
||||||
const response = userCtl.post(data);
|
const response = await userCtl.post(data);
|
||||||
res.status(200).send(response);
|
res.status(response.code).send(response.data);
|
||||||
})
|
})
|
||||||
|
|
||||||
// get friendships by requester ID
|
// get friendships by requester ID
|
||||||
|
|||||||
Reference in New Issue
Block a user