Skip to content

Commit

Permalink
Adding pagination in product index
Browse files Browse the repository at this point in the history
  • Loading branch information
israelvf committed Mar 6, 2020
1 parent ca4da6b commit 05215a5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.9.3",
"mongoose-paginate": "^5.0.3",
"require-dir": "^1.2.0"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion backend/src/controllers/ProductController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const Product = mongoose.model("Product");

module.exports = {
async index(req, res) {
const products = await Product.find();
const { page = 1 } = req.query;
const products = await Product.paginate({}, { page, limit: 10 });

return res.json(products);
},
Expand Down
3 changes: 3 additions & 0 deletions backend/src/models/Product.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mongoose = require("mongoose");
const mongoosePaginate = require("mongoose-paginate");

const ProductSchema = new mongoose.Schema({
title: {
Expand All @@ -19,4 +20,6 @@ const ProductSchema = new mongoose.Schema({
}
});

ProductSchema.plugin(mongoosePaginate);

mongoose.model("Product", ProductSchema);

0 comments on commit 05215a5

Please sign in to comment.