parent
648b5201f7
commit
e6371d7d48
5 changed files with 108 additions and 88 deletions
@ -0,0 +1,18 @@ |
||||
const models = require('../models'); |
||||
import * as api from '../../client/src/api'; |
||||
|
||||
export const CreateArtistEndpointHandler = (req: any, res: any) => { |
||||
if (!api.checkCreateArtistRequest(req)) { |
||||
console.log('Invalid CreateArtist request: ' + JSON.stringify(req.body)); |
||||
res.sendStatus(400); |
||||
return; |
||||
} |
||||
const reqObject: api.CreateArtistRequest = req.body; |
||||
models.Artist.create(reqObject) |
||||
.then((artist: any) => { |
||||
const responseObject: api.CreateArtistResponse = { |
||||
id: artist.id |
||||
}; |
||||
res.status(200).send(responseObject); |
||||
}) |
||||
} |
@ -0,0 +1,34 @@ |
||||
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); |
||||
}) |
||||
}) |
||||
} |
@ -0,0 +1,20 @@ |
||||
const models = require('../models'); |
||||
import * as api from '../../client/src/api'; |
||||
|
||||
export const ListArtistsEndpointHandler = (req: any, res: any) => { |
||||
if (!api.checkListArtistsRequest(req)) { |
||||
console.log('Invalid ListArtists request: ' + JSON.stringify(req.body)); |
||||
res.sendStatus(400); |
||||
return; |
||||
} |
||||
models.Artist.findAll() |
||||
.then((artists: any[]) => { |
||||
const response: api.ListArtistsResponse = artists.map((artist: any) => { |
||||
return { |
||||
name: artist.name, |
||||
id: artist.id, |
||||
}; |
||||
}); |
||||
res.send(response); |
||||
}); |
||||
} |
@ -0,0 +1,25 @@ |
||||
const models = require('../models'); |
||||
import * as api from '../../client/src/api'; |
||||
|
||||
export const ListSongsEndpointHandler = (req: any, res: any) => { |
||||
if (!api.checkListSongsRequest(req)) { |
||||
console.log('Invalid ListSongs request: ' + JSON.stringify(req.body)); |
||||
res.sendStatus(400); |
||||
return; |
||||
} |
||||
models.Song.findAll({ |
||||
include: [models.Artist] |
||||
}) |
||||
.then((songs: any[]) => { |
||||
console.log(songs); |
||||
const response: api.ListSongsResponse = songs.map((song: any) => { |
||||
return { |
||||
title: song.title, |
||||
id: song.id, |
||||
artistName: song.Artist.name, |
||||
artistId: song.ArtistId, |
||||
}; |
||||
}); |
||||
res.send(response); |
||||
}); |
||||
} |
Loading…
Reference in new issue