Parcourir la source

Move thread meta data logic to instance method

sbkwgh il y a 8 ans
Parent
commit
53bfc3e1bd
2 fichiers modifiés avec 60 ajouts et 52 suppressions
  1. 55 0
      models/thread.js
  2. 5 52
      routes/thread.js

+ 55 - 0
models/thread.js

@@ -19,6 +19,61 @@ module.exports = (sequelize, DataTypes) => {
 			defaultValue: false
 		}
 	}, {
+		instanceMethods: {
+			async getMeta (limit, posts) {
+				let meta = {}
+
+				let firstPost = posts[0]
+				let lastPost = posts.slice(-1)[0]
+
+				//next url
+				if(!lastPost || lastPost.postNumber+1 === this.postsCount) {
+					meta.nextURL = null
+				} else {
+					meta.nextURL =
+						`/api/v1/thread/${this.id}?limit=${limit}&from=${lastPost.postNumber + 1}`
+				}
+
+				//previous url
+				if(!firstPost || firstPost.postNumber === 0) {
+					meta.previousURL = null
+				} else if(firstPost.postNumber - limit < 0) {
+					meta.previousURL =
+						`/api/v1/thread/${this.id}?limit=${firstPost.postNumber}&from=0`
+				} else {
+					meta.previousURL =
+						`/api/v1/thread/${this.id}?limit=${limit}&from=${firstPost.postNumber - limit}`
+				}
+
+				//remaining posts
+				if(lastPost === undefined) {
+					meta.nextPostsCount = 0
+					meta.previousPostsCount = 0
+					meta.postsRemaining = 0
+				} else {
+					let postsRemaining =
+						this.postsCount - lastPost.postNumber - 1
+
+					meta.postsRemaining = postsRemaining
+
+					if(postsRemaining < limit) {
+						meta.nextPostsCount = postsRemaining
+					} else {
+						meta.nextPostsCount = limit
+					}
+
+					if(firstPost.postNumber === 0) {
+						meta.previousPostsCount = 0
+					} else if(firstPost.postNumber - limit < 0) {
+						meta.previousPostsCount = firstPost.postNumber
+					} else {
+						meta.previousPostsCount = limit
+					}
+				}
+
+				return meta
+			}
+		},
 		classMethods: {
 			associate (models) {
 				Thread.belongsTo(models.User)

+ 5 - 52
routes/thread.js

@@ -8,61 +8,14 @@ let pagination = require('../lib/pagination.js')
 router.get('/:thread_id', async (req, res) => {
 	try {
 		let { from, limit } = pagination.getPaginationProps(req.query)
-		let thread, resThread
-	
-		thread = await Thread.findById(req.params.thread_id, {
+		let thread = await Thread.findById(req.params.thread_id, {
 			include: Thread.includeOptions(from, limit)
-		})
-
+		}) 
 		if(!thread) throw Errors.invalidParameter('id', 'thread does not exist')
-		resThread = thread.toJSON()
-
-		resThread.meta = {}
-	
-		let lastPost = thread.Posts.slice(-1)[0]
-		let firstPost = thread.Posts[0]
-
-		if(!lastPost || lastPost.postNumber+1 === thread.postsCount) {
-			resThread.meta.nextURL = null
-		} else {
-			resThread.meta.nextURL =
-				`/api/v1/thread/${thread.id}?limit=${limit}&from=${lastPost.postNumber + 1}`
-		}
-
-		if(!firstPost || firstPost.postNumber === 0) {
-			resThread.meta.previousURL = null
-		} else if(firstPost.postNumber - limit < 0) {
-			resThread.meta.previousURL =
-				`/api/v1/thread/${thread.id}?limit=${firstPost.postNumber}&from=0`
-		} else {
-			resThread.meta.previousURL =
-				`/api/v1/thread/${thread.id}?limit=${limit}&from=${firstPost.postNumber - limit}`
-		}
-
-		if(lastPost === undefined) {
-			resThread.meta.postsRemaining = 0
-			resThread.meta.nextPostsCount = 0
-			resThread.meta.previousPostsCount = 0
-		} else {
-			resThread.meta.postsRemaining =
-				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
-			}
-		}
+		
+		let meta = await thread.getMeta(limit, thread.Posts)
 
-		res.json(resThread)
+		res.json(Object.assign( thread.toJSON(), { meta } ))
 		
 	} catch (e) {
 		if(e.name === 'invalidParameter') {