Forráskód Böngészése

Change pagination mechanism, since for threads you want newest first

sbkwgh 8 éve
szülő
commit
c82a34c13f
2 módosított fájl, 33 hozzáadás és 9 törlés
  1. 23 1
      lib/pagination.js
  2. 10 8
      routes/category.js

+ 23 - 1
lib/pagination.js

@@ -14,6 +14,23 @@ async function getNextId(Model, where, items) {
 	}
 }
 
+async function getNextIdDesc(Model, where, items) {
+	try {
+		let minId = await Model.min('id', { where })
+		let lastItem = items.slice(-1)[0]
+
+		if(!lastItem || minId === lastItem.id) {
+			return null
+		} else {
+			return lastItem.id
+		}
+	} catch (e) {
+		console.log(e)
+		return null
+	}
+}
+
+
 async function getNextCount (Model, items, limit, where) {
 	let lastItem
 
@@ -34,10 +51,14 @@ async function getNextCount (Model, items, limit, where) {
 	}
 }
 
-function getPaginationProps(query) {
+function getPaginationProps(query, desc) {
 	let from = 0
 	let limit = 10
 
+	if(desc) {
+		from = null
+	}
+
 	if(+query.from > 0) from = +query.from
 	if(+query.limit > 0) limit = +query.limit
 
@@ -56,6 +77,7 @@ function getPaginationProps(query) {
 
 module.exports = {
 	getNextId,
+	getNextIdDesc,
 	getNextCount,
 	getPaginationProps
 }

+ 10 - 8
routes/category.js

@@ -34,7 +34,7 @@ router.get('/:category', async (req, res) => {
 
 	try {
 		let threads, threadsLatestPost, resThreads, user
-		let { from, limit } = pagination.getPaginationProps(req.query)
+		let { from, limit } = pagination.getPaginationProps(req.query, true)
 
 		if(req.query.username) {
 			user = await User.findOne({ where: { username: req.query.username }})
@@ -43,11 +43,9 @@ router.get('/:category', async (req, res) => {
 		function threadInclude(order) {
 			let options = {
 				model: Thread,
+				order: [['id', 'DESC']],
 				limit,
-				where: {
-					id: { $gte: from }
-					
-				},
+				where: {},
 				include: [
 					Category,
 					{ model: User, attributes: ['username', 'createdAt', 'id', 'color'] }, 
@@ -62,6 +60,10 @@ router.get('/:category', async (req, res) => {
 				options.where.userId = user.id
 			}
 
+			if(from !== null) {
+				options.where.id = { $lte: from }
+			}
+
 			return [options]
 		}
 
@@ -106,11 +108,11 @@ router.get('/:category', async (req, res) => {
 		})
 
 
-		let nextId = await pagination.getNextId(Thread, user ? { userId: user.id } : {}, resThreads.Threads)
+		let nextId = await pagination.getNextIdDesc(Thread, user ? { userId: user.id } : {}, resThreads.Threads)
 
 		if(nextId) {
 			resThreads.meta.nextURL =
-				`/api/v1/category/${req.params.category}?&limit=${limit}&from=${nextId + 1}`
+				`/api/v1/category/${req.params.category}?&limit=${limit}&from=${nextId - 1}`
 
 			if(user) {
 				resThreads.meta.nextURL += '&username=' + user.username
@@ -118,7 +120,7 @@ router.get('/:category', async (req, res) => {
 
 			resThreads.meta.nextThreadsCount = await pagination.getNextCount(
 				Thread, resThreads.Threads, limit,
-				{ userId: user.id }
+				user ? { userId: user.id } : {}
 			)
 		} else {
 			resThreads.meta.nextURL = null