aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/blacklist.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-25 11:55:06 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commiteb08047657e739bcd9e592d76307befa3998482b (patch)
treefc309f51ece792fd4307c4af510710a853e1d6b2 /server/controllers/api/videos/blacklist.ts
parent5f04dd2f743961e0a06c29531cc3ccc9e4928d56 (diff)
downloadPeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.gz
PeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.zst
PeerTube-eb08047657e739bcd9e592d76307befa3998482b.zip
Use async/await in controllers
Diffstat (limited to 'server/controllers/api/videos/blacklist.ts')
-rw-r--r--server/controllers/api/videos/blacklist.ts48
1 files changed, 23 insertions, 25 deletions
diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts
index 66311598e..5a2c3fd80 100644
--- a/server/controllers/api/videos/blacklist.ts
+++ b/server/controllers/api/videos/blacklist.ts
@@ -10,7 +10,8 @@ import {
10 paginationValidator, 10 paginationValidator,
11 blacklistSortValidator, 11 blacklistSortValidator,
12 setBlacklistSort, 12 setBlacklistSort,
13 setPagination 13 setPagination,
14 asyncMiddleware
14} from '../../../middlewares' 15} from '../../../middlewares'
15import { BlacklistedVideoInstance } from '../../../models' 16import { BlacklistedVideoInstance } from '../../../models'
16import { BlacklistedVideo } from '../../../../shared' 17import { BlacklistedVideo } from '../../../../shared'
@@ -21,7 +22,7 @@ blacklistRouter.post('/:videoId/blacklist',
21 authenticate, 22 authenticate,
22 ensureIsAdmin, 23 ensureIsAdmin,
23 videosBlacklistAddValidator, 24 videosBlacklistAddValidator,
24 addVideoToBlacklist 25 asyncMiddleware(addVideoToBlacklist)
25) 26)
26 27
27blacklistRouter.get('/blacklist', 28blacklistRouter.get('/blacklist',
@@ -31,14 +32,14 @@ blacklistRouter.get('/blacklist',
31 blacklistSortValidator, 32 blacklistSortValidator,
32 setBlacklistSort, 33 setBlacklistSort,
33 setPagination, 34 setPagination,
34 listBlacklist 35 asyncMiddleware(listBlacklist)
35) 36)
36 37
37blacklistRouter.delete('/:videoId/blacklist', 38blacklistRouter.delete('/:videoId/blacklist',
38 authenticate, 39 authenticate,
39 ensureIsAdmin, 40 ensureIsAdmin,
40 videosBlacklistRemoveValidator, 41 videosBlacklistRemoveValidator,
41 removeVideoFromBlacklistController 42 asyncMiddleware(removeVideoFromBlacklistController)
42) 43)
43 44
44// --------------------------------------------------------------------------- 45// ---------------------------------------------------------------------------
@@ -49,37 +50,34 @@ export {
49 50
50// --------------------------------------------------------------------------- 51// ---------------------------------------------------------------------------
51 52
52function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { 53async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
53 const videoInstance = res.locals.video 54 const videoInstance = res.locals.video
54 55
55 const toCreate = { 56 const toCreate = {
56 videoId: videoInstance.id 57 videoId: videoInstance.id
57 } 58 }
58 59
59 db.BlacklistedVideo.create(toCreate) 60 await db.BlacklistedVideo.create(toCreate)
60 .then(() => res.type('json').status(204).end()) 61 return res.type('json').status(204).end()
61 .catch(err => {
62 logger.error('Errors when blacklisting video ', err)
63 return next(err)
64 })
65} 62}
66 63
67function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { 64async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
68 db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort) 65 const resultList = await db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort)
69 .then(resultList => res.json(getFormattedObjects<BlacklistedVideo, BlacklistedVideoInstance>(resultList.data, resultList.total))) 66
70 .catch(err => next(err)) 67 return res.json(getFormattedObjects<BlacklistedVideo, BlacklistedVideoInstance>(resultList.data, resultList.total))
71} 68}
72 69
73function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { 70async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
74 const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance 71 const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance
75 72
76 blacklistedVideo.destroy() 73 try {
77 .then(() => { 74 await blacklistedVideo.destroy()
78 logger.info('Video %s removed from blacklist.', res.locals.video.uuid) 75
79 res.sendStatus(204) 76 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
80 }) 77
81 .catch(err => { 78 return res.sendStatus(204)
82 logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err) 79 } catch (err) {
83 next(err) 80 logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err)
84 }) 81 throw err
82 }
85} 83}