From f07db5bbc40bd1be3a393419c53033a7ea3ce66b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Y=C3=BBki=20Vachot?= Date: Fri, 10 Dec 2021 18:20:31 +0100 Subject: [PATCH] Update: Video.create --- app-backend/controllers/video.controller.js | 51 +++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/app-backend/controllers/video.controller.js b/app-backend/controllers/video.controller.js index f44d82c..a3ee7b9 100644 --- a/app-backend/controllers/video.controller.js +++ b/app-backend/controllers/video.controller.js @@ -4,7 +4,7 @@ const {sendError, sendMessage} = require ("../config/response.config"); const {checkLogin} = require("../config/sessionJWT.config"); const {youtube, dailymotion} = require("../config/host.config"); const VideoCategories = require("../models/objects/video.categories.model"); -const Video = db.video; +const Video = db.videos; // Search Videos exports.search = (req, res) => { @@ -125,8 +125,53 @@ exports.get = (req, res) => { // Create a new Video exports.create = (req, res) => { const token = checkLogin(req, res); - if(token){ - return sendError(res, 501, -1, "Video.create not Implemented", token); + if(token && + typeof req.body.source !== 'undefined' && + typeof req.body.interest !== 'undefined' && + typeof req.params.id !== 'undefined'){ + const id = req.params.id; + Video.exists({userId: token.id, videoId: id, source: req.body.source}, function (err, docs){ + if(err){ + sendError(res, 500,100,err.message || "Some error occurred while checking if the Video already exists.", token); + } else{ + if(docs === null) { + let video; + + video = new Video({ + userId: token.id, + videoId: id, + source: req.body.source, + interest: req.body.interest, + watchedDates: [new Date()] + }); + + // Save Video in the database + video + .save(video) + .then(data => { + return sendMessage(res, 33, data, token) + }) + .catch(err => { + return sendError(res, 500,100,err.message || "Some error occurred while creating the Video.", token); + }); + } else{ + const id = docs._id.toString(); + Video.findByIdAndUpdate(id, {$push: {watchedDates: [new Date()]}}, {useFindAndModify: false}) + .then(data => { + if(data) { + return sendMessage(res, 33, {message: `Video ${id} was successfully updated.`}, token); + } else { + return sendError(res, 404, 105, `Video not found with id=${id}`, token); + } + }) + .catch(err => { + return sendError(res, 500, 100, err.message || `Some error occurred while updating the Video with id=${id}`, token); + }); + } + } + }); + } else { + return sendError(res, 500, -1, `No source or interest or id given`, token); } };