Skip to content

Commit

Permalink
Merge pull request #43 from Dreallers/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Tony97421 authored Dec 22, 2023
2 parents 2a4dd47 + 90fe6bb commit ecefd48
Show file tree
Hide file tree
Showing 85 changed files with 5,202 additions and 657 deletions.
1 change: 1 addition & 0 deletions backend/database/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require("dotenv").config();
// Get variables from .env file for database connection
const { DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME } = process.env;

Expand Down
519 changes: 468 additions & 51 deletions backend/database/schema.sql

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"db:seed": "node seed.js",
"build": "node migrate.js",
"start": "node index.js",
"test": "jest"
"test": "jest",
"prettier": "npx prettier --write ."
},
"dependencies": {
"cookie-parser": "^1.4.6",
Expand Down
Binary file removed backend/public/assets/images/favicon.png
Binary file not shown.
35 changes: 24 additions & 11 deletions backend/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require("dotenv").config();

// Import Faker library for generating fake data
const { faker } = require("@faker-js/faker");
// const { faker } = require("@faker-js/faker");

// Import database client
const database = require("./database/client");
Expand All @@ -19,16 +19,29 @@ const seed = async () => {

// Generating Seed Data

// Optional: Truncate tables (remove existing data)
await database.query("truncate item");

// Insert fake data into the 'item' table
for (let i = 0; i < 10; i += 1) {
queries.push(
database.query("insert into item(title) values (?)", [
faker.lorem.word(),
])
);
// // Optional: Truncate tables (remove existing data)
// await database.query("truncate item");
await database.query("TRUNCATE Categorie_par_film");

// // Insert fake data into the 'item' table
// for (let i = 0; i < 10; i += 1) {
// queries.push(
// database.query("insert into item(title) values (?)", [
// faker.lorem.word(),
// ])
// );
// }

// Insert fake data into the 'Categorie_par_film' table
for (let categorieIndex = 0; categorieIndex < 20; categorieIndex += 1) {
for (let filmIndex = 0; filmIndex < 20; filmIndex += 1) {
queries.push(
database.query(
"insert into Categorie_par_film(id_categorie,id_film) values (?,?)",
[categorieIndex, filmIndex]
)
);
}
}

/* ************************************************************************* */
Expand Down
44 changes: 22 additions & 22 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ const app = express();
// 4. Be sure to only have URLs in the array with domains from which you want to allow requests.
// For example: ["http://mysite.com", "http://another-domain.com"]

// const cors = require("cors");

// app.use(
// cors({
// origin: [
// process.env.FRONTEND_URL, // keep this one, after checking the value in `backend/.env`
// "http://mysite.com",
// "http://another-domain.com",
// ],
// })
// );
const cors = require("cors"); // eslint-disable-line

app.use(
cors({
origin: [
process.env.FRONTEND_URL, // keep this one, after checking the value in `backend/.env`
],
})
);
/**/

/* ************************************************************************* */

// Request Parsing: Understanding the purpose of this part
Expand All @@ -52,9 +52,9 @@ const app = express();
// Uncomment one or more of these options depending on the format of the data sent by your client:

app.use(express.json());
app.use(express.urlencoded());
app.use(express.text());
app.use(express.raw());
// app.use(express.urlencoded());
// app.use(express.text());
// app.use(express.raw());

/* ************************************************************************* */

Expand All @@ -69,7 +69,7 @@ app.use(express.raw());

// Then, require the module and use it as middleware in your Express application:

// const cookieParser = require("cookie-parser");
// const cookieParser = require("cookie-parser"); // eslint-disable-line

// app.use(cookieParser());

Expand Down Expand Up @@ -106,17 +106,17 @@ app.use("/api", router);
// 1. Uncomment the lines related to serving static files and redirecting unhandled requests.
// 2. Ensure that the `reactBuildPath` points to the correct directory where your frontend's build artifacts are located.

const reactBuildPath = `${__dirname}/../../frontend/dist`;
// const reactBuildPath = `${__dirname}/../../frontend/dist`;

// Serve react resources
// // Serve react resources

app.use(express.static(reactBuildPath));
app.use(express.static("./public"));

// Redirect unhandled requests to the react index file
// // Redirect unhandled requests to the react index file

app.get("*", (req, res) => {
res.sendFile(`${reactBuildPath}/index.html`);
});
// app.get("*", (req, res) => {
// res.sendFile(`${reactBuildPath}/index.html`);
// });

/* ************************************************************************* */

Expand Down
17 changes: 17 additions & 0 deletions backend/src/controllers/authControllers.js
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,
};
109 changes: 109 additions & 0 deletions backend/src/controllers/categorieControllers.js
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,
};
110 changes: 110 additions & 0 deletions backend/src/controllers/categorieParFilmControllers.js
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,
};
Loading

0 comments on commit ecefd48

Please sign in to comment.