Compare commits
No commits in common. "12364717bfa8227ec8af6fcee81178cd10bb4fe2" and "f0b76d82b11a818c0af8f305755e631857a52e21" have entirely different histories.
12364717bf
...
f0b76d82b1
|
@ -1,33 +1,21 @@
|
||||||
|
|
||||||
const advancedResult =(model)=> async (req, res, next) => {
|
const advancedResult =(model)=> async (req, res, next) => {
|
||||||
//search : l'information que je cherche
|
//search : l'information que je cherche
|
||||||
//fields : les colonnes je veux utiliser pour la recherche
|
//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
|
//Recherche
|
||||||
if (fields) {
|
if (fields) {
|
||||||
|
|
||||||
//search = be
|
|
||||||
// mark , model, matrcle
|
|
||||||
//Recherche l'element search dans le tableau de propriété fields
|
//Recherche l'element search dans le tableau de propriété fields
|
||||||
const query = {
|
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 => ({
|
$or: fields.map(property => ({
|
||||||
[property]: {$regex: search, $options: 'i'}
|
[property]: {$regex: search, $options: 'i'}
|
||||||
}))
|
}))
|
||||||
|
@ -42,33 +30,32 @@ const advancedResult =(model)=> async (req, res, next) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pagination
|
//Pagination
|
||||||
page = parseInt(page, 10) || 0;
|
page = parseInt(page, 10) || 1;
|
||||||
rows = parseInt(rows, 10) || 3000;
|
limit = parseInt(limit, 10) || 3000;
|
||||||
const startIndex = page * rows;
|
const startIndex = (page - 1) * limit; //1, 1-1 = 0
|
||||||
const endIndex = page * rows;
|
const endIndex = page * limit;
|
||||||
const total = await model.find(find).countDocuments();
|
const total = await model.find(find).countDocuments();
|
||||||
|
|
||||||
//declaration
|
//declaration
|
||||||
let query = model.find(find);
|
let query = model.find(find);
|
||||||
|
|
||||||
query = query.skip(startIndex).limit(rows); // (5,8) 5,6,7,8,9,10,11, 12
|
query = query.skip(startIndex).limit(limit); // (5,8) 5,6,7,8,9,10,11, 12
|
||||||
|
|
||||||
//exécuté
|
//exécuté
|
||||||
let results = await query;
|
let results = await query;
|
||||||
|
|
||||||
const pagination = {};
|
const pagination = {};
|
||||||
|
|
||||||
if (endIndex < total) {
|
if (endIndex < total) {
|
||||||
pagination.next = {
|
pagination.next = {
|
||||||
page: page + 1,
|
page: page + 1,
|
||||||
rows
|
limit
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (startIndex > 0) {
|
if (startIndex > 0) {
|
||||||
pagination.prev = {
|
pagination.prev = {
|
||||||
page: page - 1,
|
page: page - 1,
|
||||||
rows
|
limit
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,10 +64,10 @@ const advancedResult =(model)=> async (req, res, next) => {
|
||||||
count: results.length,
|
count: results.length,
|
||||||
pagination: pagination,
|
pagination: pagination,
|
||||||
data: results,
|
data: results,
|
||||||
startIndex: startIndex,
|
startIndex: startIndex + 1,
|
||||||
endIndex: results.length,
|
endIndex: results.length,
|
||||||
totalRecords: total,
|
totalRecords: total,
|
||||||
rows: Number(rows),
|
limit: Number(limit),
|
||||||
page: page
|
page: page
|
||||||
};
|
};
|
||||||
next();
|
next();
|
||||||
|
|
Loading…
Reference in New Issue