generated from WildCodeSchool/create-js-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from Dreallers/dev
Dev
- Loading branch information
Showing
85 changed files
with
5,202 additions
and
657 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Binary file not shown.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const tables = require("../tables"); | ||
|
||
const login = async (req, res) => { | ||
const { email, password } = req.body; | ||
|
||
const user = await tables.user.readByEmail(email); | ||
|
||
if (user && user.password === password) { | ||
res.status(200).send(user); | ||
} else { | ||
res.status(400).send("incorrect email or password"); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
login, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Import access to database tables | ||
const tables = require("../tables"); | ||
|
||
// The B of BREAD - Browse (Read All) operation | ||
const browse = async (request, response, next) => { | ||
try { | ||
// Fetch all items from the database | ||
const categories = await tables.categorie.readAll(); | ||
|
||
// Respond with the items in JSON format | ||
response.json(categories); | ||
} catch (error) { | ||
// Pass any errors to the error-handling middleware | ||
next(error); | ||
} | ||
}; | ||
|
||
// The R of BREAD - Read operation | ||
const read = async (request, response, next) => { | ||
try { | ||
// Fetch a specific item from the database based on the provided ID | ||
const categorie = await tables.categorie.read(request.params.id); | ||
|
||
// If the item is not found, respond with HTTP 404 (Not Found) | ||
// Otherwise, respond with the item in JSON format | ||
if (categorie == null) { | ||
response.sendStatus(404); | ||
} else { | ||
response.json(categorie); | ||
} | ||
} catch (error) { | ||
// Pass any errors to the error-handling middleware | ||
next(error); | ||
} | ||
}; | ||
|
||
// The E of BREAD - Edit (Update) operation | ||
const edit = async (request, response, next) => { | ||
// Extract the ID of the item to be updated from the request parameters | ||
const { id } = request.params; | ||
|
||
// Add the ID to the item data extracted from the request body | ||
request.body.id = id; | ||
|
||
try { | ||
// Update the item in the database | ||
const result = await tables.categorie.update(request.body); | ||
|
||
// If the item is not found, respond with HTTP 404 (Not Found) | ||
// Otherwise, respond with HTTP 204 (No Content) | ||
if (result) { | ||
response.json(result); | ||
response.sendStatus(204); | ||
} else { | ||
response.sendStatus(404); | ||
} | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// The A of BREAD - Add (Create) operation | ||
const add = async (request, response, next) => { | ||
// Extract the item data from the request body | ||
const categorie = request.body; | ||
|
||
try { | ||
// Insert the item into the database | ||
const insertId = await tables.categorie.create(categorie); | ||
|
||
// Respond with HTTP 201 (Created) and the ID of the newly inserted item | ||
response.status(201).json({ insertId }); | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// The D of BREAD - Destroy (Delete) operation | ||
const destroy = async (request, response, next) => { | ||
// Extract the ID of the item to be deleted from the request parameters | ||
const { id } = request.params.id; | ||
|
||
try { | ||
// Delete the item from the database | ||
const result = await tables.categorie.delete(id); | ||
|
||
// If the item is not found, respond with HTTP 404 (Not Found) | ||
// Otherwise, respond with HTTP 200 (OK) | ||
if (result.affectedRows) { | ||
response.sendStatus(200); | ||
} else { | ||
response.sendStatus(404); | ||
} | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// Ready to export the controller functions | ||
module.exports = { | ||
browse, | ||
read, | ||
edit, | ||
add, | ||
destroy, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Import access to database tables | ||
const tables = require("../tables"); | ||
|
||
const browse = async (req, res, next) => { | ||
try { | ||
const categorieParFilm = await tables.Categorie_par_film.readAll(); | ||
res.json(categorieParFilm); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
// The B of BREAD - Browse (Read All) operation | ||
const browseCategoriesForSpecificFilm = async (request, response, next) => { | ||
try { | ||
// Fetch all items from the database | ||
const categories = | ||
await tables.Categorie_par_film.readAllCategoriesForSpecificFilm( | ||
request.params.id | ||
); | ||
|
||
// Respond with the items in JSON format | ||
response.json(categories); | ||
} catch (error) { | ||
// Pass any errors to the error-handling middleware | ||
next(error); | ||
} | ||
}; | ||
|
||
const browseFilmsForSpecificCategorie = async (request, response, next) => { | ||
try { | ||
// Fetch all items from the database | ||
const films = | ||
await tables.Categorie_par_film.readAllFilmsForSpecificCategorie( | ||
request.params.id | ||
); | ||
|
||
// Respond with the items in JSON format | ||
response.json(films); | ||
} catch (error) { | ||
// Pass any errors to the error-handling middleware | ||
next(error); | ||
} | ||
}; | ||
|
||
// The A of BREAD - Add operation | ||
const add = async (request, response, next) => { | ||
const { filmId, categorieId } = request.body; | ||
|
||
try { | ||
// Insert the new item into the database | ||
const result = await tables.Categorie_par_film.create({ | ||
filmId, | ||
categorieId, | ||
}); | ||
|
||
if (result.affectedRows) { | ||
const categorieParFilm = | ||
await tables.Categorie_par_film.readAllFilmsForSpecificCategorie( | ||
categorieId | ||
); | ||
|
||
// Fetch the newly created item from the database | ||
// const categorieParFilm = await tables.categorieParFilm.read(id); | ||
|
||
// Respond with the newly created item in JSON format | ||
response.status(200).json(categorieParFilm); | ||
} else { | ||
response | ||
.status(200) | ||
.json({ message: "no problem but element not created" }); | ||
} | ||
} catch (error) { | ||
// Pass any errors to the error-handling middleware | ||
next(error); | ||
} | ||
}; | ||
|
||
// The D of BREAD - Delete operation | ||
const destroy = async (req, response, next) => { | ||
const { id: filmId } = req.params; | ||
const { categorieId } = req.body; | ||
try { | ||
// Delete the item from the database | ||
const result = await tables.Categorie_par_film.delete({ | ||
filmId, | ||
categorieId, | ||
}); | ||
|
||
// If the item is not found, respond with HTTP 404 (Not Found) | ||
// Otherwise, respond with HTTP 204 (No Content) | ||
if (result.affectedRows) { | ||
response.sendStatus(200); | ||
} else { | ||
response.sendStatus(404); | ||
} | ||
} catch (error) { | ||
// Pass any errors to the error-handling middleware | ||
next(error); | ||
} | ||
}; | ||
|
||
// Ready to export the controller functions | ||
module.exports = { | ||
browse, | ||
browseCategoriesForSpecificFilm, | ||
browseFilmsForSpecificCategorie, | ||
add, | ||
destroy, | ||
}; |
Oops, something went wrong.