diff --git a/api/assets/utils/helperQueryNumber.js b/api/assets/utils/helperQueryNumber.js new file mode 100644 index 0000000..f61d5d7 --- /dev/null +++ b/api/assets/utils/helperQueryNumber.js @@ -0,0 +1,6 @@ +function helperQueryNumber(value, defaultValue) { + if (value && !isNaN(value)) return parseInt(value); + return defaultValue; +} + +module.exports = helperQueryNumber; \ No newline at end of file diff --git a/api/controllers/comments.js b/api/controllers/comments.js index 80e01f5..8dfc9a5 100644 --- a/api/controllers/comments.js +++ b/api/controllers/comments.js @@ -1,2 +1,46 @@ -const errorHandling = require('../assets/utils/errorHandling'); -const Comments = require('../models/comments'); \ No newline at end of file +const errorHandling = require('../assets/utils/errorHandling'); +const Comments = require('../models/comments'); +const Users = require('../models/users'); +const Functions = require('../models/functions'); +const helperQueryNumber = require('../assets/utils/helperQueryNumber'); + +exports.getCommentsByFunctionId = (req, res, next) => { + const { functionId } = req.params; + const page = helperQueryNumber(req.query.page, 1); + const limit = helperQueryNumber(req.query.limit, 10); + const offset = (page - 1) * limit; + Comments.findAndCountAll({ + limit, + offset, + where: { functionId }, + include: [ + { model: Users, attributes: ["name", "logo"] } + ], + order: [['createdAt', 'DESC']] + }) + .then((result) => { + const { count, rows } = result; + const hasMore = (page * limit) < count; + return res.status(200).json({ totalItems: count, hasMore, rows }); + }) + .catch((error) => { + console.log(error); + return errorHandling(next, serverError); + }); +} + +exports.postCommentsByFunctionId = async (req, res, next) => { + const { functionId } = req.params; + const { message } = req.body; + try { + const resultFunction = await Functions.findOne({ where: { id: functionId } }); + if (!resultFunction) { + return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 }); + } + await Comments.create({ message, userId: req.userId, functionId }); + return res.status(201).json({ result: "Le commentaire a bien été ajouté!" }); + } catch (error) { + console.log(error); + return errorHandling(next, serverError); + } +} \ No newline at end of file diff --git a/api/controllers/functions.js b/api/controllers/functions.js index 536d3b8..2d3f16c 100644 --- a/api/controllers/functions.js +++ b/api/controllers/functions.js @@ -3,18 +3,15 @@ const { serverError } = require('../assets/config/errors'); const Functions = require('../models/functions'); const Categories = require('../models/categories'); const functionToExecute = require('../assets/functions/functionObject'); +const helperQueryNumber = require('../assets/utils/helperQueryNumber'); const Sequelize = require('sequelize'); -function helperQueryNumber(value, defaultValue) { - if (value && !isNaN(value)) return parseInt(value); - return defaultValue; -} - exports.getFunctions = (req, res, next) => { const page = helperQueryNumber(req.query.page, 1); const limit = helperQueryNumber(req.query.limit, 10); const categoryId = helperQueryNumber(req.query.categoryId, 0); - const search = req.query.search.toLowerCase(); + let search = req.query.search; + try { search = search.toLowerCase(); } catch {} const offset = (page - 1) * limit; Functions.findAndCountAll({ limit, @@ -43,7 +40,7 @@ exports.getFunctions = (req, res, next) => { .then((result) => { const { count, rows } = result; const hasMore = (page * limit) < count; - return res.status(200).json({ totalItems: count, hasMore, rows }); + return res.status(200).json({ totalItems: count, hasMore, rows }); }) .catch((error) => { console.log(error); diff --git a/api/routes/comments.js b/api/routes/comments.js index 88563f7..08c7b76 100644 --- a/api/routes/comments.js +++ b/api/routes/comments.js @@ -1,11 +1,15 @@ const { Router } = require('express'); const commentsController = require('../controllers/comments'); +const isAuth = require('../middlewares/isAuth'); const CommentsRouter = Router(); -// CommentsRouter.route('/') +CommentsRouter.route('/:functionId') -// // Récupère les catégories -// .get(commentsController.getCategories); + // Récupère les commentaires + .get(commentsController.getCommentsByFunctionId) + + // Permet à un utilisateur de poster un commentaire sur une fonction + .post(isAuth, commentsController.postCommentsByFunctionId); module.exports = CommentsRouter; \ No newline at end of file