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.
- Loading branch information
Showing
7 changed files
with
370 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ CREATE TABLE | |
`id` int primary key auto_increment not null, | ||
`name` varchar(50) not null, | ||
`email` varchar(50) not null, | ||
`naissance` DATETIME NOT NULL, | ||
`naissance` DATE NOT NULL, | ||
`civility` BOOLEAN NOT NULL, | ||
`password` varchar(50) not null, | ||
`IsAdmin` bool not null | ||
|
@@ -159,4 +159,84 @@ CREATE TABLE | |
CONSTRAINT FK_Categorie_Par_Film_film_id FOREIGN KEY (`filmId`) REFERENCES `Film`(`id`), | ||
CONSTRAINT FK_Categorie_Par_Film_categorie_id FOREIGN KEY (`categorieId`) REFERENCES `Categorie`(`id`), | ||
PRIMARY KEY (`filmId`, `categorieId`) | ||
); | ||
); | ||
|
||
INSERT INTO | ||
`Film` (`miniature`, `title`, `duration`, `year`, `description`, `isAvailable`) | ||
VALUES | ||
( | ||
'https://m.media-amazon.com/images/M/MV5BMTc5MDE2ODcwNV5BMl5BanBnXkFtZTgwMzI2NzQ2NzM@._V1_.jpg', | ||
'Avengers: Endgame', | ||
181, | ||
'2019', | ||
'After the devastating events of Avengers: Infinity War (2018), the universe is in ruins. With the help of remaining allies, the Avengers assemble once more in order to reverse Thanos actions and restore balance to the universe.', | ||
1 | ||
), | ||
( | ||
'https://m.media-amazon.com/images/M/MV5BMjMxNjY2MDU1OV5BMl5BanBnXkFtZTgwNzY1MTUwNTM@._V1_.jpg', | ||
'Avengers: Infinity War', | ||
149, | ||
'2018', | ||
'The Avengers and their allies must be willing to sacrifice all in an attempt to defeat the powerful Thanos before his blitz of devastation and ruin puts an end to the universe.', | ||
0 | ||
), | ||
( | ||
'https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_.jpg', | ||
'The Avengers', | ||
143, | ||
'2012', | ||
'Earth mightiest heroes must come together and learn to fight as a team if they are going to stop the mischievous Loki and his alien army from enslaving humanity.', | ||
1 | ||
), | ||
( | ||
'https://static.wikia.nocookie.net/ironman/images/d/da/P170620_v_v8_ba.jpg/revision/latest?cb=20191202183622', | ||
'Iron man', | ||
126, | ||
'2008', | ||
'After being held captive in an Afghan cave, billionaire engineer Tony Stark creates a unique weaponized suit of armor to fight evil.', | ||
0 | ||
); | ||
|
||
INSERT INTO | ||
`Categorie` (`name`, `position`) | ||
VALUES | ||
('Action', 1), | ||
('Adventure', 2), | ||
('Sci-Fi', 3), | ||
('Drama', 4), | ||
('Thriller', 5), | ||
('Comedy', 6), | ||
('Crime', 7), | ||
('Fantasy', 8), | ||
('Mystery', 9), | ||
('Animation', 10), | ||
('Family', 11), | ||
('Biography', 12), | ||
('History', 13), | ||
('Horror', 14), | ||
('Music', 15), | ||
('Musical', 16), | ||
('Romance', 17), | ||
('Sport', 18), | ||
('War', 19), | ||
('Western', 20); | ||
|
||
INSERT INTO | ||
`User` (`name`, `email`,`naissance`, `civility`, `password`, `IsAdmin`) | ||
VALUES | ||
( | ||
'Aurel', | ||
'[email protected]', | ||
'1983/06/10', | ||
'0', | ||
'ggfd4554', | ||
'0' | ||
), | ||
( | ||
'Alex', | ||
'[email protected]', | ||
'1998/03/19', | ||
'0', | ||
'ggfd455', | ||
'0' | ||
); |
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,97 @@ | ||
// Import access to database tables | ||
const tables = require("../tables"); | ||
|
||
// The B of BREAD - Browse (Read All) operation | ||
const browse = async (req, res, next) => { | ||
try { | ||
// Fetch all items from the database | ||
const users = await tables.user.readAll(); | ||
|
||
// Respond with the items in JSON format | ||
res.json(users); | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// The R of BREAD - Read operation | ||
const read = async (req, res, next) => { | ||
try { | ||
// Fetch a specific item from the database based on the provided ID | ||
const user = await tables.user.read(req.params.id); | ||
|
||
// If the item is not found, respond with HTTP 404 (Not Found) | ||
// Otherwise, respond with the item in JSON format | ||
if (user == null) { | ||
res.sendStatus(404); | ||
} else { | ||
res.json(user); | ||
} | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// The E of BREAD - Edit (Update) operation | ||
// This operation is not yet implemented | ||
|
||
// eslint-disable-next-line consistent-return | ||
const edit = async (req, res, next) => { | ||
const { id } = req.params; | ||
req.body.id = id; | ||
try { | ||
const result = await tables.user.update(req.body); | ||
if (result) { | ||
res.json(result); | ||
res.status(204); | ||
} else { | ||
return res.sendStatus(404); | ||
} | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
// The A of BREAD - Add (Create) operation | ||
const add = async (req, res, next) => { | ||
// Extract the item data from the request body | ||
const user = req.body; | ||
|
||
try { | ||
// Insert the item into the database | ||
const insertId = await tables.user.create(user); | ||
|
||
// Respond with HTTP 201 (Created) and the ID of the newly inserted item | ||
res.status(201).json({ insertId }); | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// The D of BREAD - Destroy (Delete) operation | ||
// This operation is not yet implemented | ||
const destroy = async (req, res, next) => { | ||
const { id } = req.params; | ||
try { | ||
const result = await tables.user.delete(id); | ||
if (result.affectedRows) { | ||
res.sendStatus(200); | ||
} else { | ||
res.sendStatus(404); | ||
} | ||
} catch (err) { | ||
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
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
Oops, something went wrong.