From 50fd9c7d385ca7fa61a896219f0eb9607fc093b5 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Tue, 13 Dec 2022 11:56:38 +0000 Subject: [PATCH] implement get `/playlist/:id` endpoint --- .../controllers/PlaylistsController/index.js | 16 ++++++++-- .../methods/getPlaylist.js | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 packages/server/src/controllers/PlaylistsController/methods/getPlaylist.js diff --git a/packages/server/src/controllers/PlaylistsController/index.js b/packages/server/src/controllers/PlaylistsController/index.js index 7f2ec57e..ee6e75f2 100755 --- a/packages/server/src/controllers/PlaylistsController/index.js +++ b/packages/server/src/controllers/PlaylistsController/index.js @@ -2,13 +2,26 @@ import { Controller } from "linebridge/dist/server" import { Schematized } from "../../lib" import publishPlaylist from "./methods/publishPlaylist" +import getPlaylist from "./methods/getPlaylist" export default class PlaylistsController extends Controller { //static useMiddlewares = ["withAuthentication"] get = { "/playlist/: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) + } } } @@ -18,7 +31,7 @@ export default class PlaylistsController extends Controller { fn: Schematized({ required: ["title", "list"], select: ["title", "description", "thumbnail", "list"], - },async (req, res) => { + }, async (req, res) => { if (typeof req.body.list === "undefined") { return res.status(400).json({ error: "list is required" @@ -43,7 +56,6 @@ export default class PlaylistsController extends Controller { return res.json(result) } }) - } } } \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/methods/getPlaylist.js b/packages/server/src/controllers/PlaylistsController/methods/getPlaylist.js new file mode 100644 index 00000000..bcb47094 --- /dev/null +++ b/packages/server/src/controllers/PlaylistsController/methods/getPlaylist.js @@ -0,0 +1,30 @@ +import { User, Playlist } from "../../../models" + +export default async (payload) => { + const { _id } = payload + + if (!_id) { + throw new Error("Missing _id") + } + + let playlist = await Playlist.findById(_id).catch((err) => false) + + if (!playlist) { + throw new Error("Playlist not found") + } + + playlist = playlist.toObject() + + const user = await User.findById(playlist.user_id).catch((err) => false) + + if (!user) { + throw new Error("User not found") + } + + playlist.user = { + username: user.username, + avatar: user.avatar, + } + + return playlist +} \ No newline at end of file