added PlaylistController

This commit is contained in:
srgooglo 2022-11-12 08:57:11 +00:00
parent 25660c4370
commit 502585cd65
3 changed files with 53 additions and 4 deletions

View File

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

View File

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

View File

@ -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"
export { default as FeaturedEventsController } from "./FeaturedEventsController"
export { default as PlaylistsController } from "./PlaylistsController"