From fd5d9505b0d68ba2f2f449fb9e1e718ab829e315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Y=C3=BBki=20Vachot?= Date: Sun, 12 Dec 2021 05:32:29 +0100 Subject: [PATCH] Update: Playlist.update --- .../controllers/playlist.controller.js | 96 ++++++++++++++++--- 1 file changed, 84 insertions(+), 12 deletions(-) diff --git a/app-backend/controllers/playlist.controller.js b/app-backend/controllers/playlist.controller.js index d81276a..8678d3e 100644 --- a/app-backend/controllers/playlist.controller.js +++ b/app-backend/controllers/playlist.controller.js @@ -1,6 +1,8 @@ const db = require("../models/mongodb.model"); const {sendError, sendMessage} = require ("../config/response.config"); const {checkLogin} = require("../config/sessionJWT.config"); +const {youtube, dailymotion} = require("../config/host.config"); +const {asyncRequest} = require("../config/functions.config"); const ObjectId = require('mongoose').Types.ObjectId; const Playlist = db.playlists; @@ -50,7 +52,7 @@ exports.findAll = (req, res) => { condition = playlistId ? playlistId : undefined; query._id = condition; - const userId = req.query.userId; + const userId = token.id; condition = userId ? userId : undefined; query.userId = condition; @@ -113,13 +115,76 @@ exports.findOne = (req, res) => { if(token && typeof req.params.id !== 'undefined') { const id = req.params.id; if(id && ObjectId.isValid(id)){ - Playlist.findById(id, {}) - .then(data => { - if(data){ - return sendMessage(res, 23, data, token); - } else { - return sendError(res,404,105,`Playlist not found with id=${id}`, token); + Playlist.aggregate([ + {$match: {_id: new ObjectId(id), userId: token.id, isActive: true}}, + {$unwind: '$videoIds'}, + {$project: { + userId: true, + name: true, + isActive: true, + createdAt: true, + updatedAt: true, + videoIds: {$toObjectId: '$videoIds'} + }}, + {$lookup: { + from: 'videos', + localField: 'videoIds', + foreignField: '_id', + as: 'videos' + }}, + {$unwind: '$videos'}, + {$group: { + _id: '$_id', + userId: {$first: "$userId"}, + name: {$first: "$name"}, + isActive: {$first: "$isActive"}, + createdAt: {$first: "$createdAt"}, + updatedAt: {$first: "$updatedAt"}, + videos: {$push: "$videos"} + }} + ]) + .then(async data => { + let yt_results = []; + let dm_results = []; + let yt_videoIds = ""; + let dm_videoIds = ""; + + for (const i in data[0].videos) { + if (data[0].videos[i].source === youtube.name) { + yt_videoIds = yt_videoIds + data[0].videos[i].videoId + ","; + } else if (data[0].videos[i].source === dailymotion.name) { + dm_videoIds = dm_videoIds + data[0].videos[i].videoId + ","; + } } + if (yt_videoIds !== "") { + const uri = youtube.baseAPIUrl + '/videos' + '?part=snippet&part=statistics&id=' + yt_videoIds.slice(0, -1) + '&key=' + youtube.YOUTUBE_API_KEY; + const dataVideos = await asyncRequest(uri, {}); + if (dataVideos.response.statusCode === 200 && dataVideos.body.items.length > 0) { + yt_results = dataVideos.body.items; + } + } + + if (dm_videoIds !== "") { + const uri = dailymotion.baseAPIUrl + '/videos?ids=' + dm_videoIds.slice(0, -1) + '&fields=thumbnail_480_url%2Ctitle%2Cid'; + const data = await asyncRequest(uri, {}); + const response = data.response; + const jsonBody = data.body; + if (response.statusCode === 200) { + dm_results = jsonBody.list; + } + } + for (const i in data[0].videos) { + if (data[0].videos[i].source === youtube.name) { + const obj = yt_results.filter(obj => obj.id === data[0].videos[i].videoId); + data[0].videos[i].imageUrl = obj[0].snippet.thumbnails.medium.url ? obj[0].snippet.thumbnails.medium.url : null; + data[0].videos[i].title = obj[0].snippet.title ? obj[0].snippet.title : null; + } else if (data[0].videos[i].source === dailymotion.name) { + const obj = dm_results.filter(obj => obj.id === data[0].videos[i].videoId); + data[0].videos[i].imageUrl = obj[0].thumbnail_480_url ? obj[0].thumbnail_480_url : null; + data[0].videos[i].title = obj[0].title ? obj[0].title : null; + } + } + return sendMessage(res, 12, data[0], token) }) .catch(err => { return sendError(res,500,100,err.message || `Some error occurred while finding the Playlist with id=${id}`, token); @@ -148,8 +213,12 @@ exports.update = (req, res) => { update.name = condition; const videoIds = req.body.videoIds; - condition = videoIds ? {videoIds: videoIds} : undefined; - update.$push = condition; + if(typeof videoIds !== 'undefined' && videoIds !== null){ + condition = videoIds ? {videoIds: videoIds} : undefined; + } else { + condition = undefined; + } + update.$addToSet = condition; const isActive = req.body.isActive; if(typeof isActive !== 'undefined'){ @@ -163,11 +232,14 @@ exports.update = (req, res) => { Object.keys(update).forEach(key => update[key] === undefined ? delete update[key] : {}); if(id && ObjectId.isValid(id)){ - Playlist.updateOne({_id: id, userId: token.id}, update) + Playlist.findOneAndUpdate({_id: id, userId: token.id}, update, {new: false}) .then(data => { if(data) { - //Object.keys(update).forEach(key => data[key] = update[key]); - return sendMessage(res, 24, update, token); + if(!data.videoIds.includes(videoIds)){ + return sendMessage(res, 24, update, token); + } else { + return sendError(res, 500, -1, `Video in Playlist ${data.name} already exists.`, token); + } } else { return sendError(res, 404, -1, `Playlist not found with id=${id}`, token); }