mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
added PlaylistController
This commit is contained in:
parent
56aed5dd17
commit
6139098616
@ -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" })
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
@ -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")
|
||||
}
|
@ -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
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { Playlist } from "../../../models"
|
||||
import { Playlist } from "@models"
|
||||
|
||||
export default async (payload) => {
|
||||
const { user_id, title, description, thumbnail, list } = payload
|
||||
|
Loading…
x
Reference in New Issue
Block a user