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 #49 from Dreallers/CRUD_commentaireFilm_et_Serie
Crud commentaire film et serie
- Loading branch information
Showing
18 changed files
with
362 additions
and
166 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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, | ||
}; |
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,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, | ||
}; |
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
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,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; |
Oops, something went wrong.