Created
December 7, 2020 11:49
-
-
Save Rooarii/33b76369e484b29ae8c59f8e1e920fb0 to your computer and use it in GitHub Desktop.
Express 5 - Méthode DELETE et suppression de 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.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.delete("/api/users/:id", (req, res) => { | |
const idUser = req.params.id; | |
connection.query( | |
"DELETE FROM users WHERE id = ?", | |
[idUser], | |
(err, results) => { | |
if (err) { | |
console.log(err); | |
res.status(500).send("😱 Error deleting an user"); | |
} else { | |
res.status(200).send("🎉 User deleted!"); | |
} | |
} | |
); | |
}); | |
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