Skip to content

Commit 395e6d0

Browse files
Merge pull request #72 from italia/fix/pager_api
Fix/pager api
2 parents 454cec1 + c62a0bf commit 395e6d0

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

controller/jobController.ts

+15-20
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ export class jobController {
7575
entityExternalId
7676
);
7777

78-
if (entityObj === null || parseInt(limit) <= 0) {
79-
return returnValues;
80-
}
81-
8278
let condition = {};
8379
if (Boolean(dateFrom) && Boolean(dateTo)) {
8480
condition = {
@@ -92,23 +88,22 @@ export class jobController {
9288

9389
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
9490
//@ts-ignore
95-
const countData = await entityObj.getJobs(condition);
96-
let cont = 0;
97-
countData.forEach((job) => {
98-
if (job) {
99-
cont++;
100-
}
101-
});
91+
const countData: Job[] = await entityObj.getJobs(condition);
92+
const cont = countData.length;
10293

103-
const pages = Math.ceil(cont / limit);
104-
const offset = limit * (page - 1);
105-
condition = {
106-
...condition,
107-
...{
108-
offset: offset,
109-
limit: limit,
110-
},
111-
};
94+
let pages = 1;
95+
96+
if (limit > 0 && page >= 0) {
97+
pages = Math.ceil(cont / limit);
98+
const offset = limit * page;
99+
condition = {
100+
...condition,
101+
...{
102+
offset: offset,
103+
limit: limit,
104+
},
105+
};
106+
}
112107

113108
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
114109
//@ts-ignore - getJobs(): metodo autogenerato dall'ORM Sequelize dopo l'associazione

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crawler-handler",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"description": "Handler per il validatore di comuni e scuole",
55
"main": "index.js",
66
"type": "module",

routes/routes.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,18 @@ router.post(
414414
* type: string
415415
* description: La tipologia di Entity
416416
* example: "municipality"
417+
* - name: limit
418+
* in: query
419+
* schema:
420+
* type: number
421+
* description: Paginazione - quantità di risultati restituiti per pagina
422+
* example: 10
423+
* - name: page
424+
* in: query
425+
* schema:
426+
* type: number
427+
* description: Paginazione - pagina da visualizzare
428+
* example: 5
417429
* responses:
418430
* "200":
419431
* description: OK
@@ -613,11 +625,8 @@ router.get(
613625
const dateFrom = req.query.dateFrom;
614626
const dateTo = req.query.dateTo;
615627

616-
const page =
617-
req.query.page === "0" || req.query.page === undefined
618-
? "1"
619-
: req.query.page;
620-
const limit = req.query.limit ?? "0";
628+
const page = req.query.page ?? "0";
629+
const limit = req.query.limit ?? "-1";
621630

622631
const result = await new jobController(dbWS).list(
623632
externalEntityId,
@@ -944,9 +953,9 @@ router.post(
944953
try {
945954
await jwtVerify(process.env.JWT_SECRET, await getToken(req));
946955

947-
const page = req.query.page;
948-
956+
const page = req.query.page ?? "0";
949957
const limit = req.query.limit ?? "-1";
958+
950959
const countOnly = req.query.countOnly
951960
? req.query.countOnly == "true"
952961
: false;

0 commit comments

Comments
 (0)