Skip to content

Commit

Permalink
api: fix news handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mvshmakov committed Jun 3, 2020
1 parent 81c5020 commit b8e8f14
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/api/v1/insomnia_api_v1.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_type": "export",
"__export_format": 4,
"__export_date": "2020-06-01T12:56:28.879Z",
"__export_date": "2020-06-03T13:43:19.531Z",
"__export_source": "insomnia.desktop.app:v7.1.1",
"resources": [
{
Expand Down Expand Up @@ -201,7 +201,7 @@
"authentication": {},
"body": {
"mimeType": "application/json",
"text": "{\n\t\"topic\": \"Внутренняя политика\",\n\t\"num\": \"15\"\n}\n"
"text": "{\n\t\"topic\": \"Внутренняя политика\",\n\t\"num\": 15\n}\n"
},
"created": 1587585865168,
"description": "",
Expand All @@ -221,7 +221,7 @@
"isPrivate": false,
"metaSortKey": -1587586022120,
"method": "POST",
"modified": 1587586082040,
"modified": 1591191613372,
"name": "/getNews",
"parameters": [],
"parentId": "fld_260538bd7c3e432b8fdcdae128a0169d",
Expand Down
24 changes: 14 additions & 10 deletions src/api/v1/routes/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ const newsRoutes = Router();

type TGetNewsRequestBody = {
topic: string;
page?: number;
num?: number;
};

newsRoutes.get("/getNews", [
// TODO: fix num, get rid of id and use page
newsRoutes.post("/getNews", [
authMiddleware,
(req: CustomRequest<TGetNewsRequestBody>, res: Response): void => {
const { page, topic } = req.body;
async (
req: CustomRequest<TGetNewsRequestBody>,
res: Response,
): Promise<void> => {
const { topic, num } = req.body;

if (!topic) {
res.status(400).send("'num' and 'topic' should be provided");
res.status(400).send("'page' and 'topic' should be provided");
return;
}

Expand All @@ -27,12 +31,12 @@ newsRoutes.get("/getNews", [
return;
}

// if (typeof num !== undefined && typeof num !== "number") {
// res.status(400).send("'num' should be 'number'");
// return;
// }
if (typeof num !== "number") {
res.status(400).send("'num' should be 'number'");
return;
}

res.status(200).send(resolveNewsArticles(topic, page));
res.status(200).send(await resolveNewsArticles(topic, num));
},
]);

Expand Down

0 comments on commit b8e8f14

Please sign in to comment.