sbkwgh 8 anni fa
parent
commit
089e2d5189
2 ha cambiato i file con 18 aggiunte e 3 eliminazioni
  1. 17 1
      routes/thread.js
  2. 1 2
      test/thread_post.js

+ 17 - 1
routes/thread.js

@@ -41,9 +41,25 @@ router.get('/:thread_id', async (req, res) => {
 
 		if(lastPost === undefined) {
 			resThread.meta.postsRemaining = 0
+			resThread.meta.nextPostsCount = 0
+			resThread.meta.previousPostsCount = 0
 		} else {
 			resThread.meta.postsRemaining =
-				thread.postsCount - lastPost.postNumber - 1
+				resThread.postsCount - lastPost.postNumber - 1
+
+			if(resThread.meta.postsRemaining < limit) {
+				resThread.meta.nextPostsCount = resThread.meta.postsRemaining
+			} else {
+				resThread.meta.nextPostsCount = limit
+			}
+
+			if(firstPost.postNumber === 0) {
+				resThread.meta.previousPostsCount = 0
+			} else if(firstPost.postNumber - limit < 0) {
+				resThread.meta.previousPostsCount = firstPost.postNumber
+			} else {
+				resThread.meta.previousPostsCount = limit
+			}
 		}
 
 		res.json(resThread)

+ 1 - 2
test/thread_post.js

@@ -434,7 +434,7 @@ describe('Thread and post', () => {
 			pageOne.body.Posts[9].should.have.property('content', '<p>POST 20</p>\n')
 			pageOne.body.meta.should.have.property('postsRemaining', 9)
 			pageOne.body.meta.should.have.property('previousPostsCount', 10)
-			pageOne.body.meta.should.have.property('nextPostsCount', 10)
+			pageOne.body.meta.should.have.property('nextPostsCount', 9)
 
 			pageTwo.body.Posts.should.have.length(9)
 			pageTwo.body.Posts[0].should.have.property('content', '<p>POST 21</p>\n')
@@ -456,7 +456,6 @@ describe('Thread and post', () => {
 			pageFirst.body.meta.should.have.property('previousURL', null)
 			pageFirst.body.meta.should.have.property('postsRemaining', 29)
 			pageFirst.body.meta.should.have.property('previousPostsCount', 0)
-			pageFirst.body.meta.should.have.property('nextPostsCount', 10)
 
 		})
 		it('should return an error if :id is invalid', async () => {