diff --git a/backend/database/schema.sql b/backend/database/schema.sql index 0ba9f05..0c5de57 100644 --- a/backend/database/schema.sql +++ b/backend/database/schema.sql @@ -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`) - ); \ No newline at end of file + ); + +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', + 'aurelien.emeriau@wcs.com', + '1983/06/10', + '0', + 'ggfd4554', + '0' + ), + ( + 'Alex', + 'alex@wcs.com', + '1998/03/19', + '0', + 'ggfd455', + '0' + ); diff --git a/backend/src/controllers/serieControllers.js b/backend/src/controllers/serieControllers.js index e6d53b2..af24827 100644 --- a/backend/src/controllers/serieControllers.js +++ b/backend/src/controllers/serieControllers.js @@ -23,11 +23,11 @@ const read = async (req, res, next) => { }; const edit = async (req, res, next) => { - const { id } = req.params.id; + const { id } = req.params; req.body.id = id; try { const result = await tables.serie.update(req.body); - if (result.affectedRows) { + if (result) { res.json(result); res.sendStatus(204); } else { @@ -50,7 +50,7 @@ const add = async (req, res, next) => { }; const destroy = async (req, res, next) => { - const { id } = req.params.id; + const { id } = req.params; try { const result = await tables.serie.delete(id); if (result.affectedRows) { diff --git a/backend/src/controllers/userControllers.js b/backend/src/controllers/userControllers.js new file mode 100644 index 0000000..4d2626e --- /dev/null +++ b/backend/src/controllers/userControllers.js @@ -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, +}; diff --git a/backend/src/models/SerieManager.js b/backend/src/models/SerieManager.js index 12bbf56..1139278 100644 --- a/backend/src/models/SerieManager.js +++ b/backend/src/models/SerieManager.js @@ -4,7 +4,7 @@ const AbstractManager = require("./AbstractManager"); class SerieManager extends AbstractManager { constructor() { - super({ table: "Serie" }); + super({ table: "serie" }); } async create({ @@ -19,7 +19,7 @@ class SerieManager extends AbstractManager { seasonsNumber, }) { const [result] = await this.database.query( - `INSERT INTO ${this.table} (miniature, title, videoUrl, duration, year, description, isAvailable, episodesNumber, seasonsNumber) values (?, ?, ?, ?, ?, ?, ?, ?)`, + `INSERT INTO ${this.table} (miniature, title, videoUrl, duration, year, description, isAvailable, episodesNumber, seasonsNumber) values (?, ?, ?, ?, ?, ?, ?, ?, ?)`, [ miniature, title, @@ -27,9 +27,9 @@ class SerieManager extends AbstractManager { duration, year, description, - IsVailable, - EpisodesNumber, - SeasonsNumber, + isAvailable, + episodesNumber, + seasonsNumber, ] ); return result; @@ -42,6 +42,7 @@ class SerieManager extends AbstractManager { ); return result; } + async readAll() { const [result] = await this.database.query(`select * from ${this.table} `); return result; @@ -60,26 +61,29 @@ class SerieManager extends AbstractManager { seasonsNumber, }) { const [result] = await this.database.query( - `UPDATE ${this.table} SET miniature = ?, title = ?, videoUrl = ?, duration = ?, year = ?, description = ?, IsVailable = ?, EpisodesNumber = ?, SeasonsNumber = ?, WHERE id = ?` + + `UPDATE ${this.table} SET miniature=?, title=?, videoUrl=?, duration=?, year=?, description=?, isAvailable=?, episodesNumber=?, seasonsNumber=? WHERE id=?`, + [ + miniature, + title, + videoUrl, + duration, + year, + description, + isAvailable, + episodesNumber, + seasonsNumber, + id, + ] ); - [ - id, - miniature, - title, - videoUrl, - duration, - year, - description, - IsVailable, - EpisodesNumber, - SeasonsNumber, - ]; - return result; + return result.affectedRows; + } async delete(id) { - const [result] = await this.database.query( - `delete from ${this.table} where id = ?,` - )[id]; + const result = await this.database.query( + `delete from ${this.table} where id = ?`, + [id] + ); return result; } } diff --git a/backend/src/models/UserManager.js b/backend/src/models/UserManager.js index a6d26fa..dd7fd31 100644 --- a/backend/src/models/UserManager.js +++ b/backend/src/models/UserManager.js @@ -45,7 +45,7 @@ class UserManager extends AbstractManager { async update({ name, email, naissance, civility, password, IsAdmin, id }) { // Execute the SQL UPDATE query to update a item to the "user" table const [result] = await this.database.query( - `UPDATE ${this.table} SET name=?, email=?, naissance=?, civilty=?, password=?, IsAdmin=? WHERE id=?`, + `UPDATE ${this.table} SET name=?, email=?, naissance=?, civility=?, password=?, IsAdmin=? WHERE id=?`, [name, email, naissance, civility, password, IsAdmin, id] ); @@ -58,7 +58,7 @@ class UserManager extends AbstractManager { async delete(id) { const [rows] = await this.database.query( - `DELETE FROM * from ${this.table} where id = ?`, + `DELETE from ${this.table} where id = ?`, [id] ); diff --git a/backend/src/router.js b/backend/src/router.js index 9a6239a..caf52ed 100644 --- a/backend/src/router.js +++ b/backend/src/router.js @@ -9,6 +9,7 @@ const router = express.Router(); // Import itemControllers module for handling item-related operations const categorieControllers = require("./controllers/categorieControllers"); const filmControllers = require("./controllers/filmControllers"); +const userControllers = require("./controllers/userControllers"); const serieControllers = require("./controllers/serieControllers"); const categorieParFilmControllers = require("./controllers/categorieParFilmControllers"); @@ -16,26 +17,31 @@ const categorieParFilmControllers = require("./controllers/categorieParFilmContr router.get("/categories", categorieControllers.browse); router.get("/films", filmControllers.browse); router.get("/series", serieControllers.browse); +router.get("/users", userControllers.browse); // Route to get a specific item by ID router.get("/categories/:id", categorieControllers.read); router.get("/films/:id", filmControllers.read); router.get("/series/:id", serieControllers.read); +router.get("/users/:id", userControllers.read); // Route to edit a specific item by ID router.put("/categories/:id", categorieControllers.edit); router.put("/films/:id", filmControllers.edit); router.put("/series/:id", serieControllers.edit); +router.put("/users/:id", userControllers.edit); // Route to add a new item router.post("/categories", categorieControllers.add); router.post("/films", filmControllers.add); router.post("/series", serieControllers.add); +router.post("/users", userControllers.add); // Route to delete a specific item by ID router.delete("/categories/:id", categorieControllers.destroy); router.delete("/films/:id", filmControllers.destroy); router.delete("/series/:id", serieControllers.destroy); +router.delete("/users/:id", userControllers.destroy); // Route to get all categories for a specific film router.get( diff --git a/package-lock.json b/package-lock.json index d17005d..5ccc7ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,9 @@ "version": "4.2.0", "hasInstallScript": true, "license": "MIT", + "dependencies": { + "axios": "^1.6.2" + }, "devDependencies": { "concurrently": "^8.2.0", "cross-env": "^7.0.3", @@ -70,6 +73,21 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", + "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/braces": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", @@ -246,6 +264,17 @@ "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "11.0.0", "resolved": "https://registry.npmjs.org/commander/-/commander-11.0.0.tgz", @@ -347,6 +376,14 @@ } } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", @@ -409,6 +446,38 @@ "node": ">=8" } }, + "node_modules/follow-redirects": { + "version": "1.15.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", + "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/get-caller-file": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", @@ -619,6 +688,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/mimic-fn": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz", @@ -712,6 +800,11 @@ "node": ">=0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/regenerator-runtime": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz", @@ -1133,6 +1226,21 @@ "color-convert": "^2.0.1" } }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "axios": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", + "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "requires": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "braces": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", @@ -1265,6 +1373,14 @@ "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==", "dev": true }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, "commander": { "version": "11.0.0", "resolved": "https://registry.npmjs.org/commander/-/commander-11.0.0.tgz", @@ -1326,6 +1442,11 @@ "ms": "2.1.2" } }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" + }, "eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", @@ -1376,6 +1497,21 @@ "to-regex-range": "^5.0.1" } }, + "follow-redirects": { + "version": "1.15.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", + "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==" + }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, "get-caller-file": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", @@ -1511,6 +1647,19 @@ "picomatch": "^2.3.1" } }, + "mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" + }, + "mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "requires": { + "mime-db": "1.52.0" + } + }, "mimic-fn": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz", @@ -1567,6 +1716,11 @@ "integrity": "sha512-eG2dWTVw5bzqGRztnHExczNxt5VGsE6OwTeCG3fdUf9KBsZzO3R5OIIIzWR+iZA0NtZ+RDVdaoE2dK1cn6jH4g==", "dev": true }, + "proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "regenerator-runtime": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz",