backend: GET et POST /comments/:functionId

This commit is contained in:
Divlo 2020-04-09 14:31:33 +02:00
parent 84dae869ef
commit 9152313110
4 changed files with 63 additions and 12 deletions

View File

@ -0,0 +1,6 @@
function helperQueryNumber(value, defaultValue) {
if (value && !isNaN(value)) return parseInt(value);
return defaultValue;
}
module.exports = helperQueryNumber;

View File

@ -1,2 +1,46 @@
const errorHandling = require('../assets/utils/errorHandling');
const Comments = require('../models/comments');
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);
}
}

View File

@ -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);

View File

@ -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;