backend: GET et POST /comments/:functionId
This commit is contained in:
parent
84dae869ef
commit
9152313110
6
api/assets/utils/helperQueryNumber.js
Normal file
6
api/assets/utils/helperQueryNumber.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
function helperQueryNumber(value, defaultValue) {
|
||||||
|
if (value && !isNaN(value)) return parseInt(value);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = helperQueryNumber;
|
@ -1,2 +1,46 @@
|
|||||||
const errorHandling = require('../assets/utils/errorHandling');
|
const errorHandling = require('../assets/utils/errorHandling');
|
||||||
const Comments = require('../models/comments');
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -3,18 +3,15 @@ const { serverError } = require('../assets/config/errors');
|
|||||||
const Functions = require('../models/functions');
|
const Functions = require('../models/functions');
|
||||||
const Categories = require('../models/categories');
|
const Categories = require('../models/categories');
|
||||||
const functionToExecute = require('../assets/functions/functionObject');
|
const functionToExecute = require('../assets/functions/functionObject');
|
||||||
|
const helperQueryNumber = require('../assets/utils/helperQueryNumber');
|
||||||
const Sequelize = require('sequelize');
|
const Sequelize = require('sequelize');
|
||||||
|
|
||||||
function helperQueryNumber(value, defaultValue) {
|
|
||||||
if (value && !isNaN(value)) return parseInt(value);
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.getFunctions = (req, res, next) => {
|
exports.getFunctions = (req, res, next) => {
|
||||||
const page = helperQueryNumber(req.query.page, 1);
|
const page = helperQueryNumber(req.query.page, 1);
|
||||||
const limit = helperQueryNumber(req.query.limit, 10);
|
const limit = helperQueryNumber(req.query.limit, 10);
|
||||||
const categoryId = helperQueryNumber(req.query.categoryId, 0);
|
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;
|
const offset = (page - 1) * limit;
|
||||||
Functions.findAndCountAll({
|
Functions.findAndCountAll({
|
||||||
limit,
|
limit,
|
||||||
@ -43,7 +40,7 @@ exports.getFunctions = (req, res, next) => {
|
|||||||
.then((result) => {
|
.then((result) => {
|
||||||
const { count, rows } = result;
|
const { count, rows } = result;
|
||||||
const hasMore = (page * limit) < count;
|
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) => {
|
.catch((error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
const { Router } = require('express');
|
const { Router } = require('express');
|
||||||
const commentsController = require('../controllers/comments');
|
const commentsController = require('../controllers/comments');
|
||||||
|
const isAuth = require('../middlewares/isAuth');
|
||||||
|
|
||||||
const CommentsRouter = Router();
|
const CommentsRouter = Router();
|
||||||
|
|
||||||
// CommentsRouter.route('/')
|
CommentsRouter.route('/:functionId')
|
||||||
|
|
||||||
// // Récupère les catégories
|
// Récupère les commentaires
|
||||||
// .get(commentsController.getCategories);
|
.get(commentsController.getCommentsByFunctionId)
|
||||||
|
|
||||||
|
// Permet à un utilisateur de poster un commentaire sur une fonction
|
||||||
|
.post(isAuth, commentsController.postCommentsByFunctionId);
|
||||||
|
|
||||||
module.exports = CommentsRouter;
|
module.exports = CommentsRouter;
|
Reference in New Issue
Block a user