2015-06-09 10:41:40 -05:00
|
|
|
;(function () {
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
var express = require('express')
|
|
|
|
var router = express.Router()
|
2015-11-06 10:34:15 -06:00
|
|
|
var middleware = require('../../../middlewares')
|
2015-11-07 07:16:26 -06:00
|
|
|
var reqValidator = require('../../../middlewares/reqValidators').videos
|
2015-11-06 10:34:15 -06:00
|
|
|
var videos = require('../../../src/videos')
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
function listVideos (req, res, next) {
|
|
|
|
videos.list(function (err, videos_list) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
res.json(videos_list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchVideos (req, res, next) {
|
|
|
|
videos.search(req.params.name, function (err, videos_list) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
res.json(videos_list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function addVideos (req, res, next) {
|
|
|
|
videos.add({ video: req.files.input_video, data: req.body }, function (err) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
// TODO : include Location of the new video
|
|
|
|
res.sendStatus(201)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVideos (req, res, next) {
|
|
|
|
videos.get(req.params.id, function (err, video) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
if (video === null) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(video)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeVideo (req, res, next) {
|
|
|
|
videos.remove(req.params.id, function (err) {
|
2015-10-30 11:51:40 -05:00
|
|
|
if (err) return next(err)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
router.get('/', middleware.cache(false), listVideos)
|
2015-11-07 07:16:26 -06:00
|
|
|
router.post('/', reqValidator.videosAdd, middleware.cache(false), addVideos)
|
|
|
|
router.get('/search/:name', reqValidator.videosSearch, middleware.cache(false), searchVideos)
|
|
|
|
router.get('/:id', reqValidator.videosGet, middleware.cache(false), getVideos)
|
|
|
|
router.delete('/:id', reqValidator.videosRemove, middleware.cache(false), removeVideo)
|
2015-06-09 10:41:40 -05:00
|
|
|
|
|
|
|
module.exports = router
|
|
|
|
})()
|