Skip to content

Commit

Permalink
Create route for fetching deals count
Browse files Browse the repository at this point in the history
  • Loading branch information
morrigan committed Mar 6, 2020
1 parent 0a194d1 commit fb55b3b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Controller/Api/NodePastDealsController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Response} from "express";
import {Response, Request} from "express";
import {ValidatedRequest} from "express-joi-validation";

import {NodePastDealsService} from "../../Services/NodePastDealsService";
Expand Down Expand Up @@ -45,4 +45,15 @@ export class NodePastDealsController {
res.status(500).json({error: "An unknown error occurred."});
}
}

public async getRecordsCount(req: Request, res: Response): Promise<void> {
try {
const nodeId = res.locals.node.id;
const result = await this.nodePastDealsService.countRecords(nodeId);
res.status(200).json(result)
} catch (e) {
logger.error(`Error occurred on fetching past deals counts in controller: ${e.message}`);
res.status(500).json({error: "An unknown error occurred."});
}
}
}
5 changes: 5 additions & 0 deletions src/Routes/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ export function createApiRoutes(
[validator.query(FetchNodePastDealsValidationSchema), passNodeData, AuthorizeUser],
controllers.nodePastDealsController.fetchNodePastDeals.bind(controllers.nodePastDealsController));

router.get(
"/user/node/pastdeals/:nodeId/count",
[passNodeData, AuthorizeUser],
controllers.nodePastDealsController.getRecordsCount.bind(controllers.nodePastDealsController));

router.post(
"/user/register",
validator.body(UserValidationSchema),
Expand Down
8 changes: 8 additions & 0 deletions src/Services/NodePastDealsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ export class NodePastDealsService {
order: [['updatedAt', orderBy]]
})
}

public async countRecords(nodeId: number) {
return await NodePastDealModel.count({
where: {
nodeId,
}
})
}
}

0 comments on commit fb55b3b

Please sign in to comment.