1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 02:39:33 +02:00

Use async/await in controllers

This commit is contained in:
Chocobozzz 2017-10-25 11:55:06 +02:00
parent 5f04dd2f74
commit eb08047657
No known key found for this signature in database
GPG key ID: 583A612D890159BE
20 changed files with 823 additions and 992 deletions

View file

@ -16,7 +16,8 @@ import {
paginationValidator,
setPagination,
setPodsSort,
podsSortValidator
podsSortValidator,
asyncMiddleware
} from '../../middlewares'
import { PodInstance } from '../../models'
@ -27,25 +28,25 @@ podsRouter.get('/',
podsSortValidator,
setPodsSort,
setPagination,
listPods
asyncMiddleware(listPods)
)
podsRouter.post('/make-friends',
authenticate,
ensureIsAdmin,
makeFriendsValidator,
setBodyHostsPort,
makeFriendsController
asyncMiddleware(makeFriendsController)
)
podsRouter.get('/quit-friends',
authenticate,
ensureIsAdmin,
quitFriendsController
asyncMiddleware(quitFriendsController)
)
podsRouter.delete('/:id',
authenticate,
ensureIsAdmin,
podRemoveValidator,
removeFriendController
asyncMiddleware(removeFriendController)
)
// ---------------------------------------------------------------------------
@ -56,33 +57,33 @@ export {
// ---------------------------------------------------------------------------
function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
db.Pod.listForApi(req.query.start, req.query.count, req.query.sort)
.then(resultList => res.json(getFormattedObjects(resultList.data, resultList.total)))
.catch(err => next(err))
async function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
const resultList = await db.Pod.listForApi(req.query.start, req.query.count, req.query.sort)
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
async function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
const hosts = req.body.hosts as string[]
// Don't wait the process that could be long
makeFriends(hosts)
.then(() => logger.info('Made friends!'))
.catch(err => logger.error('Could not make friends.', err))
// Don't wait the process that could be long
res.type('json').status(204).end()
return res.type('json').status(204).end()
}
function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
quitFriends()
.then(() => res.type('json').status(204).end())
.catch(err => next(err))
async function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
await quitFriends()
return res.type('json').status(204).end()
}
function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) {
async function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) {
const pod = res.locals.pod as PodInstance
removeFriend(pod)
.then(() => res.type('json').status(204).end())
.catch(err => next(err))
await removeFriend(pod)
return res.type('json').status(204).end()
}