aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/videos.js85
1 files changed, 84 insertions, 1 deletions
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js
index f29edac74..1b306d1cf 100644
--- a/server/controllers/api/videos.js
+++ b/server/controllers/api/videos.js
@@ -50,6 +50,12 @@ router.get('/',
50 pagination.setPagination, 50 pagination.setPagination,
51 listVideos 51 listVideos
52) 52)
53router.put('/:id',
54 oAuth.authenticate,
55 reqFiles,
56 validatorsVideos.videosUpdate,
57 updateVideo
58)
53router.post('/', 59router.post('/',
54 oAuth.authenticate, 60 oAuth.authenticate,
55 reqFiles, 61 reqFiles,
@@ -165,7 +171,7 @@ function addVideo (req, res, next) {
165 }, 171 },
166 172
167 function sendToFriends (t, video, callback) { 173 function sendToFriends (t, video, callback) {
168 video.toRemoteJSON(function (err, remoteVideo) { 174 video.toAddRemoteJSON(function (err, remoteVideo) {
169 if (err) return callback(err) 175 if (err) return callback(err)
170 176
171 // Now we'll add the video's meta data to our friends 177 // Now we'll add the video's meta data to our friends
@@ -193,6 +199,83 @@ function addVideo (req, res, next) {
193 }) 199 })
194} 200}
195 201
202function updateVideo (req, res, next) {
203 let videoInstance = res.locals.video
204 const videoInfosToUpdate = req.body
205
206 waterfall([
207
208 function startTransaction (callback) {
209 db.sequelize.transaction().asCallback(function (err, t) {
210 return callback(err, t)
211 })
212 },
213
214 function findOrCreateTags (t, callback) {
215 if (videoInfosToUpdate.tags) {
216 db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t, function (err, tagInstances) {
217 return callback(err, t, tagInstances)
218 })
219 } else {
220 return callback(null, t, null)
221 }
222 },
223
224 function updateVideoIntoDB (t, tagInstances, callback) {
225 const options = { transaction: t }
226
227 if (videoInfosToUpdate.name) videoInstance.set('name', videoInfosToUpdate.name)
228 if (videoInfosToUpdate.description) videoInstance.set('description', videoInfosToUpdate.description)
229
230 // Add tags association
231 videoInstance.save(options).asCallback(function (err) {
232 if (err) return callback(err)
233
234 return callback(err, t, tagInstances)
235 })
236 },
237
238 function associateTagsToVideo (t, tagInstances, callback) {
239 if (tagInstances) {
240 const options = { transaction: t }
241
242 videoInstance.setTags(tagInstances, options).asCallback(function (err) {
243 videoInstance.Tags = tagInstances
244
245 return callback(err, t)
246 })
247 } else {
248 return callback(null, t)
249 }
250 },
251
252 function sendToFriends (t, callback) {
253 const json = videoInstance.toUpdateRemoteJSON()
254
255 // Now we'll update the video's meta data to our friends
256 friends.updateVideoToFriends(json)
257
258 return callback(null, t)
259 }
260
261 ], function andFinally (err, t) {
262 if (err) {
263 logger.error('Cannot insert the video.')
264
265 // Abort transaction?
266 if (t) t.rollback()
267
268 return next(err)
269 }
270
271 // Commit transaction
272 t.commit()
273
274 // TODO : include Location of the new video -> 201
275 return res.type('json').status(204).end()
276 })
277}
278
196function getVideo (req, res, next) { 279function getVideo (req, res, next) {
197 db.Video.loadAndPopulateAuthorAndPodAndTags(req.params.id, function (err, video) { 280 db.Video.loadAndPopulateAuthorAndPodAndTags(req.params.id, function (err, video) {
198 if (err) return next(err) 281 if (err) return next(err)