Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. const request = require('request');
  2. var apiParametri = {
  3. streznik: 'http://localhost:' + (process.env.PORT || 3000)
  4. };
  5. if (process.env.NODE_ENV === 'production') {
  6. apiParametri.streznik = 'https://skupina2-sp-2019-2020.herokuapp.com';
  7. }
  8.  
  9. /*GET Artists*/
  10. var getArtists = (req, res) => {
  11. const pot = '/api/artists';
  12. const parametriZahteve = {
  13. url: apiParametri.streznik + pot,
  14. method: 'GET',
  15. json: {}
  16. };
  17.  
  18. request(parametriZahteve, (napaka, odgovor, vsebina) => {
  19.  
  20. res.render('artists', {
  21. title: 'Artists - CentralMusic',
  22. artisti: vsebina
  23. });
  24. });
  25. };
  26.  
  27. var getArtist = (req, res) => {
  28. var artistId = req.params.idArtist;
  29.  
  30. const pot = '/api/artist'+ artistId;
  31. const parametriZahteve = {
  32. url: apiParametri.streznik + pot,
  33. method: 'GET',
  34. json: {}
  35. }
  36.  
  37. request(parametriZahteve, (napaka, odgovor, vsebina) => {
  38. if(napaka){
  39. if(odgovor.status === 404){
  40. res.status(404).json({
  41. "message":
  42. "Artists not found!"
  43. });
  44. }else if(odgovor.status === 500){
  45. res.status(404).json(napaka);
  46. }
  47. }
  48. res.redirect('/artists');
  49. });
  50. };
  51.  
  52. var addArtist = (req, res) => {
  53. const pot = '/api/artist/add';
  54. const novArtist = {
  55. name: req.body.name,
  56. albums: req.body.albums
  57. };
  58. const parametriZahteve = {
  59. url: apiParametri.streznik + pot,
  60. method: 'POST',
  61. json: novArtist
  62. };
  63.  
  64. request(parametriZahteve, (napaka, odgovor, vsebina) => {
  65. if(napaka){
  66. if(odgovor.status === 404){
  67. res.status(404).json({
  68. "message":
  69. "Artists not found!"
  70. });
  71. }else if(odgovor.status === 500){
  72. res.status(404).json(napaka);
  73. }
  74. }
  75. res.redirect('/artists');
  76. });
  77. };
  78.  
  79.  
  80. var izbrisiAlbum = (req, res) => {
  81.  
  82. var artistId = req.params.idArtist;
  83.  
  84. const pot = '/api/album/' + artistId;
  85.  
  86. const parametriZahteve = {
  87. url: apiParametri.streznik + pot,
  88. method: 'DELETE',
  89. json: {}
  90. };
  91.  
  92.  
  93. request(parametriZahteve, (napaka, odgovor, vsebina) => {
  94. if(napaka){
  95. if(odgovor.status === 404){
  96. res.status(404).json({
  97. "message":
  98. "Artists not found!"
  99. });
  100. }else if(odgovor.status === 500){
  101. res.status(404).json(napaka);
  102. }
  103. }
  104. res.redirect('/artists');
  105. });
  106. };
  107.  
  108. var posodobiAlbum = (req, res) => {
  109. var albumId = req.params.idAlbum;
  110.  
  111. const pot = '/api/album/' + albumId;
  112. const posodobiArtist = {
  113. name: req.body.name,
  114. albums: req.body.albums
  115. };
  116. const parametriZahteve = {
  117. url: apiParametri.streznik + pot,
  118. method: 'PUT',
  119. json: posodobiArtist
  120. };
  121.  
  122. request(parametriZahteve, (napaka, odgovor, vsebina) => {
  123. if(napaka){
  124. if(odgovor.status === 404){
  125. res.status(404).json({
  126. "message":
  127. "Artists not found!"
  128. });
  129. }else if(odgovor.status === 500){
  130. res.status(404).json(napaka);
  131. }
  132. }
  133. res.redirect('/artists');
  134. });
  135. };
  136.  
  137.  
  138. /*Tuka gi dodavame site napraveni funkcii*/
  139. module.exports = {
  140. izbrisiAlbum,
  141. posodobiAlbum,
  142. addArtist,
  143. getArtist,
  144. getArtists
  145. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement