Compare commits

..

2 Commits

Author SHA1 Message Date
Smile 12364717bf Merge remote-tracking branch 'origin/master'
# Conflicts:
#	shared/middleware/advanced-result.js
2024-08-23 18:06:07 +01:00
Smile f5bd8252a5 Lis 2024-08-23 18:04:52 +01:00
1 changed files with 30 additions and 17 deletions

View File

@ -1,21 +1,33 @@
const advancedResult =(model)=> async (req, res, next) => {
//search : l'information que je cherche
//fields : les colonnes je veux utiliser pour la recherche
const fields = req.query.fields; //Permet de récupérer les champs qu'on va effectuer la recherche du front.
const search = req.query.search; //Permet de récupérer la recherche
let page = req.query.page; //Permet de récupérer la page actuelle
let limit = req.query.limit; //Permet de récupérer la page de limite
const fields = req.query.fields;
const search = req.query.search;
let page = req.query.page;
let rows = req.query.rows;
let reqQuery = {...req.query};
const noField = ['page','rows', 'fields', 'search','sort','select'];
noField.forEach(param=> delete reqQuery[param]);
//4. Create query string
let queryStr = JSON.stringify(reqQuery);
//5. Create operators mongo db query ['$lte'] = 30
queryStr = queryStr.replace(/\b(gt|gte|lt|lte|in|ne|or|nin)\b/g, match => `$${match}`);
let find = JSON.parse(queryStr);
console.log("varien", find);
let find = {}
//Recherche
if (fields) {
//search = be
// mark , model, matrcle
//Recherche l'element search dans le tableau de propriété fields
const query = {
//$or: c'est pour faire la recherche sur plusieurs champs, $regex : permet de stocker la recherche
//i : c'est pour mettre le texte insensible à la casse.
//-i: sensible à la casse.
$or: fields.map(property => ({
[property]: {$regex: search, $options: 'i'}
}))
@ -30,32 +42,33 @@ const advancedResult =(model)=> async (req, res, next) => {
}
//Pagination
page = parseInt(page, 10) || 1;
limit = parseInt(limit, 10) || 3000;
const startIndex = (page - 1) * limit; //1, 1-1 = 0
const endIndex = page * limit;
page = parseInt(page, 10) || 0;
rows = parseInt(rows, 10) || 3000;
const startIndex = page * rows;
const endIndex = page * rows;
const total = await model.find(find).countDocuments();
//declaration
let query = model.find(find);
query = query.skip(startIndex).limit(limit); // (5,8) 5,6,7,8,9,10,11, 12
query = query.skip(startIndex).limit(rows); // (5,8) 5,6,7,8,9,10,11, 12
//exécuté
let results = await query;
const pagination = {};
if (endIndex < total) {
pagination.next = {
page: page + 1,
limit
rows
};
}
if (startIndex > 0) {
pagination.prev = {
page: page - 1,
limit
rows
};
}
@ -64,10 +77,10 @@ const advancedResult =(model)=> async (req, res, next) => {
count: results.length,
pagination: pagination,
data: results,
startIndex: startIndex + 1,
startIndex: startIndex,
endIndex: results.length,
totalRecords: total,
limit: Number(limit),
rows: Number(rows),
page: page
};
next();