Guest User

Untitled

a guest
Oct 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. require("dotenv").config();
  2.  
  3. const bodyParser = require("body-parser");
  4. const pgp = require("pg-promise")();
  5. const express = require("express");
  6. const app = express();
  7. const db = pgp({
  8. host: "localhost",
  9. port: 5432,
  10. database: process.env.DB_NAME,
  11. user: process.env.DB_USERNAME,
  12. password: process.env.DB_PASSWORD
  13. });
  14.  
  15. app.use(bodyParser.json());
  16.  
  17. // app.get('/api/songs', function(req, res){
  18. // db.any('SELECT * FROM song')
  19. // .then(function(data) {
  20. // res.json(data);
  21. // })
  22. // .catch(function(error) {
  23. // res.json({error: error.message});
  24. // });
  25. // });
  26.  
  27. /*
  28. app.get('/api/songs/:id', function(req, res){
  29. const id = req.params.id;
  30. db.any('SELECT * FROM song WHERE id=$1', [id])
  31. .then(function(data) {
  32. res.json(data);
  33. })
  34. .catch(function(error) {
  35. res.json({error: error.message});
  36. });
  37. });
  38. */
  39.  
  40. app.get("/api/songs/:id", function(req, res) {
  41. const id = req.params.id;
  42. db.any(
  43. `SELECT song.id,artist.name, song.title
  44. FROM song, artist
  45. WHERE artist.id = song.artist_id
  46. AND song.id = $1`,
  47. [id]
  48. )
  49. .then(function(data) {
  50. res.json(data);
  51. })
  52. .catch(function(error) {
  53. res.json({ error: error.message });
  54. });
  55. });
  56.  
  57. app.post("/api/songs", function(req, res) {
  58. // using destructing assignment to
  59. // extract properties into variables
  60. const { artistId, title, year } = req.body;
  61. // ES6 strings for multiline
  62. db.one(
  63. `INSERT INTO song(artist_id, title, year)
  64. VALUES($1, $2, $3) RETURNING id`,
  65. [artistId, title, year]
  66. )
  67. .then(data => {
  68. db.one(
  69. `SELECT song.id, song.title, artist.name AS artist
  70. FROM song, artist
  71. WHERE artist.id = song.artist_id
  72. AND song.id = $1`,
  73. [data.id]
  74. ).then(data => {
  75. res.json(Object.assign({}, { id: data.id }, req.body));
  76. });
  77. // let's combine returned id with submitted data and
  78. // return object with id to user
  79. })
  80. .catch(error => {
  81. res.json({
  82. error: error.message
  83. });
  84. });
  85. });
  86.  
  87. app.get("/api/artists", function(req, res) {
  88. db.any("SELECT * FROM artist")
  89. .then(function(data) {
  90. res.json(data);
  91. })
  92. .catch(function(error) {
  93. res.json({ error: error.message });
  94. });
  95. });
  96.  
  97. app.post("/api/artists", function(req, res) {
  98. const { name, email } = req.body;
  99. // ES6 strings for multiline
  100. db.one(
  101. `INSERT INTO artist (name, email)
  102. VALUES($1, $2) RETURNING id`,
  103. [name, email]
  104. )
  105. .then(data => {
  106. res.json(Object.assign({}, { id: data.id }, req.body));
  107. })
  108. .catch(error => {
  109. res.json({
  110. error: error.message
  111. });
  112. });
  113. });
  114.  
  115. // Add new playlist to the db
  116. app.post("/api/playlists", function(req, res) {
  117. const { name } = req.body;
  118. db.one(
  119. `INSERT INTO playlist (name)
  120. VALUES($1) RETURNING id`,
  121. [name]
  122. )
  123. .then(data => {
  124. res.json(Object.assign({}, { id: data.id }, req.body));
  125. })
  126. .catch(error => {
  127. res.json({
  128. error: error.message
  129. });
  130. });
  131. });
  132.  
  133. // Posting a song to a playlist
  134.  
  135. app.post("/api/playlists/:playlistId/songs", function(req, res) {
  136. const { songId } = req.body;
  137. const playlistId = req.params.playlistId;
  138.  
  139. db.one(
  140. `INSERT INTO song_playlist (song_id, playlist_id) VALUES ($1, $2) RETURNING id`,
  141. [songId, playlistId]
  142. )
  143. .then(playlist => {
  144. return db
  145. .any(
  146. `SELECT playlist.name, song.title, artist.name
  147. FROM song_playlist, playlist, artist, song
  148. WHERE song_playlist.playlist_id=$1
  149. AND song_playlist.song_id = song.id
  150. AND song.artist_id = artist.id
  151. AND song_playlist.playlist_id = playlist.id`,
  152. [playlistId, playlist]
  153. )
  154. .then(data => res.json(data));
  155. })
  156. .catch(error => {
  157. res.status(404).json({
  158. error: error.message
  159. });
  160. });
  161. });
  162.  
  163. app.get("/api/songs", function(req, res) {
  164. db.any(
  165. "SELECT song.id, song.title, song.year, artist.name AS artist FROM song, artist WHERE artist.id = song.artist_id"
  166. )
  167. .then(function(data) {
  168. res.json(data);
  169. })
  170. .catch(function(error) {
  171. res.json({ error: error.message });
  172. });
  173. });
  174.  
  175. app.get("/api/playlists", function(req, res) {
  176. db.any("SELECT * FROM playlist")
  177. .then(function(data) {
  178. res.json(data);
  179. })
  180. .catch(function(error) {
  181. res.json({ error: error.message });
  182. });
  183. });
  184.  
  185. // DELETE /playlists/:id/songs/:songId should delete the song with songId from the relevant playlist
  186.  
  187. app.delete("/api/playlists/:id/songs/:songId", function(req, res) {
  188. db.any(
  189. `
  190. DELETE FROM song_playlist
  191. WHERE
  192. song_playlist.song_id = $1
  193. AND song_playlist.playlist_id = $2`,
  194. [req.params.songId, req.params.id]
  195. )
  196. .then(function(data) {
  197. res.json(
  198. `DELETED song ID ${req.params.songId} of ${req.params.id} playlist`
  199. );
  200. })
  201. .catch(function(error) {
  202. res.json({ error: error.message });
  203. });
  204. });
  205.  
  206. // DELETE /playlists/:id should delete the playlist with relevant id from the database. Before deleting a playlist, you will need to delete all rows from song_playlist table that reference the playlist being deleted.
  207.  
  208. app.delete("/api/playlists/:id", function(req, res) {
  209. db.one(`SELECT name FROM playlist WHERE id = $1`, [req.params.id])
  210. .then(data => {
  211. db.any(
  212. `DELETE FROM song_playlist WHERE song_playlist.playlist_id = $1; DELETE FROM playlist WHERE id = $1`,
  213. [req.params.id]
  214. );
  215. return data;
  216. })
  217. .then(function(playlistName) {
  218. res.json(`Destroyed your ${playlistName.name} playlist`);
  219. })
  220. .catch(function(error) {
  221. res.json({ error: error.message });
  222. });
  223. });
  224.  
  225. // PATCH /artists/:id should receive an artist object with name and/or email and update the corresponding entry in the database
  226. app
  227. .patch("/api/artists/:id", function(req, res) {
  228. if (req.body.name) {
  229. db.one(
  230. `UPDATE artist SET name = $2 WHERE id = $1 RETURNING name`,
  231. [req.params.id, req.body.name ]
  232. )
  233. .then(({ name }) =>
  234. res.json(
  235. `${name} is your artist's new name`
  236. )
  237. )
  238. .catch(function(error) {
  239. res.json({ error: error.message });
  240. })
  241. }
  242. if (req.body.email) {
  243. db.one(
  244. `UPDATE artist SET email = $2 WHERE id = $1 RETURNING email`,
  245. [req.params.id ,req.body.email]
  246. )
  247. .then(({email }) =>
  248. res.json(
  249. `Your artist's new email address is ${email}`
  250. )
  251. )
  252. .catch(function(error) {
  253. res.json({ error: error.message });
  254. })
  255. }
  256.  
  257. })
  258.  
  259.  
  260. app.listen(8080, function() {
  261. console.log("Listening on port 8080!");
  262. });
Add Comment
Please, Sign In to add comment