Refactor endpoints.

pull/7/head
Sander Vocke 5 years ago
parent 648b5201f7
commit e6371d7d48
  1. 18
      server/endpoints/CreateArtistEndpointHandler.ts
  2. 34
      server/endpoints/CreateSongEndpointHandler.ts
  3. 20
      server/endpoints/ListArtistsEndpointHandler.ts
  4. 25
      server/endpoints/ListSongsEndpointHandler.ts
  5. 99
      server/server.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);
})
}

@ -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);
});
}

@ -1,5 +1,11 @@
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser'); 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'); const models = require('./models');
import * as api from '../client/src/api'; import * as api from '../client/src/api';
@ -11,94 +17,11 @@ const port = process.env.PORT || 5000;
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.post(api.CreateSongEndpoint, (req: any, res: any) => { // Set up REST API endpoints
if (!api.checkCreateSongRequest(req)) { app.post(api.CreateSongEndpoint, CreateSongEndpointHandler);
console.log('Invalid CreateSong request: ' + JSON.stringify(req.body)); app.get(api.ListSongsEndpoint, ListSongsEndpointHandler);
res.sendStatus(400); app.post(api.CreateArtistEndpoint, CreateArtistEndpointHandler);
return; app.get(api.ListArtistsEndpoint, ListArtistsEndpointHandler);
}
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);
});
});
models.sequelize.sync().then(() => { models.sequelize.sync().then(() => {
app.listen(port, () => console.log(`Listening on port ${port}`)); app.listen(port, () => console.log(`Listening on port ${port}`));

Loading…
Cancel
Save