From e6371d7d488dd0a2bf0905b3775bd15c22eaaef6 Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Tue, 14 Jul 2020 15:13:36 +0200 Subject: [PATCH] Refactor endpoints. --- .../endpoints/CreateArtistEndpointHandler.ts | 18 ++++ server/endpoints/CreateSongEndpointHandler.ts | 34 +++++++ .../endpoints/ListArtistsEndpointHandler.ts | 20 ++++ server/endpoints/ListSongsEndpointHandler.ts | 25 +++++ server/server.ts | 99 +++---------------- 5 files changed, 108 insertions(+), 88 deletions(-) create mode 100644 server/endpoints/CreateArtistEndpointHandler.ts create mode 100644 server/endpoints/CreateSongEndpointHandler.ts create mode 100644 server/endpoints/ListArtistsEndpointHandler.ts create mode 100644 server/endpoints/ListSongsEndpointHandler.ts diff --git a/server/endpoints/CreateArtistEndpointHandler.ts b/server/endpoints/CreateArtistEndpointHandler.ts new file mode 100644 index 0000000..9351203 --- /dev/null +++ b/server/endpoints/CreateArtistEndpointHandler.ts @@ -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); + }) +} \ No newline at end of file diff --git a/server/endpoints/CreateSongEndpointHandler.ts b/server/endpoints/CreateSongEndpointHandler.ts new file mode 100644 index 0000000..cf1bba9 --- /dev/null +++ b/server/endpoints/CreateSongEndpointHandler.ts @@ -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); + }) + }) +} \ No newline at end of file diff --git a/server/endpoints/ListArtistsEndpointHandler.ts b/server/endpoints/ListArtistsEndpointHandler.ts new file mode 100644 index 0000000..b354242 --- /dev/null +++ b/server/endpoints/ListArtistsEndpointHandler.ts @@ -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); + }); +} \ No newline at end of file diff --git a/server/endpoints/ListSongsEndpointHandler.ts b/server/endpoints/ListSongsEndpointHandler.ts new file mode 100644 index 0000000..dce496d --- /dev/null +++ b/server/endpoints/ListSongsEndpointHandler.ts @@ -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); + }); +} \ No newline at end of file diff --git a/server/server.ts b/server/server.ts index 78710f9..989d48b 100644 --- a/server/server.ts +++ b/server/server.ts @@ -1,5 +1,11 @@ const express = require('express'); const bodyParser = require('body-parser'); + +import { CreateSongEndpointHandler } from './endpoints/CreateSongEndpointHandler'; +import { CreateArtistEndpointHandler } from './endpoints/CreateArtistEndpointHandler'; +import { ListSongsEndpointHandler } from './endpoints/ListSongsEndpointHandler'; +import { ListArtistsEndpointHandler } from './endpoints/ListArtistsEndpointHandler'; + const models = require('./models'); import * as api from '../client/src/api'; @@ -11,94 +17,11 @@ const port = process.env.PORT || 5000; app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); -app.post(api.CreateSongEndpoint, (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); - }) - }) -}); - -app.get(api.ListSongsEndpoint, (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); - }); -}); - -app.post(api.CreateArtistEndpoint, (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); - }) -}); - -app.get(api.ListArtistsEndpoint, (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); - }); -}); +// Set up REST API endpoints +app.post(api.CreateSongEndpoint, CreateSongEndpointHandler); +app.get(api.ListSongsEndpoint, ListSongsEndpointHandler); +app.post(api.CreateArtistEndpoint, CreateArtistEndpointHandler); +app.get(api.ListArtistsEndpoint, ListArtistsEndpointHandler); models.sequelize.sync().then(() => { app.listen(port, () => console.log(`Listening on port ${port}`));