Explorar el Código

Fix bugs with user page api routes

sbkwgh hace 8 años
padre
commit
64da3bb39c
Se han modificado 4 ficheros con 14 adiciones y 33 borrados
  1. 0 18
      lib/pagination.js
  2. 2 2
      models/user.js
  3. 10 11
      routes/user.js
  4. 2 2
      test/user.js

+ 0 - 18
lib/pagination.js

@@ -14,23 +14,6 @@ async function getNextId(Model, where, items) {
 	}
 }
 
-async function getPreviousId(Model, where, items, limit) {
-	try {
-		let minId = await Model.min('id', { where })
-		let firstItem = items[0]
-
-		if(!firstItem || minId === firstItem.id) {
-			return null
-		} else {
-			return firstItem.id
-		}
-	} catch (e) {
-		console.log(e)
-		return null
-	}
-}
-
-
 function getPaginationProps(query) {
 	let from = 0
 	let limit = 10
@@ -53,6 +36,5 @@ function getPaginationProps(query) {
 
 module.exports = {
 	getNextId,
-	getPreviousId,
 	getPaginationProps
 }

+ 2 - 2
models/user.js

@@ -23,7 +23,7 @@ module.exports = (sequelize, DataTypes) => {
 				User.hasMany(models.Post)
 				User.hasMany(models.Thread)
 			},
-			includeOptions (lastId, limit) {
+			includeOptions (from, limit) {
 				let models = sequelize.models
 				let options = models.Post.includeOptions()
 
@@ -31,7 +31,7 @@ module.exports = (sequelize, DataTypes) => {
 					model: models.Post,
 					include: options,
 					limit,
-					where: { id: { $gt: lastId } },
+					where: { postNumber: { $gte: from } },
 					order: [['id', 'ASC']]
 				}]
 			}

+ 10 - 11
routes/user.js

@@ -124,9 +124,9 @@ router.get('/:username', async (req, res) => {
 
 		if(req.query.posts) {
 			
-			let { lastId, limit } = pagination.getPaginationProps(req.query)
+			let { from, limit } = pagination.getPaginationProps(req.query)
 
-			queryObj.include = User.includeOptions(lastId, limit)
+			queryObj.include = User.includeOptions(from, limit)
 
 			let user = await User.findOne(queryObj)
 			if(!user) throw Errors.accountDoesNotExist
@@ -134,24 +134,23 @@ router.get('/:username', async (req, res) => {
 			let resUser = user.toJSON()
 			resUser.meta = {}
 
-			let nextId = await pagination.getNextId(Post, { userId: user.id }, resUser.Posts)
-
-			if(nextId) {
-				resUser.meta.nextURL =
-					`/api/v1/user/${user.username}?posts=true&limit=${limit}&lastId=${nextId}`
-			} else {
+			let lastPost = user.Posts.slice(-1)[0]
+			if(!lastPost || lastPost.postNumber+1 === lastPost.Thread.postsCount) {
 				resUser.meta.nextURL = null
+			} else {
+				resUser.meta.nextURL =
+					`/api/v1/user/${user.username}?posts=true&limit=${limit}&from=${lastPost.postNumber + 1}`
 			}
 
 			res.json(resUser)
 		} else if(req.query.threads) {
-			let { lastId, limit } = pagination.getPaginationProps(req.query)
+			let { from, limit } = pagination.getPaginationProps(req.query)
 
 			queryObj.include = [{
 				model: Thread,
 				include: [Category],
 				limit,
-				where: { id: { $gt: lastId } },
+				where: { id: { $gt: from } },
 				order: [['id', 'ASC']]
 			}]
 
@@ -165,7 +164,7 @@ router.get('/:username', async (req, res) => {
 
 			if(nextId) {
 				resUser.meta.nextURL =
-					`/api/v1/user/${user.username}?threads=true&limit=${limit}&lastId=${nextId}`
+					`/api/v1/user/${user.username}?threads=true&limit=${limit}&from=${nextId}`
 			} else {
 				resUser.meta.nextURL = null
 			}

+ 2 - 2
test/user.js

@@ -357,7 +357,7 @@ describe('User', () => {
 			let pageOne = await agent.get('/api/v1/user/paginationaccount?posts=true')
 			let pageTwo = await agent.get(pageOne.body.meta.nextURL)
 			let pageThree = await agent.get(pageTwo.body.meta.nextURL)
-			let pageInvalid = await agent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + 100)
+			let pageInvalid = await agent.get('/api/v1/user/paginationaccount?posts=true&from=100')
 
 			pageOne.body.Posts.should.have.length(10)
 			pageOne.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
@@ -396,7 +396,7 @@ describe('User', () => {
 
 			let pageOne = await agent.get('/api/v1/user/threadaccount?threads=true')
 			let pageTwo = await agent.get(pageOne.body.meta.nextURL)
-			let pageInvalid = await agent.get('/api/v1/user/threadaccount?threads=true&lastId=100')
+			let pageInvalid = await agent.get('/api/v1/user/threadaccount?threads=true&from=100')
 
 			pageOne.body.Threads.should.have.length(10)
 			pageOne.body.Threads[0].should.have.property('name', 'THREAD 0')