Browse Source

Add postsCount field and increment when new post added

sbkwgh 8 năm trước cách đây
mục cha
commit
36bc9c9e6b
2 tập tin đã thay đổi với 7 bổ sung1 xóa
  1. 5 1
      models/thread.js
  2. 2 0
      routes/post.js

+ 5 - 1
models/thread.js

@@ -9,7 +9,11 @@ module.exports = (sequelize, DataTypes) => {
 				this.setDataValue('slug', slug(val).toLowerCase())
 			}
 		},
-		slug: DataTypes.TEXT
+		slug: DataTypes.TEXT,
+		postsCount: {
+			type: DataTypes.INTEGER,
+			defaultValue: 0
+		}
 	}, {
 		classMethods: {
 			associate (models) {

+ 2 - 0
routes/post.js

@@ -87,6 +87,8 @@ router.post('/', async (req, res) => {
 		await post.setUser(user)
 		await post.setThread(thread)
 
+		await thread.increment('postsCount')
+
 		res.json(await post.reload({
 			include: Post.includeOptions()
 		}))