added TracksController

This commit is contained in:
SrGooglo 2023-02-24 14:43:34 +00:00
parent fece97f61b
commit 9fb8282974
6 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,18 @@
import { Track } from "@models"
export default {
route: "/:id",
method: "GET",
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const track = await Track.findById(req.params.id).catch((err) => false)
if (!track) {
return res.status(404).json({
error: "Track not found"
})
}
return res.json(track)
}
}

View File

@ -0,0 +1,33 @@
import { Track } from "@models"
export default {
method: "POST",
route: "/publish",
middlewares: ["withAuthentication"],
fn: async (req, res) => {
let {
title,
thumbnail,
metadata,
source,
} = req.body
if (!title || !source) {
return res.status(400).json({
error: "title and source are required"
})
}
const track = new Track({
user_id: req.user._id.toString(),
title,
thumbnail,
metadata,
source,
})
await track.save()
return res.json(track)
}
}

View File

@ -0,0 +1,47 @@
import { Track } from "@models"
const allowedUpdateFields = [
"title",
"tags",
"thumbnail",
"source",
]
export default {
method: "PUT",
route: "/:track_id",
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const { payload } = req.body
if (!payload) {
return res.status(400).json({
message: "Payload is required"
})
}
let track = await Track.findById(req.params.track_id).catch((err) => false)
if (!track) {
return res.status(404).json({
message: "Track not found"
})
}
// check if the user is the owner of the track
if (req.user._id.toString() !== track.user_id.toString()) {
return res.status(403).json({
message: "You are not the owner of this track"
})
}
// update the track
allowedUpdateFields.forEach((key) => {
track[key] = payload[key] || track[key]
})
await track.save()
return res.json(track)
}
}

View File

@ -0,0 +1,9 @@
import { Controller } from "linebridge/dist/server"
import generateEndpointsFromDir from "linebridge/dist/server/lib/generateEndpointsFromDir"
export default class TracksController extends Controller {
static refName = "TracksController"
static useRoute = "/tracks"
httpEndpoints = generateEndpointsFromDir(__dirname + "/endpoints")
}

View File

@ -0,0 +1,25 @@
import { Track, User } from "@models"
export default async (_id) => {
if (!_id) {
throw new Error("Missing _id")
}
let track = await Track.findById(_id).catch((err) => false)
if (!track) {
throw new Error("Track not found")
}
track = track.toObject()
if (!track.metadata) {
// TODO: Get metadata from source
}
const userData = await User.findById(track.user_id).catch((err) => false)
track.artist = track.metadata?.artist ?? userData?.fullName ?? userData?.username ?? "Unknown artist"
return track
}

View File

@ -0,0 +1,24 @@
export default {
name: "Track",
collection: "tracks",
schema: {
user_id: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
source: {
type: String,
required: true,
},
metadata: {
type: Object,
},
thumbnail: {
type: String,
},
}
}