From 2355b90881aca1e5f82d8a875a1c9c305d59ca99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Cala=C3=A7a?= Date: Tue, 31 Jan 2023 22:46:35 -0300 Subject: [PATCH 1/2] feat: search engine implemented --- backend/src/index.js | 25 +++++++++++++------------ frontend/src/App.js | 11 +++++++++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/backend/src/index.js b/backend/src/index.js index cd9bcd2..35cd570 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -14,22 +14,23 @@ app.post("/items", async (req, res) => { }); app.get("/items/:page", (req, res) => { - const perPage = 10; + const perPage = 5; const page = req.params.page || 1; + const search = req.query.search || ""; - Person.find({}) - .skip(perPage * page - perPage) - .limit(perPage) - .exec((err, items) => { - Person.countDocuments().exec((err, count) => { - if (err) return res.status(400).send(err); - res.json({ - items, - current: page, - pages: Math.ceil(count / perPage) - }); + Person.find({ name: { $regex: search, $options: "i" } }) + .skip(perPage * page - perPage) + .limit(perPage) + .exec((err, items) => { + Person.countDocuments({ name: { $regex: search, $options: "i" } }).exec((err, count) => { + if (err) return res.status(400).send(err); + res.json({ + items, + current: page, + pages: Math.ceil(count / perPage) }); }); + }); }); app.listen(9696, () => { diff --git a/frontend/src/App.js b/frontend/src/App.js index 1f80e15..0ec8978 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -7,19 +7,26 @@ function App() { const [items, setItems] = useState([]); const [currentPage, setCurrentPage] = useState(1); const [pages, setPages] = useState(1); + const [searchQuery, setSearchQuery] = useState(""); useEffect(() => { axios - .get(`items/${currentPage}`) + .get(`items/${currentPage}?search=${searchQuery}`) .then(res => { setItems(res.data.items); setPages(res.data.pages); }) .catch(err => console.log(err)); - }, [currentPage]); + }, [currentPage, searchQuery]); return ( + setSearchQuery(e.target.value)} + placeholder="Search..." + /> {items.map(item => ( {item.name} From 4dfbf01e667ce263208951e49b0d2c0fb3028e29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Cala=C3=A7a?= Date: Thu, 2 Feb 2023 14:02:12 -0300 Subject: [PATCH 2/2] feat: full text search MongoDB --- backend/src/connection.js | 3 ++- backend/src/index.js | 8 ++++---- frontend/src/App.js | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/backend/src/connection.js b/backend/src/connection.js index a0607a5..83c0028 100644 --- a/backend/src/connection.js +++ b/backend/src/connection.js @@ -5,7 +5,8 @@ mongoose.connect("mongodb://127.0.0.1:27017/full", { }); const PersonSchema = new mongoose.Schema({ - name: String + name: String, + description: String }); const Person = mongoose.model('Person', PersonSchema); diff --git a/backend/src/index.js b/backend/src/index.js index 35cd570..42e7fa9 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -5,9 +5,9 @@ const Person = require("./connection"); app.use(express.json()) app.post("/items", async (req, res) => { - const {name} = req.body + const {name, description} = req.body - const person = new Person({name: name}) + const person = new Person({name: name, description: description}) await person.save() res.status(400).send("Saved"); @@ -18,11 +18,11 @@ app.get("/items/:page", (req, res) => { const page = req.params.page || 1; const search = req.query.search || ""; - Person.find({ name: { $regex: search, $options: "i" } }) + Person.find({ $text : { $search: search, $caseSensitive: false}}) .skip(perPage * page - perPage) .limit(perPage) .exec((err, items) => { - Person.countDocuments({ name: { $regex: search, $options: "i" } }).exec((err, count) => { + Person.countDocuments({ $text : { $search: search, $caseSensitive: false} }).exec((err, count) => { if (err) return res.status(400).send(err); res.json({ items, diff --git a/frontend/src/App.js b/frontend/src/App.js index 0ec8978..ed2a47c 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -29,7 +29,7 @@ function App() { /> {items.map(item => ( - {item.name} + {item.name} - {item.description} ))}