Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 15 additions & 14 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@ 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");
});

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({ $text : { $search: search, $caseSensitive: false}})
.skip(perPage * page - perPage)
.limit(perPage)
.exec((err, items) => {
Person.countDocuments({ $text : { $search: search, $caseSensitive: false} }).exec((err, count) => {
if (err) return res.status(400).send(err);
res.json({
items,
current: page,
pages: Math.ceil(count / perPage)
});
});
});
});

app.listen(9696, () => {
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ 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 (
<Container>
<input
type="text"
value={searchQuery}
onChange={e => setSearchQuery(e.target.value)}
placeholder="Search..."
/>
<Row>
{items.map(item => (
<Col key={item._id}>{item.name}</Col>
<Col key={item._id}>{item.name} - {item.description}</Col>
))}
</Row>
<Row className="justify-content-center mt-3">
Expand Down