From 8d917e7216df66ade6dc50746d408e0a45dbc035 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Tue, 7 Jan 2020 22:39:30 +0100 Subject: [PATCH] Implement text search api --- src/utils/api.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/utils/api.js diff --git a/src/utils/api.js b/src/utils/api.js new file mode 100644 index 0000000..c0a3dcb --- /dev/null +++ b/src/utils/api.js @@ -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)) + } + } +}