-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylists.js
47 lines (43 loc) · 1.27 KB
/
playlists.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const express = require('express');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const router = express.Router();
router.get('/', async (req, res, next) => {
try{
const playlists = await prisma.playlist.findMany({
include: { owner: true, tracks: true},
});
res.json(playlists);
} catch (error) {
next(error)
}
});
router.post('/', async (req,res,next) => {
try{
const { name, description, ownerId, trackIds} = req.body;
const playlist = await prisma.playlist.create({
data: {
name,
description,
owner: { connect: {id: ownerId} },
tracks: { connect: trackIds.map((id) => ({id}))},
},
});
res.status(201).json(playlist);
}catch (error) {
next(error)
}
});
router.get('/:id', async (req, res, next) => {
try{
const playlist = await prisma.playlist.findUnique({
where: { id: parseInt(req.params.id)},
include: {tracks: true, owner: true},
});
if (!playlist) return res.status(404).json({error: 'Playlist not found?'})
res.json(playlist);
} catch (error) {
next(error)
}
});
module.exports = router;