Skip to content

Instantly share code, notes, and snippets.

@Rooarii
Created December 7, 2020 11:42
Show Gist options
  • Save Rooarii/14e71d8e76add67c3e9eb8de29af8ebd to your computer and use it in GitHub Desktop.
Save Rooarii/14e71d8e76add67c3e9eb8de29af8ebd to your computer and use it in GitHub Desktop.
Express 4 - 🛸 Méthode PUT et modification des données
// 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.put("/api/users/:id", (req, res) => {
// We get the ID from the url:
const idUser = req.params.id;
// We get the data from the req.body
const newUser = req.body;
// We send a UPDATE query to the DB
connection.query(
"UPDATE users SET ? WHERE id = ?",
[newUser, idUser],
(err, results) => {
if (err) {
console.log(err);
res.status(500).send("Error updating a user");
} else {
res.status(200).send("User updated successfully 🎉");
}
}
);
});
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