Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Request_screenshots/DELETE.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Request_screenshots/Get_ALL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Request_screenshots/Get_ID.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Request_screenshots/POST.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Request_screenshots/PUT.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 39 additions & 10 deletions routes/carsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,58 @@ carsRouter.get("/", (req, res) => {

// POST add a new car
carsRouter.post("/", (req, res) => {
res.json({
msg: "add a new car ... ",
})
const { carName, carYear, carImage } = req.body
db.run(
"INSERT INTO cars (carName, carYear, carImage) VALUES (?, ?, ?)",
[carName, carYear, carImage],
function (err) {
if (err) {
res.status(500).json({ error: err.message })
} else {
res.json({ id: this.lastID })
}
}
)
})

// PUT update a car based on the param id
carsRouter.put("/:id", (req, res) => {
res.json({
msg: "update a car based on its id ... ",
})
const { id } = req.params
const { carName, carYear, carImage } = req.body
db.run(
"UPDATE cars SET carName = ?, carYear = ?, carImage = ? WHERE id = ?",
[carName, carYear, carImage, id],
function (err) {
if (err) {
res.status(500).json({ error: err.message })
} else {
res.json({ changes: this.changes })
}
}
)
})

// DELETE delete a car based on the param id
carsRouter.delete("/:id", (req, res) => {
res.json({
msg: "update a car based on its id ... ",
const { id } = req.params
db.run("DELETE FROM cars WHERE id = ?", [id], function (err) {
if (err) {
res.status(500).json({ error: err.message })
} else {
res.json({ changes: this.changes })
}
})
})

// GET one car based on its id
carsRouter.get("/:id", (req, res) => {
res.json({
msg: "update a car based on its id ... ",
const { id } = req.params // obtenir l'id à partir des paramètres
db.get("SELECT * FROM cars WHERE id = ?", [id], (err, row) => {
if (err) {
res.status(500).json({ error: err.message })
} else {
res.json(row)
}
})
})

Expand Down