Bladeren bron

Implement pagination system

sbkwgh 8 jaren geleden
bovenliggende
commit
9e380e434f
3 gewijzigde bestanden met toevoegingen van 42 en 8 verwijderingen
  1. 3 3
      models/thread.js
  2. 8 5
      routes/thread.js
  3. 31 0
      test/thread_post.js

+ 3 - 3
models/thread.js

@@ -17,7 +17,7 @@ module.exports = (sequelize, DataTypes) => {
 				Thread.belongsTo(models.Category)
 				Thread.hasMany(models.Post)
 			},
-			includeOptions (start, limit) {
+			includeOptions (lastId, limit) {
 				let models = sequelize.models
 
 				return [
@@ -25,9 +25,9 @@ module.exports = (sequelize, DataTypes) => {
 					models.Category,
 					{ 
 						model: models.Post, 
-						where: { createdAt: { $lt: start } },
+						where: { id: { $gt: lastId } },
 						limit: limit,
-						order: [['createdAt', 'DESC']],
+						order: [['id', 'ASC']],
 						include: [
 							{ model: models.User, attributes: ['username', 'createdAt', 'id', 'color'] }, 
 							{

+ 8 - 5
routes/thread.js

@@ -6,20 +6,23 @@ let { User, Thread, Category } = require('../models')
 
 router.get('/:thread_id', async (req, res) => {
 	try {
-		let start = req.query.start || new Date()
-		let limit = +req.query.limit || 10
+		let lastId = 0
+		let limit = 10
+
+		if(+req.query.lastId > 0) lastId = +req.query.lastId
+		if(+req.query.limit > 0) limit = +req.query.limit
 
 		let thread = await Thread.findById(req.params.thread_id, {
-			include: Thread.includeOptions(start, limit)
+			include: Thread.includeOptions(lastId, limit)
 		})
 
 		if(!thread) throw Errors.invalidParameter('id', 'thread does not exist')
 
-		let meta = { limit: limit, next: null }
+		let meta = { limit: limit }
 
 		if(thread.Posts && thread.Posts.length) {
 			let lastPost = thread.Posts.slice(-1)[0]
-			meta.next = lastPost.createdAt
+			meta.lastId = lastPost.id
 		}
 
 		res.json({

+ 31 - 0
test/thread_post.js

@@ -358,6 +358,37 @@ describe('Thread and post', () => {
 			res.body.thread.Posts.should.contain.something.that.has.property('content', '<p>another post</p>\n')
 			res.body.thread.Posts.should.contain.something.that.has.deep.property('User.username', 'username1')
 		})
+		it('should allow pagination', async () => {
+			let thread = await userAgent
+				.post('/api/v1/thread')
+				.set('content-type', 'application/json')
+				.send({ category: 'category_name', name: 'pagination' })
+
+			for(var i = 0; i < 30; i++) {
+				await userAgent
+					.post('/api/v1/post')
+					.set('content-type', 'application/json')
+					.send({ threadId: thread.body.id, content: `POST ${i}` })
+			}
+
+			let pageOne = await userAgent.get('/api/v1/thread/' + thread.body.id)
+			let pageTwo = await userAgent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + pageOne.body.meta.lastId)
+			let pageThree = await userAgent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + pageTwo.body.meta.lastId)
+			let pageInvalid = await userAgent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + 100)
+
+			pageOne.body.thread.Posts.should.have.length(10)
+			pageOne.body.thread.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
+
+			pageTwo.body.thread.Posts.should.have.length(10)
+			pageTwo.body.thread.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
+
+			pageThree.body.thread.Posts.should.have.length(10)
+			pageThree.body.thread.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
+			pageThree.body.thread.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
+
+			pageInvalid.body.thread.Posts.should.have.length(0)
+
+		})
 		it('should return an error if :id is invalid', async () => {
 			try {
 				let res = await chai.request(server).get('/api/v1/thread/invalid')