]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos.js
Server: split check params tests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos.js
index f29edac743f11ca8b62ef41b2fa5acf8609a666b..35d6979e5ecd483daf72cb9302454a2f439e14ab 100644 (file)
@@ -50,6 +50,12 @@ router.get('/',
   pagination.setPagination,
   listVideos
 )
+router.put('/:id',
+  oAuth.authenticate,
+  reqFiles,
+  validatorsVideos.videosUpdate,
+  updateVideo
+)
 router.post('/',
   oAuth.authenticate,
   reqFiles,
@@ -165,7 +171,7 @@ function addVideo (req, res, next) {
     },
 
     function sendToFriends (t, video, callback) {
-      video.toRemoteJSON(function (err, remoteVideo) {
+      video.toAddRemoteJSON(function (err, remoteVideo) {
         if (err) return callback(err)
 
         // Now we'll add the video's meta data to our friends
@@ -193,18 +199,86 @@ function addVideo (req, res, next) {
   })
 }
 
-function getVideo (req, res, next) {
-  db.Video.loadAndPopulateAuthorAndPodAndTags(req.params.id, function (err, video) {
-    if (err) return next(err)
+function updateVideo (req, res, next) {
+  const videoInstance = res.locals.video
+  const videoInfosToUpdate = req.body
+
+  waterfall([
+
+    function startTransaction (callback) {
+      db.sequelize.transaction().asCallback(function (err, t) {
+        return callback(err, t)
+      })
+    },
+
+    function findOrCreateTags (t, callback) {
+      if (videoInfosToUpdate.tags) {
+        db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t, function (err, tagInstances) {
+          return callback(err, t, tagInstances)
+        })
+      } else {
+        return callback(null, t, null)
+      }
+    },
+
+    function updateVideoIntoDB (t, tagInstances, callback) {
+      const options = { transaction: t }
+
+      if (videoInfosToUpdate.name) videoInstance.set('name', videoInfosToUpdate.name)
+      if (videoInfosToUpdate.description) videoInstance.set('description', videoInfosToUpdate.description)
+
+      // Add tags association
+      videoInstance.save(options).asCallback(function (err) {
+        return callback(err, t, tagInstances)
+      })
+    },
+
+    function associateTagsToVideo (t, tagInstances, callback) {
+      if (tagInstances) {
+        const options = { transaction: t }
+
+        videoInstance.setTags(tagInstances, options).asCallback(function (err) {
+          videoInstance.Tags = tagInstances
+
+          return callback(err, t)
+        })
+      } else {
+        return callback(null, t)
+      }
+    },
+
+    function sendToFriends (t, callback) {
+      const json = videoInstance.toUpdateRemoteJSON()
+
+      // Now we'll update the video's meta data to our friends
+      friends.updateVideoToFriends(json)
+
+      return callback(null, t)
+    }
+
+  ], function andFinally (err, t) {
+    if (err) {
+      logger.error('Cannot insert the video.')
 
-    if (!video) {
-      return res.type('json').status(204).end()
+      // Abort transaction?
+      if (t) t.rollback()
+
+      return next(err)
     }
 
-    res.json(video.toFormatedJSON())
+    // Commit transaction
+    t.commit()
+
+    // TODO : include Location of the new video -> 201
+    return res.type('json').status(204).end()
   })
 }
 
+function getVideo (req, res, next) {
+  const videoInstance = res.locals.video
+  res.json(videoInstance.toFormatedJSON())
+}
+
 function listVideos (req, res, next) {
   db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
     if (err) return next(err)
@@ -214,20 +288,9 @@ function listVideos (req, res, next) {
 }
 
 function removeVideo (req, res, next) {
-  const videoId = req.params.id
-
-  waterfall([
-    function loadVideo (callback) {
-      db.Video.load(videoId, function (err, video) {
-        return callback(err, video)
-      })
-    },
+  const videoInstance = res.locals.video
 
-    function deleteVideo (video, callback) {
-      // Informations to other pods will be sent by the afterDestroy video hook
-      video.destroy().asCallback(callback)
-    }
-  ], function andFinally (err) {
+  videoInstance.destroy().asCallback(function (err) {
     if (err) {
       logger.error('Errors when removed the video.', { error: err })
       return next(err)