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