Fix search bug

This commit is contained in:
Mr Swift
2024-08-26 00:07:19 +03:30
parent 11d8507e3c
commit 01a2871103
3 changed files with 57 additions and 27 deletions
+19 -8
View File
@@ -24,17 +24,28 @@ module.exports.getAll = async (req, res) => {
page = req.query.page
page = page * rows - rows
}
// const search = req.query?.search ? req.query.search : ""
const search = req.query?.search ? req.query.search : null
let sort
if(req.query?.sort){
sort= req.query?.sort
}else{
sort= { created_at: -1 }
}
const score = await Score.find({
const filter = {
userId: req.user_id,
// $or: [
// { title: { $regex: search } },
// { score: { $regex: search } },
// ],
}).skip(page).limit(rows);
const total = Math.ceil((await Score.countDocuments({ userId: req.user_id })))
}
if(search){
filter.$or = [
{ title: { $regex: search } },
{ score: search },
]
}
const score = await Score.find(filter).skip(page).limit(rows).sort(sort);
const total = Math.ceil((await Score.countDocuments(filter)))
const user = await User.findById(req.user_id).select('score')
res.status(200).json({ data: score, score: user.score, total })