Implement text search api

This commit is contained in:
LordMathis 2020-01-07 22:39:30 +01:00
parent 8c0415c3f7
commit 8d917e7216
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
1 changed files with 24 additions and 0 deletions

24
src/utils/api.js Normal file
View File

@ -0,0 +1,24 @@
export class Api {
constructor (storage) {
this.storage = storage
}
getPosts (req, res) {
const limit = req.query.limit ? req.query.limit : 10
const skip = req.query.skip ? req.query.skip : 0
if (req.query.search) {
this.storage.Post.find(
{ $text: { $search: req.query.search } },
{ body: false })
.skip(skip)
.limit(limit)
.then(posts => res.send(posts))
} else {
this.storage.Post.find({}, { body: false })
.skip(skip)
.limit(limit)
.then(posts => res.send(posts))
}
}
}