From 483c0791ec67accc6cc3403ec51e9ba2bb754d6d Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Wed, 8 Jul 2020 11:05:03 +0200 Subject: [PATCH] Change/add endpoints --- server/server.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/server/server.js b/server/server.js index 780ced2..0c47096 100644 --- a/server/server.js +++ b/server/server.js @@ -22,22 +22,41 @@ app.post('/api/world', (req, res) => { }); app.post('/song/create', (req, res) => { - // TODO error handling - console.log('Create song: ' + req.body.title) - const song = models.Song.build({ title: req.body.title }); - console.log(song.title); + console.log('Create song: ' + JSON.stringify(req.body)) + include = []; + if ("Artist" in req.body) { + include.push(models.Artist); + } + const song = models.Song.build(req.body, { include: include }); song.save(); res.sendStatus(200); }); app.get('/song/list', (req, res) => { console.log("List songs"); - models.Song.findAll() + models.Song.findAll({ + include: [models.Artist] + }) .then(songs => { res.send(songs); }); }); +app.post('/artist/create', (req, res) => { + console.log('Create artist: ' + req.body.name) + const artist = models.Artist.build(req.body); + artist.save(); + res.sendStatus(200); +}); + +app.get('/artist/list', (req, res) => { + console.log("List artists"); + models.Artist.findAll() + .then(artists => { + res.send(artists); + }); +}); + models.sequelize.sync().then(() => { app.listen(port, () => console.log(`Listening on port ${port}`)); }) \ No newline at end of file