Skip to content

Commit

Permalink
Merge pull request #49 from Dreallers/CRUD_commentaireFilm_et_Serie
Browse files Browse the repository at this point in the history
Crud commentaire film et serie
  • Loading branch information
Dreallers authored Jan 5, 2024
2 parents 3812b1a + 868355b commit 170a50f
Show file tree
Hide file tree
Showing 18 changed files with 362 additions and 166 deletions.
194 changes: 60 additions & 134 deletions backend/database/schema.sql

Large diffs are not rendered by default.

Binary file added backend/public/assets/images/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions backend/src/controllers/categorieParFilmControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const tables = require("../tables");

const browse = async (req, res, next) => {
try {
const categorieParFilm = await tables.Categorie_par_film.readAll();
const categorieParFilm = await tables.categorie_par_film.readAll();
res.json(categorieParFilm);
} catch (err) {
next(err);
Expand All @@ -15,7 +15,7 @@ const browseCategoriesForSpecificFilm = async (request, response, next) => {
try {
// Fetch all items from the database
const categories =
await tables.Categorie_par_film.readAllCategoriesForSpecificFilm(
await tables.categorie_par_film.readAllCategoriesForSpecificFilm(
request.params.id
);

Expand All @@ -31,7 +31,7 @@ const browseFilmsForSpecificCategorie = async (request, response, next) => {
try {
// Fetch all items from the database
const films =
await tables.Categorie_par_film.readAllFilmsForSpecificCategorie(
await tables.categorie_par_film.readAllFilmsForSpecificCategorie(
request.params.id
);

Expand All @@ -49,14 +49,14 @@ const add = async (request, response, next) => {

try {
// Insert the new item into the database
const result = await tables.Categorie_par_film.create({
const result = await tables.categorie_par_film.create({
filmId,
categorieId,
});

if (result.affectedRows) {
const categorieParFilm =
await tables.Categorie_par_film.readAllFilmsForSpecificCategorie(
await tables.categorie_par_film.readAllFilmsForSpecificCategorie(
categorieId
);

Expand All @@ -82,7 +82,7 @@ const destroy = async (req, response, next) => {
const { categorieId } = req.body;
try {
// Delete the item from the database
const result = await tables.Categorie_par_film.delete({
const result = await tables.categorie_par_film.delete({
filmId,
categorieId,
});
Expand Down
12 changes: 6 additions & 6 deletions backend/src/controllers/categorieParSerieControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const tables = require("../tables");

const browse = async (req, res, next) => {
try {
const categorieParSerie = await tables.Categorie_par_serie.readAll();
const categorieParSerie = await tables.categorie_par_serie.readAll();
res.json(categorieParSerie);
} catch (err) {
next(err);
Expand All @@ -15,7 +15,7 @@ const browseCategoriesForSpecificSerie = async (request, response, next) => {
try {
// Fetch all items from the database
const categories =
await tables.Categorie_par_serie.readAllCategoriesForSpecificSerie(
await tables.categorie_par_serie.readAllCategoriesForSpecificSerie(
request.params.id
);

Expand All @@ -31,7 +31,7 @@ const browseSeriesForSpecificCategorie = async (request, response, next) => {
try {
// Fetch all items from the database
const series =
await tables.Categorie_par_serie.readAllSeriesForSpecificCategorie(
await tables.categorie_par_serie.readAllSeriesForSpecificCategorie(
request.params.id
);

Expand All @@ -49,14 +49,14 @@ const add = async (request, response, next) => {

try {
// Insert the new item into the database
const result = await tables.Categorie_par_serie.create({
const result = await tables.categorie_par_serie.create({
serieId,
categorieId,
});

if (result.affectedRows) {
const categorieParSerie =
await tables.Categorie_par_serie.readAllSeriesForSpecificCategorie(
await tables.categorie_par_serie.readAllSeriesForSpecificCategorie(
categorieId
);
response.status(200).json(categorieParSerie);
Expand All @@ -81,7 +81,7 @@ const destroy = async (req, response, next) => {
const { categorieId } = req.body;
try {
// Delete the item from the database
const result = await tables.Categorie_par_serie.delete({
const result = await tables.categorie_par_serie.delete({
serieId,
categorieId,
});
Expand Down
71 changes: 71 additions & 0 deletions backend/src/controllers/commentaireParFilmControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const tables = require("../tables");

const read = async (req, res, next) => {
try {
const commentaire = await tables.commentaire_film.read(req.params.id);
if (commentaire == null) {
res.sendStatus(404);
} else {
res.json(commentaire);
}
} catch (err) {
next(err);
}
};

const edit = async (req, res, next) => {
try {
const result = await tables.commentaire_film.update(req);
if (result) {
try {
const newComment = await tables.commentaire_film.readOne(req.params.id);
if (newComment) {
res.json(newComment);
}
res.sendStatus(404);
} catch (err) {
next(err);
}
}
res.sendStatus(404);
} catch (err) {
next(err);
}
};

const add = async (req, res, next) => {
const commentaire = req.body;

try {
const insertId = await tables.commentaire_film.create(commentaire);
res.status(200).json({ insertId });
} catch (err) {
next(err);
}
};

const destroy = async (req, res, next) => {
const commentaireId = req.params.id;

try {
const deletedRows = await tables.commentaire_film.delete({
id: commentaireId,
});

if (deletedRows > 0) {
res.status(200).json({ message: "Commentaire supprimé avec succès." });
} else {
res.status(404).json({ message: "Commentaire non trouvé." });
}
} catch (err) {
console.error("Erreur lors de la suppression du commentaire :", err);
next(err);
}
};

module.exports = {
read,
edit,
add,
destroy,
};
73 changes: 73 additions & 0 deletions backend/src/controllers/commentaireParSerieControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const tables = require("../tables");

const read = async (req, res, next) => {
try {
const commentaire = await tables.commentaire_serie.read(req.params.id);
if (commentaire == null) {
res.sendStatus(404);
} else {
res.json(commentaire);
}
} catch (err) {
next(err);
}
};

const edit = async (req, res, next) => {
try {
const result = await tables.commentaire_serie.update(req);
if (result) {
try {
const newComment = await tables.commentaire_serie.readOne(
req.params.id
);
if (newComment) {
res.json(newComment);
}
res.sendStatus(404);
} catch (err) {
next(err);
}
}
res.sendStatus(404);
} catch (err) {
next(err);
}
};

const add = async (req, res, next) => {
const commentaire = req.body;

try {
const insertId = await tables.commentaire_serie.create(commentaire);
res.status(200).json({ insertId });
} catch (err) {
next(err);
}
};

const destroy = async (req, res, next) => {
const commentaireId = req.params.id;

try {
const deletedRows = await tables.commentaire_serie.delete({
id: commentaireId,
});

if (deletedRows > 0) {
res.status(200).json({ message: "Commentaire supprimé avec succès." });
} else {
res.status(404).json({ message: "Commentaire non trouvé." });
}
} catch (err) {
console.error("Erreur lors de la suppression du commentaire :", err);
next(err);
}
};

module.exports = {
read,
edit,
add,
destroy,
};
8 changes: 4 additions & 4 deletions backend/src/controllers/enTendanceFilmControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tables = require("../tables");
const browse = async (req, res, next) => {
try {
// Fetch all items from the database
const tendances = await tables.En_Tendance_Film.readAll();
const tendances = await tables.en_tendance_film.readAll();

// Respond with the items in JSON format
res.json(tendances);
Expand All @@ -19,7 +19,7 @@ const browse = async (req, res, next) => {
const read = async (req, res, next) => {
try {
// Fetch a specific item from the database based on the provided ID
const tendances = await tables.En_Tendance_Film.read(req.params.id);
const tendances = await tables.en_tendance_film.read(req.params.id);

// If the item is not found, respond with HTTP 404 (Not Found)
// Otherwise, respond with the item in JSON format
Expand All @@ -44,7 +44,7 @@ const add = async (req, res, next) => {

try {
// Insert the item into the database
const insertId = await tables.En_Tendance_Film.create(tendances);
const insertId = await tables.en_tendance_film.create(tendances);

// Respond with HTTP 201 (Created) and the ID of the newly inserted item
res.status(200).json({ insertId });
Expand All @@ -60,7 +60,7 @@ const destroy = async (req, res, next) => {
const { id: userId } = req.params;
const { filmId } = req.body;
try {
const [result] = await tables.En_Tendance_Film.delete(userId, filmId);
const [result] = await tables.en_tendance_film.delete(userId, filmId);
if (result.affectedRows) {
res.sendStatus(200);
} else {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/controllers/enTendanceSerieControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tables = require("../tables");
const browse = async (req, res, next) => {
try {
// Fetch all items from the database
const tendances = await tables.En_Tendance_Serie.readAll();
const tendances = await tables.en_tendance_serie.readAll();

// Respond with the items in JSON format
res.json(tendances);
Expand All @@ -19,7 +19,7 @@ const browse = async (req, res, next) => {
const read = async (req, res, next) => {
try {
// Fetch a specific item from the database based on the provided ID
const tendances = await tables.En_Tendance_Serie.read(req.params.id);
const tendances = await tables.en_tendance_serie.read(req.params.id);

// If the item is not found, respond with HTTP 404 (Not Found)
// Otherwise, respond with the item in JSON format
Expand All @@ -44,7 +44,7 @@ const add = async (req, res, next) => {

try {
// Insert the item into the database
const insertId = await tables.En_Tendance_Serie.create(tendances);
const insertId = await tables.en_tendance_serie.create(tendances);

// Respond with HTTP 201 (Created) and the ID of the newly inserted item
res.status(200).json({ insertId });
Expand All @@ -60,7 +60,7 @@ const destroy = async (req, res, next) => {
const { id: userId } = req.params;
const { serieId } = req.body;
try {
const [result] = await tables.En_Tendance_Serie.delete(userId, serieId);
const [result] = await tables.en_tendance_serie.delete(userId, serieId);
if (result.affectedRows) {
res.sendStatus(200);
} else {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/CategorieParFilmManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CategorieParFilmManager extends AbstractManager {
constructor() {
// Call the constructor of the parent class (AbstractManager)
// and pass the table name "categorieParFilm" as configuration
super({ table: "Categorie_par_film" });
super({ table: "categorie_par_film" });
}

// The C of CRUD - Create operation
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/CategorieParSerieManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CategorieParSerieManager extends AbstractManager {
constructor() {
// Call the constructor of the parent class (AbstractManager)
// and pass the table name "categorieParFilm" as configuration
super({ table: "Categorie_par_serie" });
super({ table: "categorie_par_serie" });
}

// The C of CRUD - Create operation
Expand Down
57 changes: 57 additions & 0 deletions backend/src/models/CommentaireParFilmManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const AbstractManager = require("./AbstractManager");

class CommentaireParFilmManager extends AbstractManager {
constructor() {
super({ table: "commentaire_film" });
}

async create({ userId, filmId, content }) {
const [result] = await this.database.query(
`insert into ${this.table} (userId, filmId, content) values (?,?,?)`,
[userId, filmId, content]
);
return result.insertId;
}

async readOne(id) {
const [result] = await this.database.query(
`SELECT cf.id, cf.content, cf.userId FROM ${this.table} cf WHERE id = ?`,
[id]
);
return {
Status: 200,
Message: "votre commentaire a bien été modifié",
Data: result,
};
}

async read(id) {
const [result] = await this.database.query(
`select cf.id, cf.content, cf.userId
from ${this.table} cf
inner join user u on cf.userId = u.id
inner join film f on cf.filmId = f.id
where cf.filmId = ?`,
[id]
);
return result;
}

async update(req) {
const [result] = await this.database.query(
`update ${this.table} SET content = ? WHERE id = ?`,
[req.body.content, req.params.id]
);
return result;
}

async delete(commentaireId) {
const [result] = await this.database.query(
`DELETE FROM commentaire_film WHERE id = ?`,
[commentaireId.id]
);

return result.affectedRows || 0;
}
}
module.exports = CommentaireParFilmManager;
Loading

0 comments on commit 170a50f

Please sign in to comment.