From 613909861624674f8a137836fc35263e662ca82d Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Fri, 24 Feb 2023 14:44:53 +0000 Subject: [PATCH] added `PlaylistController` --- .../endpoints/deletePlaylist.js | 29 +++++++++ .../endpoints/getPlaylist.js | 22 +++++++ .../endpoints/getSelfReleases.js | 27 +++++++++ .../endpoints/publishPlaylist.js | 34 +++++++++++ .../endpoints/updatePlaylist.js | 49 +++++++++++++++ .../controllers/PlaylistsController/index.js | 59 +------------------ .../services/getPlaylist.js | 9 ++- .../services/publishPlaylist.js | 2 +- 8 files changed, 172 insertions(+), 59 deletions(-) create mode 100644 packages/server/src/controllers/PlaylistsController/endpoints/deletePlaylist.js create mode 100644 packages/server/src/controllers/PlaylistsController/endpoints/getPlaylist.js create mode 100644 packages/server/src/controllers/PlaylistsController/endpoints/getSelfReleases.js create mode 100644 packages/server/src/controllers/PlaylistsController/endpoints/publishPlaylist.js create mode 100644 packages/server/src/controllers/PlaylistsController/endpoints/updatePlaylist.js diff --git a/packages/server/src/controllers/PlaylistsController/endpoints/deletePlaylist.js b/packages/server/src/controllers/PlaylistsController/endpoints/deletePlaylist.js new file mode 100644 index 00000000..24a8f318 --- /dev/null +++ b/packages/server/src/controllers/PlaylistsController/endpoints/deletePlaylist.js @@ -0,0 +1,29 @@ +import { Playlist } from "@models" + +export default { + method: "DELETE", + route: "/:playlist_id", + middlewares: ["withAuthentication"], + fn: async (req, res) => { + const user_id = req.user._id.toString() + + let playlist = await Playlist.findById(req.params.playlist_id).catch((err) => false) + + if (!playlist) { + return res.status(404).json({ + message: "Playlist not found" + }) + } + + // check if the user is the owner of the playlist + if (user_id !== playlist.user_id.toString()) { + return res.status(403).json({ + message: "You are not the owner of this playlist" + }) + } + + await playlist.delete() + + return res.json({ message: "Playlist deleted" }) + } +} \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/endpoints/getPlaylist.js b/packages/server/src/controllers/PlaylistsController/endpoints/getPlaylist.js new file mode 100644 index 00000000..86744ce6 --- /dev/null +++ b/packages/server/src/controllers/PlaylistsController/endpoints/getPlaylist.js @@ -0,0 +1,22 @@ +import getPlaylist from "../services/getPlaylist" + +export default { + method: "GET", + route: "/data/:id", + middlewares: ["withAuthentication"], + fn: async (req, res) => { + const result = await getPlaylist({ + _id: req.params.id + }).catch((err) => { + res.status(500).json({ + error: err.message + }) + + return null + }) + + if (result) { + return res.json(result) + } + } +} \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/endpoints/getSelfReleases.js b/packages/server/src/controllers/PlaylistsController/endpoints/getSelfReleases.js new file mode 100644 index 00000000..966f0229 --- /dev/null +++ b/packages/server/src/controllers/PlaylistsController/endpoints/getSelfReleases.js @@ -0,0 +1,27 @@ +import { User, Playlist } from "@models" +import getTrackDataById from "../../TracksController/services/getTrackDataById" + +export default { + method: "GET", + route: "/self", + middlewares: ["withAuthentication"], + fn: async (req, res) => { + const user_id = req.user._id.toString() + + let playlists = await Playlist.find({ user_id }).catch((err) => false) + + if (!playlists) { + throw new Error("Playlists not found") + } + + playlists = await Promise.all(playlists.map(async (playlist) => { + playlist.list = await Promise.all(playlist.list.map(async (track_id) => { + return await getTrackDataById(track_id).catch((err) => null) + })) + + return playlist + })) + + return res.json(playlists) + } +} \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/endpoints/publishPlaylist.js b/packages/server/src/controllers/PlaylistsController/endpoints/publishPlaylist.js new file mode 100644 index 00000000..74f15915 --- /dev/null +++ b/packages/server/src/controllers/PlaylistsController/endpoints/publishPlaylist.js @@ -0,0 +1,34 @@ +import { Schematized } from "@lib" + +import publishPlaylist from "../services/publishPlaylist" + +export default { + method: "POST", + route: "/publish", + middlewares: ["withAuthentication"], + fn: Schematized({ + required: ["title", "list"], + select: ["title", "description", "thumbnail", "list"], + }, async (req, res) => { + if (typeof req.body.list === "undefined") { + return res.status(400).json({ + error: "list is required" + }) + } + + const result = await publishPlaylist({ + user_id: req.user._id.toString(), + ...req.selection + }).catch((err) => { + res.status(500).json({ + error: err.message + }) + + return null + }) + + if (result) { + return res.json(result) + } + }) +} \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/endpoints/updatePlaylist.js b/packages/server/src/controllers/PlaylistsController/endpoints/updatePlaylist.js new file mode 100644 index 00000000..dbbcac27 --- /dev/null +++ b/packages/server/src/controllers/PlaylistsController/endpoints/updatePlaylist.js @@ -0,0 +1,49 @@ +import { Playlist } from "@models" + +const allowedUpdateFields = [ + "title", + "description", + "thumbnail", + "list", +] + +export default { + method: "PUT", + route: "/:playlist_id", + middlewares: ["withAuthentication"], + fn: async (req, res) => { + const { payload } = req.body + + if (!payload) { + return res.status(400).json({ + message: "Payload is required" + }) + } + + let playlist = await Playlist.findById(req.params.playlist_id).catch((err) => false) + + if (!playlist) { + return res.status(404).json({ + message: "Playlist not found" + }) + } + + // check if the user is the owner of the playlist + if (req.user._id.toString() !== playlist.user_id.toString()) { + return res.status(403).json({ + message: "You are not the owner of this playlist" + }) + } + + console.log(payload) + + // update the playlist + allowedUpdateFields.forEach((key) => { + playlist[key] = payload[key] || playlist[key] + }) + + await playlist.save() + + return res.json(playlist) + } +} \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/index.js b/packages/server/src/controllers/PlaylistsController/index.js index ee741fc4..845b0655 100755 --- a/packages/server/src/controllers/PlaylistsController/index.js +++ b/packages/server/src/controllers/PlaylistsController/index.js @@ -1,64 +1,9 @@ import { Controller } from "linebridge/dist/server" -import { Schematized } from "@lib" - -import publishPlaylist from "./services/publishPlaylist" -import getPlaylist from "./services/getPlaylist" +import generateEndpointsFromDir from "linebridge/dist/server/lib/generateEndpointsFromDir" export default class PlaylistsController extends Controller { static refName = "PlaylistsController" static useRoute = "/playlist" - httpEndpoints = { - get: { - "/:id": async (req, res) => { - const result = await getPlaylist({ - _id: req.params.id - }).catch((err) => { - res.status(500).json({ - error: err.message - }) - - return null - }) - - if (result) { - return res.json(result) - } - } - }, - - post: { - "/publish": { - middlewares: ["withAuthentication"], - fn: Schematized({ - required: ["title", "list"], - select: ["title", "description", "thumbnail", "list"], - }, async (req, res) => { - if (typeof req.body.list === "undefined") { - return res.status(400).json({ - error: "list is required" - }) - } - - // parse - req.selection.list = JSON.parse(req.selection.list) - - const result = await publishPlaylist({ - user_id: req.user._id.toString(), - ...req.selection - }).catch((err) => { - res.status(500).json({ - error: err.message - }) - - return null - }) - - if (result) { - return res.json(result) - } - }) - } - } - } + httpEndpoints = generateEndpointsFromDir(__dirname + "/endpoints") } \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/services/getPlaylist.js b/packages/server/src/controllers/PlaylistsController/services/getPlaylist.js index bcb47094..f2f33bdc 100755 --- a/packages/server/src/controllers/PlaylistsController/services/getPlaylist.js +++ b/packages/server/src/controllers/PlaylistsController/services/getPlaylist.js @@ -1,4 +1,5 @@ -import { User, Playlist } from "../../../models" +import { User, Playlist } from "@models" +import getTrackDataById from "../../TracksController/services/getTrackDataById" export default async (payload) => { const { _id } = payload @@ -26,5 +27,11 @@ export default async (payload) => { avatar: user.avatar, } + playlist.list = await Promise.all(playlist.list.map(async (track_id) => { + return await getTrackDataById(track_id) + })) + + playlist.artist = user.fullName ?? user.username + return playlist } \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/services/publishPlaylist.js b/packages/server/src/controllers/PlaylistsController/services/publishPlaylist.js index 9123990e..ff9d1698 100755 --- a/packages/server/src/controllers/PlaylistsController/services/publishPlaylist.js +++ b/packages/server/src/controllers/PlaylistsController/services/publishPlaylist.js @@ -1,4 +1,4 @@ -import { Playlist } from "../../../models" +import { Playlist } from "@models" export default async (payload) => { const { user_id, title, description, thumbnail, list } = payload