backend: Chercher par catégorie et recherche

This commit is contained in:
Divlo 2020-03-20 22:57:43 +01:00
parent cb3ca25aac
commit 404af8be6a
3 changed files with 25 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

View File

@ -3,22 +3,36 @@ const { serverError } = require('../assets/config/errors');
const Functions = require('../models/functions');
const Categories = require('../models/categories');
const functionToExecute = require('../assets/functions/functionObject');
const Sequelize = require('sequelize');
function helperQueryNumber(value, defaultValue) {
if (value && !isNaN(value)) return parseInt(value);
return defaultValue;
}
exports.getFunctions = (req, res, next) => {
// TODO: Trier et chercher par catégories
let page = 1;
let limit = 10;
if (req.query.page && !isNaN(req.query.page)) {
page = parseInt(req.query.page);
}
if (req.query.limit && !isNaN(req.query.limit)) {
limit = parseInt(req.query.limit);
}
const page = helperQueryNumber(req.query.page, 1);
const limit = helperQueryNumber(req.query.limit, 10);
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,
where: { isOnline: 1 },
where: {
isOnline: 1,
// Trie par catégorie
... (categoryId !== 0) && { categorieId: categoryId },
// Recherche
... (search != undefined) && {
[Sequelize.Op.or]: [
{ title: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('title')), 'LIKE', `%${search}%`) },
{ slug: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('slug')), 'LIKE', `%${search}%`) },
{ description: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('description')), 'LIKE', `%${search}%`) }
]
}
},
include: [
{ model: Categories, attributes: ["name", "color"] }
]

View File

@ -55,4 +55,5 @@
border-radius: 0.5em;
padding: 0.5em;
margin-right: 20px;
font-size: 16.4px;
}