-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (36 loc) · 1.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const express = require('express');
const repoContext = require('./repository/repository-wrapper');
const cors = require('cors');
const {validateSong} = require('./middleware/music-validations');
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.listen(1000, function () {
console.log("Server Started. Listening on Port 1000.");
});
app.get('/api/songs', (req, res) => {
const songs = repoContext.songs.findAllSongs();
return res.send(songs);
});
app.get('/api/songs/:id', (req, res) => {
const id = req.params.id;
const song = repoContext.songs.findSongById(id);
return res.send(song);
});
app.post('/api/songs', [validateSong], (req, res) => {
const newSong = req.body;
const addedSong = repoContext.songs.createSong(newSong);
return res.send(addedSong);
});
app.put('/api/songs/:id', [validateSong], (req, res) => {
const id = req.params.id;
const songPropertiesToUpdate = req.body;
const updatedSong = repoContext.songs.updateSong(id, songPropertiesToUpdate);
return res.send(updatedSong);
});
app.delete('/api/songs/:id', (req, res) => {
const id = req.params.id;
const updatedDataSet = repoContext.songs.deleteSong(id);
return res.send(updatedDataSet);
});