diff options
Diffstat (limited to 'server/controllers/api/accounts.ts')
-rw-r--r-- | server/controllers/api/accounts.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/server/controllers/api/accounts.ts b/server/controllers/api/accounts.ts new file mode 100644 index 000000000..aded581a5 --- /dev/null +++ b/server/controllers/api/accounts.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | import * as express from 'express' | ||
2 | import { getFormattedObjects } from '../../helpers/utils' | ||
3 | import { asyncMiddleware, paginationValidator, setAccountsSort, setPagination } from '../../middlewares' | ||
4 | import { accountsGetValidator, accountsSortValidator } from '../../middlewares/validators' | ||
5 | import { AccountModel } from '../../models/account/account' | ||
6 | |||
7 | const accountsRouter = express.Router() | ||
8 | |||
9 | accountsRouter.get('/', | ||
10 | paginationValidator, | ||
11 | accountsSortValidator, | ||
12 | setAccountsSort, | ||
13 | setPagination, | ||
14 | asyncMiddleware(listAccounts) | ||
15 | ) | ||
16 | |||
17 | accountsRouter.get('/:id', | ||
18 | asyncMiddleware(accountsGetValidator), | ||
19 | getAccount | ||
20 | ) | ||
21 | |||
22 | // --------------------------------------------------------------------------- | ||
23 | |||
24 | export { | ||
25 | accountsRouter | ||
26 | } | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
31 | return res.json(res.locals.account.toFormattedJSON()) | ||
32 | } | ||
33 | |||
34 | async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
35 | const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort) | ||
36 | |||
37 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
38 | } | ||