From f0d515446f72757f771a311a9ce883e9399c96eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chif=20Gerg=C5=91?= <chif.gergo@kszk.bme.hu> Date: Mon, 9 Mar 2020 22:57:37 +0100 Subject: [PATCH] Add return middlewares in the routes --- src/routes/news.ts | 49 +++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/routes/news.ts b/src/routes/news.ts index 807e7abe..a2a54bf1 100644 --- a/src/routes/news.ts +++ b/src/routes/news.ts @@ -1,26 +1,35 @@ -import { Application } from 'express'; -import getNewsListMiddleware from '../middlewares/news/getNewsList'; -import addNews from '../middlewares/news/addNews'; -import getNews from '../middlewares/news/getNews'; -import deleteNews from '../middlewares/news/deleteNews'; +import { Application, Response } from "express"; +import getNewsListMiddleware from "../middlewares/news/getNewsList"; +import addNews from "../middlewares/news/addNews"; +import getNews from "../middlewares/news/getNews"; +import deleteNews from "../middlewares/news/deleteNews"; export default (app: Application): void => { - app.get('/news', - getNewsListMiddleware() - ); + app.get("/news", getNewsListMiddleware(), (req, res: Response) => { + if (res.data.news) { + res.json(res.data.news); + } else { + throw Error("Cant get the news list"); + } + }); - app.post('/news', - addNews() - ); + app.post("/news", addNews(), (req, res: Response) => { + if (res.data.newsObject) { + res.json(res.data.newsObject); + } else { + throw Error("Cant add news"); + } + }); - app.get('/news/:id', - getNews() - ); + app.get("/news/:id", getNews(), (req, res: Response) => { + if (res.data.newsObject) { + res.json(res.data.newsObject); + } else { + throw Error("Cant get the news"); + } + }); - app.post('/news/:id' - ); + app.post("/news/:id"); - app.delete('/news/:id', - deleteNews() - ); -} \ No newline at end of file + app.delete("/news/:id", deleteNews()); +}; -- GitLab