diff --git a/packages/server/src/controllers/PlaylistsController/index.js b/packages/server/src/controllers/PlaylistsController/index.js index af0edac0..7f2ec57e 100755 --- a/packages/server/src/controllers/PlaylistsController/index.js +++ b/packages/server/src/controllers/PlaylistsController/index.js @@ -1,6 +1,8 @@ import { Controller } from "linebridge/dist/server" import { Schematized } from "../../lib" +import publishPlaylist from "./methods/publishPlaylist" + export default class PlaylistsController extends Controller { //static useMiddlewares = ["withAuthentication"] @@ -13,9 +15,35 @@ export default class PlaylistsController extends Controller { post = { "/playlist/publish": { middlewares: ["withAuthentication"], - fn: async (req, res) => { - - } + 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) + } + }) + } } } \ No newline at end of file diff --git a/packages/server/src/controllers/PlaylistsController/methods/publishPlaylist.js b/packages/server/src/controllers/PlaylistsController/methods/publishPlaylist.js index e69de29b..d536d963 100644 --- a/packages/server/src/controllers/PlaylistsController/methods/publishPlaylist.js +++ b/packages/server/src/controllers/PlaylistsController/methods/publishPlaylist.js @@ -0,0 +1,20 @@ +import { Playlist } from "../../../models" + +export default async (payload) => { + const { user_id, title, description, thumbnail, list } = payload + + const playlist = new Playlist({ + user_id, + created_at: Date.now(), + title: title ?? "Untitled", + description, + thumbnail, + list, + }) + + await playlist.save() + + global.eventBus.emit("playlist.created", playlist) + + return playlist +} \ No newline at end of file diff --git a/packages/server/src/controllers/index.js b/packages/server/src/controllers/index.js index 03a37474..56512494 100755 --- a/packages/server/src/controllers/index.js +++ b/packages/server/src/controllers/index.js @@ -10,4 +10,5 @@ export { default as StreamingController } from "./StreamingController" export { default as BadgesController } from "./BadgesController" export { default as CommentsController } from "./CommentsController" export { default as SearchController } from "./SearchController" -export { default as FeaturedEventsController } from "./FeaturedEventsController" \ No newline at end of file +export { default as FeaturedEventsController } from "./FeaturedEventsController" +export { default as PlaylistsController } from "./PlaylistsController" \ No newline at end of file