Created
December 7, 2020 11:14
-
-
Save Rooarii/1f661209555ba7afc232d60fac78213f to your computer and use it in GitHub Desktop.
Express 3 - 🛰️Poster des données
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Express 2 - 🚀 Lecture depuis la base de données | |
const express = require("express"); | |
const movies = require("./movies"); | |
const connection = require("../config"); | |
const port = 3000; | |
const app = express(); | |
app.use(express.json()) | |
connection.connect(function(err) { | |
if (err) { | |
console.error('error connecting: ' + err.stack); | |
return; | |
} | |
console.log('connected as id ' + connection.threadId); | |
}); | |
app.get("/", (req, res) => { | |
res.send("Welcome to my favorite movie list"); | |
}); | |
app.get("/api/movies", (req, res) => { | |
connection.query("SELECT * from movies", (err, results) => { | |
if (err) { | |
res.status(500).send("Error retrieving data"); | |
} else { | |
res.status(200).json(results); | |
} | |
}); | |
}); | |
app.get("/api/movies/:id", (req, res) => { | |
connection.query("SELECT * FROM movies", (err, movies)=>{ | |
if (err) { | |
res.status(500).send("Error retrieving data"); | |
} else { | |
const movie = movies.find((movie) => { | |
return movie.id === Number(req.params.id); | |
}); | |
if (movie) { | |
res.status(200).json(movie); | |
} else { | |
res.status(404).send("Not found"); | |
} | |
} | |
}) | |
}); | |
app.get("/api/search", (req, res) => { | |
connection.query("SELECT * FROM movies", (err,movies)=>{ | |
if (err){ | |
res.status(500).send("Error retrieving data"); | |
}else{ | |
const matchingMovies = movies.filter( | |
(movie) => movie.duration <= req.query.durationMax | |
); | |
if (matchingMovies.length > 0) { | |
res.json(matchingMovies); | |
} else { | |
res.status(404).send("No movies found for this duration"); | |
} | |
} | |
}) | |
}); | |
app.get("/api/users", (req, res) => { | |
res.status(401).send("Unauthorized"); | |
}); | |
app.post("/api/movies", (req, res) => { | |
const { title, director, year, color, duration } = req.body; | |
connection.query( | |
"INSERT INTO movies(title, director, year, color, duration) VALUES(?, ?, ?, ?, ?)", | |
[title, director, year, color, duration], | |
(err, results) => { | |
if (err) { | |
console.log(err); | |
res.status(500).send("Error saving a movie"); | |
} else { | |
res.status(200).send("Successfully saved"); | |
} | |
} | |
); | |
}); | |
app.post("/api/users", (req, res)=>{ | |
const {firstname, lastname, email} = req.body; | |
connection.query( | |
"INSERT INTO users (firstname, lastname, email) VALUES (?, ?, ?)", | |
[firstname, lastname, email], | |
(err, results) => { | |
if (err) { | |
console.log(err); | |
res.status(500).send("Error saving a user"); | |
} else { | |
res.status(200).send("Successfully saved"); | |
} | |
} | |
); | |
}) | |
app.listen(port, () => { | |
console.log(`Server is runing on http://localhost:3000`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment