From 7b1f49de22c40ae121ddb3c399b2540ba56fd414 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Dec 2016 19:07:05 +0100 Subject: Server: add ability to update a video --- server/middlewares/validators/videos.js | 41 +++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'server/middlewares/validators/videos.js') diff --git a/server/middlewares/validators/videos.js b/server/middlewares/validators/videos.js index 7e90ca047..09a188c76 100644 --- a/server/middlewares/validators/videos.js +++ b/server/middlewares/validators/videos.js @@ -8,6 +8,7 @@ const logger = require('../../helpers/logger') const validatorsVideos = { videosAdd, + videosUpdate, videosGet, videosRemove, videosSearch @@ -41,22 +42,26 @@ function videosAdd (req, res, next) { }) } -function videosGet (req, res, next) { +function videosUpdate (req, res, next) { req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) + req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid() + req.checkBody('description', 'Should have a valid description').optional().isVideoDescriptionValid() + req.checkBody('tags', 'Should have correct tags').optional().isVideoTagsValid() - logger.debug('Checking videosGet parameters', { parameters: req.params }) + logger.debug('Checking videosUpdate parameters', { parameters: req.body }) checkErrors(req, res, function () { - db.Video.load(req.params.id, function (err, video) { - if (err) { - logger.error('Error in videosGet request validator.', { error: err }) - return res.sendStatus(500) - } + checkVideoExists(req.params.id, res, next) + }) +} - if (!video) return res.status(404).send('Video not found') +function videosGet (req, res, next) { + req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) - next() - }) + logger.debug('Checking videosGet parameters', { parameters: req.params }) + + checkErrors(req, res, function () { + checkVideoExists(req.params.id, res, next) }) } @@ -94,3 +99,19 @@ function videosSearch (req, res, next) { // --------------------------------------------------------------------------- module.exports = validatorsVideos + +// --------------------------------------------------------------------------- + +function checkVideoExists (id, res, callback) { + db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) { + if (err) { + logger.error('Error in video request validator.', { error: err }) + return res.sendStatus(500) + } + + if (!video) return res.status(404).send('Video not found') + + res.locals.video = video + callback() + }) +} -- cgit v1.2.3