👌 IMPROVE: getPagesHelper refactoring code API

This commit is contained in:
Divlo 2020-05-01 20:41:59 +02:00
parent 56728e227d
commit c36b0a46ab
5 changed files with 65 additions and 94 deletions

View File

@ -0,0 +1,34 @@
const errorHandling = require('../utils/errorHandling');
const { serverError } = require('../config/errors');
const helperQueryNumber = require('../utils/helperQueryNumber');
const DEFAULT_OPTIONS = {
order: [['createdAt', 'DESC']]
};
/**
* @description Permet de faire un système de pagination sur un model Sequelize
* @param {Object} Object { req, res, next }
* @param {*} Model Model Sequelize
* @param {Object} options Options avec clause where etc.
*/
async function getPagesHelper({ req, res, next }, Model, options = DEFAULT_OPTIONS) {
const page = helperQueryNumber(req.query.page, 1);
const limit = helperQueryNumber(req.query.limit, 10);
const offset = (page - 1) * limit;
try {
const result = await Model.findAndCountAll({
limit,
offset,
...options
});
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);
}
}
module.exports = getPagesHelper;

View File

@ -8,6 +8,7 @@ const Categories = require('../models/categories');
const Quotes = require('../models/quotes'); const Quotes = require('../models/quotes');
const Users = require('../models/users'); const Users = require('../models/users');
const helperQueryNumber = require('../assets/utils/helperQueryNumber'); const helperQueryNumber = require('../assets/utils/helperQueryNumber');
const getPagesHelper = require('../assets/utils/getPagesHelper');
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const deleteFilesNameStartWith = require('../assets/utils/deleteFilesNameStartWith'); const deleteFilesNameStartWith = require('../assets/utils/deleteFilesNameStartWith');
const { EMAIL_INFO, FRONT_END_HOST } = require('../assets/config/config'); const { EMAIL_INFO, FRONT_END_HOST } = require('../assets/config/config');
@ -28,16 +29,11 @@ const handleEditFunction = async (res, resultFunction, { title, slug, descriptio
res.status(200).json({ message: "La fonction a bien été modifié!", result }); res.status(200).json({ message: "La fonction a bien été modifié!", result });
} }
exports.getFunctions = (req, res, next) => { exports.getFunctions = async (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 categoryId = helperQueryNumber(req.query.categoryId, 0);
let search = req.query.search; let search = req.query.search;
try { search = search.toLowerCase(); } catch {} try { search = search.toLowerCase(); } catch {};
const offset = (page - 1) * limit; const options = {
Functions.findAndCountAll({
limit,
offset,
where: { where: {
// Trie par catégorie // Trie par catégorie
... (categoryId !== 0) && { categorieId: categoryId }, ... (categoryId !== 0) && { categorieId: categoryId },
@ -57,16 +53,8 @@ exports.getFunctions = (req, res, next) => {
exclude: ["updatedAt", "utilizationForm", "article", "isOnline"] exclude: ["updatedAt", "utilizationForm", "article", "isOnline"]
}, },
order: [['createdAt', 'DESC']] order: [['createdAt', 'DESC']]
}) };
.then((result) => { return await getPagesHelper({ req, res, next }, Functions, options);
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.getFunctionBySlug = (req, res, next) => { exports.getFunctionBySlug = (req, res, next) => {
@ -275,13 +263,8 @@ exports.deleteCategory = async (req, res, next) => {
} }
} }
exports.getQuotes = (req, res, next) => { exports.getQuotes = async (req, res, next) => {
const page = helperQueryNumber(req.query.page, 1); const options = {
const limit = helperQueryNumber(req.query.limit, 10);
const offset = (page - 1) * limit;
Quotes.findAndCountAll({
limit,
offset,
where: { where: {
isValidated: 0, isValidated: 0,
}, },
@ -289,16 +272,8 @@ exports.getQuotes = (req, res, next) => {
{ model: Users, attributes: ["name", "logo"] } { model: Users, attributes: ["name", "logo"] }
], ],
order: [['createdAt', 'DESC']] order: [['createdAt', 'DESC']]
}) };
.then((result) => { return await getPagesHelper({ req, res, next }, Quotes, options);
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.putQuote = async (req, res, next) => { exports.putQuote = async (req, res, next) => {

View File

@ -1,33 +1,20 @@
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 Users = require('../models/users');
const Functions = require('../models/functions'); const Functions = require('../models/functions');
const helperQueryNumber = require('../assets/utils/helperQueryNumber'); const getPagesHelper = require('../assets/utils/getPagesHelper');
const { serverError } = require('../assets/config/errors'); const { serverError } = require('../assets/config/errors');
exports.getCommentsByFunctionId = (req, res, next) => { exports.getCommentsByFunctionId = async (req, res, next) => {
const { functionId } = req.params; const { functionId } = req.params;
const page = helperQueryNumber(req.query.page, 1); const options = {
const limit = helperQueryNumber(req.query.limit, 10);
const offset = (page - 1) * limit;
Comments.findAndCountAll({
limit,
offset,
where: { functionId }, where: { functionId },
include: [ include: [
{ model: Users, attributes: ["name", "logo"] } { model: Users, attributes: ["name", "logo"] }
], ],
order: [['createdAt', 'DESC']] order: [['createdAt', 'DESC']]
}) };
.then((result) => { return await getPagesHelper({ req, res, next }, Comments, options);
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) => { exports.postCommentsByFunctionId = async (req, res, next) => {

View File

@ -4,18 +4,14 @@ 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 helperQueryNumber = require('../assets/utils/helperQueryNumber');
const getPagesHelper = require('../assets/utils/getPagesHelper');
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
exports.getFunctions = (req, res, next) => { exports.getFunctions = async (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 categoryId = helperQueryNumber(req.query.categoryId, 0);
let search = req.query.search; let search = req.query.search;
try { search = search.toLowerCase(); } catch {} try { search = search.toLowerCase(); } catch {};
const offset = (page - 1) * limit; const options = {
Functions.findAndCountAll({
limit,
offset,
where: { where: {
isOnline: 1, isOnline: 1,
// Trie par catégorie // Trie par catégorie
@ -36,16 +32,8 @@ exports.getFunctions = (req, res, next) => {
exclude: ["updatedAt", "utilizationForm", "article", "isOnline"] exclude: ["updatedAt", "utilizationForm", "article", "isOnline"]
}, },
order: [['createdAt', 'DESC']] order: [['createdAt', 'DESC']]
}) };
.then((result) => { return await getPagesHelper({ req, res, next }, Functions, options);
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.getFunctionBySlug = (req, res, next) => { exports.getFunctionBySlug = (req, res, next) => {

View File

@ -2,15 +2,10 @@ const errorHandling = require('../assets/utils/errorHandling')
const { serverError, requiredFields } = require('../assets/config/errors'); const { serverError, requiredFields } = require('../assets/config/errors');
const Quotes = require('../models/quotes'); const Quotes = require('../models/quotes');
const Users = require('../models/users'); const Users = require('../models/users');
const helperQueryNumber = require('../assets/utils/helperQueryNumber'); const getPagesHelper = require('../assets/utils/getPagesHelper');
exports.getQuotes = (req, res, next) => { exports.getQuotes = async (req, res, next) => {
const page = helperQueryNumber(req.query.page, 1); const options = {
const limit = helperQueryNumber(req.query.limit, 10);
const offset = (page - 1) * limit;
Quotes.findAndCountAll({
limit,
offset,
where: { where: {
isValidated: 1, isValidated: 1,
}, },
@ -21,16 +16,8 @@ exports.getQuotes = (req, res, next) => {
exclude: ["isValidated"] exclude: ["isValidated"]
}, },
order: [['createdAt', 'DESC']] order: [['createdAt', 'DESC']]
}) };
.then((result) => { return await getPagesHelper({ req, res, next }, Quotes, options);
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.postQuote = (req, res, next) => { exports.postQuote = (req, res, next) => {