SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const connexion = require('./conf');
  3. const bodyParser = require('body-parser')
  4. const app = express();
  5. const port = 3000;
  6.  
  7. app.use(bodyParser.json());
  8.  
  9. app.use(bodyParser.urlencoded({
  10.     extended: true
  11. }));
  12.  
  13. app.delete('/api/movies/:id', (request, response) => {    
  14.     const idMovie = request.params.id;  
  15.     connexion.query('DELETE FROM movie WHERE id = ?', [idMovie], error => {  
  16.       if (error) {
  17.         console.log(error);
  18.         response.status(500).send("Erreur lors de la suppression d'un film");
  19.       } else {  
  20.         response.sendStatus(200);
  21.       }
  22.     });
  23. });
  24.  
  25. app.put('/api/movies/:id', (request, response) => {
  26.     const idMovie = request.params.id;
  27.     const formData = request.body;
  28.     connexion.query('UPDATE movie SET ? WHERE id=?',[formData,idMovie], (error,results) => {
  29.       if (err) {    
  30.         console.log(error);
  31.         response.status(500).send("Erreur lors de la modification d'un film");
  32.       } else {
  33.         response.sendStatus(200);
  34.       }
  35.     })
  36.   });
  37.  
  38. app.post('/api/movies', (request,response) => {
  39.     const formdata = request.body;
  40.     connexion.query('INSERT INTO movie SET ?', formdata, (error,results) => {
  41.         if(error) {
  42.             console.log(error);
  43.             response.status(500).send("Erreur lors de la sauvegarde d'un film");
  44.         } else {
  45.             response.sendStatus(200);
  46.         }
  47.     })
  48. })
  49.  
  50. app.get('/api/movies',(request,response) => {
  51.     connexion.query('SELECT * FROM movie', (error,results) => {
  52.         if (error) {
  53.             response.status(500).send('Impossible de récupérer les films');
  54.         } else {
  55.             response.json(results);
  56.         }
  57.     })
  58. });
  59.  
  60. app.get('/api/movies/names', (request,response) =>{
  61.     connexion.query('SELECT name FROM movie', (error,results) => {
  62.         if(error) {
  63.             response.status(500).send('Impossible de récupérer les noms des films');            
  64.         } else {
  65.             response.json(results);
  66.         }
  67.     })
  68. });
  69.  
  70. app.listen(port, (error) => {
  71.     if (error) {
  72.         throw new Error('Something bad happened...');
  73.       }    
  74.       console.log(`Server is listening on ${port}`);
  75. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top