You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.1 KiB
34 lines
1.1 KiB
const models = require('../models'); |
|
import * as api from '../../client/src/api'; |
|
|
|
export const CreateSongEndpointHandler = (req: any, res: any) => { |
|
if (!api.checkCreateSongRequest(req)) { |
|
console.log('Invalid CreateSong request: ' + JSON.stringify(req.body)); |
|
res.sendStatus(400); |
|
return; |
|
} |
|
const reqObject: api.CreateSongRequest = req.body; |
|
console.log("Request create song: ", reqObject); // TODO: remove |
|
// First check that the artist exists. |
|
models.Artist.findAll({ |
|
where: { id: reqObject.artistId } |
|
}) |
|
.then((artist: any[]) => { |
|
if (artist.length != 1) { |
|
console.log('Invalid CreateSong request: ' + JSON.stringify(req.body) |
|
+ ". There is no artist with id " + reqObject.artistId + "."); |
|
res.sendStatus(400); |
|
return; |
|
} |
|
models.Song.create({ |
|
title: reqObject.title, |
|
ArtistId: reqObject.artistId |
|
}) |
|
.then((song: any) => { |
|
const responseObject: api.CreateSongResponse = { |
|
id: song.id |
|
}; |
|
res.status(200).send(responseObject); |
|
}) |
|
}) |
|
} |