👌 IMPROVE: getPagesHelper refactoring code API
This commit is contained in:
parent
56728e227d
commit
c36b0a46ab
34
api/assets/utils/getPagesHelper.js
Normal file
34
api/assets/utils/getPagesHelper.js
Normal 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;
|
@ -8,6 +8,7 @@ const Categories = require('../models/categories');
|
||||
const Quotes = require('../models/quotes');
|
||||
const Users = require('../models/users');
|
||||
const helperQueryNumber = require('../assets/utils/helperQueryNumber');
|
||||
const getPagesHelper = require('../assets/utils/getPagesHelper');
|
||||
const Sequelize = require('sequelize');
|
||||
const deleteFilesNameStartWith = require('../assets/utils/deleteFilesNameStartWith');
|
||||
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 });
|
||||
}
|
||||
|
||||
exports.getFunctions = (req, res, next) => {
|
||||
const page = helperQueryNumber(req.query.page, 1);
|
||||
const limit = helperQueryNumber(req.query.limit, 10);
|
||||
exports.getFunctions = async (req, res, next) => {
|
||||
const categoryId = helperQueryNumber(req.query.categoryId, 0);
|
||||
let search = req.query.search;
|
||||
try { search = search.toLowerCase(); } catch {}
|
||||
const offset = (page - 1) * limit;
|
||||
Functions.findAndCountAll({
|
||||
limit,
|
||||
offset,
|
||||
try { search = search.toLowerCase(); } catch {};
|
||||
const options = {
|
||||
where: {
|
||||
// Trie par catégorie
|
||||
... (categoryId !== 0) && { categorieId: categoryId },
|
||||
@ -57,16 +53,8 @@ exports.getFunctions = (req, res, next) => {
|
||||
exclude: ["updatedAt", "utilizationForm", "article", "isOnline"]
|
||||
},
|
||||
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);
|
||||
});
|
||||
};
|
||||
return await getPagesHelper({ req, res, next }, Functions, options);
|
||||
}
|
||||
|
||||
exports.getFunctionBySlug = (req, res, next) => {
|
||||
@ -275,13 +263,8 @@ exports.deleteCategory = async (req, res, next) => {
|
||||
}
|
||||
}
|
||||
|
||||
exports.getQuotes = (req, res, next) => {
|
||||
const page = helperQueryNumber(req.query.page, 1);
|
||||
const limit = helperQueryNumber(req.query.limit, 10);
|
||||
const offset = (page - 1) * limit;
|
||||
Quotes.findAndCountAll({
|
||||
limit,
|
||||
offset,
|
||||
exports.getQuotes = async (req, res, next) => {
|
||||
const options = {
|
||||
where: {
|
||||
isValidated: 0,
|
||||
},
|
||||
@ -289,16 +272,8 @@ exports.getQuotes = (req, res, next) => {
|
||||
{ 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);
|
||||
});
|
||||
};
|
||||
return await getPagesHelper({ req, res, next }, Quotes, options);
|
||||
}
|
||||
|
||||
exports.putQuote = async (req, res, next) => {
|
||||
|
@ -2,32 +2,19 @@ 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');
|
||||
const getPagesHelper = require('../assets/utils/getPagesHelper');
|
||||
const { serverError } = require('../assets/config/errors');
|
||||
|
||||
exports.getCommentsByFunctionId = (req, res, next) => {
|
||||
exports.getCommentsByFunctionId = async (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,
|
||||
const options = {
|
||||
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);
|
||||
});
|
||||
};
|
||||
return await getPagesHelper({ req, res, next }, Comments, options);
|
||||
}
|
||||
|
||||
exports.postCommentsByFunctionId = async (req, res, next) => {
|
||||
|
@ -4,18 +4,14 @@ const Functions = require('../models/functions');
|
||||
const Categories = require('../models/categories');
|
||||
const functionToExecute = require('../assets/functions/functionObject');
|
||||
const helperQueryNumber = require('../assets/utils/helperQueryNumber');
|
||||
const getPagesHelper = require('../assets/utils/getPagesHelper');
|
||||
const Sequelize = require('sequelize');
|
||||
|
||||
exports.getFunctions = (req, res, next) => {
|
||||
const page = helperQueryNumber(req.query.page, 1);
|
||||
const limit = helperQueryNumber(req.query.limit, 10);
|
||||
exports.getFunctions = async (req, res, next) => {
|
||||
const categoryId = helperQueryNumber(req.query.categoryId, 0);
|
||||
let search = req.query.search;
|
||||
try { search = search.toLowerCase(); } catch {}
|
||||
const offset = (page - 1) * limit;
|
||||
Functions.findAndCountAll({
|
||||
limit,
|
||||
offset,
|
||||
try { search = search.toLowerCase(); } catch {};
|
||||
const options = {
|
||||
where: {
|
||||
isOnline: 1,
|
||||
// Trie par catégorie
|
||||
@ -36,16 +32,8 @@ exports.getFunctions = (req, res, next) => {
|
||||
exclude: ["updatedAt", "utilizationForm", "article", "isOnline"]
|
||||
},
|
||||
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);
|
||||
});
|
||||
};
|
||||
return await getPagesHelper({ req, res, next }, Functions, options);
|
||||
}
|
||||
|
||||
exports.getFunctionBySlug = (req, res, next) => {
|
||||
|
@ -2,15 +2,10 @@ const errorHandling = require('../assets/utils/errorHandling')
|
||||
const { serverError, requiredFields } = require('../assets/config/errors');
|
||||
const Quotes = require('../models/quotes');
|
||||
const Users = require('../models/users');
|
||||
const helperQueryNumber = require('../assets/utils/helperQueryNumber');
|
||||
const getPagesHelper = require('../assets/utils/getPagesHelper');
|
||||
|
||||
exports.getQuotes = (req, res, next) => {
|
||||
const page = helperQueryNumber(req.query.page, 1);
|
||||
const limit = helperQueryNumber(req.query.limit, 10);
|
||||
const offset = (page - 1) * limit;
|
||||
Quotes.findAndCountAll({
|
||||
limit,
|
||||
offset,
|
||||
exports.getQuotes = async (req, res, next) => {
|
||||
const options = {
|
||||
where: {
|
||||
isValidated: 1,
|
||||
},
|
||||
@ -21,16 +16,8 @@ exports.getQuotes = (req, res, next) => {
|
||||
exclude: ["isValidated"]
|
||||
},
|
||||
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);
|
||||
});
|
||||
};
|
||||
return await getPagesHelper({ req, res, next }, Quotes, options);
|
||||
}
|
||||
|
||||
exports.postQuote = (req, res, next) => {
|
||||
|
Reference in New Issue
Block a user