Adrien BĂ©raud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame^] | 1 | const express = require('express'); |
| 2 | |
| 3 | class JamiRestApi { |
| 4 | constructor(jami) { |
| 5 | this.jami = jami |
| 6 | } |
| 7 | |
| 8 | getRouter() { |
| 9 | const router = express.Router({mergeParams: true}); |
| 10 | |
| 11 | // Accounts |
| 12 | router.get(['/accounts'], (req, res, next) => { |
| 13 | console.log("Request account list") |
| 14 | res.json(this.jami.getAccountList().map(account => account.getSummary())) |
| 15 | }); |
| 16 | |
| 17 | router.get(['/accounts/:accountId'], (req, res, next) => { |
| 18 | console.log(`Request account ${req.params.accountId}`) |
| 19 | const account = this.jami.getAccount(req.params.accountId); |
| 20 | if (account) |
| 21 | res.json(account.getObject()) |
| 22 | else |
| 23 | res.sendStatus(404) |
| 24 | }); |
| 25 | |
| 26 | // Conversations |
| 27 | const conversationRouter = express.Router({mergeParams: true}); |
| 28 | conversationRouter.get('/', (req, res, next) => { |
| 29 | console.log(`Request conversations for account ${req.params.accountId}`); |
| 30 | res.json([]); |
| 31 | }) |
| 32 | |
| 33 | conversationRouter.get('/:conversationId', (req, res, next) => { |
| 34 | console.log(`Request conversation ${req.params.conversationId} for account ${req.params.accountId}`); |
| 35 | res.json({}); |
| 36 | }) |
| 37 | |
| 38 | router.use('/accounts/:accountId/conversations', conversationRouter); |
| 39 | return router; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | module.exports = JamiRestApi; |