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