implement get /playlist/:id endpoint

This commit is contained in:
SrGooglo 2022-12-13 11:56:38 +00:00
parent f4f4726231
commit 50fd9c7d38
2 changed files with 44 additions and 2 deletions

View File

@ -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)
}
})
}
}
}

View File

@ -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
}