Переглянути джерело

Add and implement test case for postNumber field

sbkwgh 8 роки тому
батько
коміт
0ff38bf672
3 змінених файлів з 5 додано та 2 видалено
  1. 1 0
      models/post.js
  2. 2 2
      routes/post.js
  3. 2 0
      test/thread_post.js

+ 1 - 0
models/post.js

@@ -16,6 +16,7 @@ module.exports = (sequelize, DataTypes) => {
 				this.setDataValue('content', marked(val))
 			}
 		},
+		postNumber: DataTypes.INTEGER,
 		replyingToUsername: DataTypes.STRING
 	}, {
 		instanceMethods: {

+ 2 - 2
routes/post.js

@@ -75,13 +75,13 @@ router.post('/', async (req, res) => {
 			} else if(replyingToPost.Thread.id !== thread.id) {
 				throw Errors.invalidParameter('replyingToId', 'replies must be in same thread')
 			} else {
-				post = await Post.create({ content: req.body.content })
+				post = await Post.create({ content: req.body.content, postNumber: thread.postsCount+1 })
 
 				await post.setReplyingTo(replyingToPost)
 				await replyingToPost.addReplies(post)
 			}
 		} else {
-			post = await Post.create({ content: req.body.content })
+			post = await Post.create({ content: req.body.content, postNumber: thread.postsCount+1 })
 		}
 
 		await post.setUser(user)

+ 2 - 0
test/thread_post.js

@@ -178,6 +178,7 @@ describe('Thread and post', () => {
 			res.should.be.json
 			res.should.have.status(200)
 			res.body.should.have.property('content', '<p>content</p>\n')
+			res.body.should.have.property('postNumber', 1)
 			res.body.should.have.deep.property('User.username', 'username')
 			res.body.should.have.deep.property('Thread.name', 'thread')
 			res.body.should.have.deep.property('Thread.postsCount', 1)
@@ -280,6 +281,7 @@ describe('Thread and post', () => {
 
 			res.should.be.json
 			res.should.have.status(200)
+			res.body.should.have.property('postNumber', 2)
 			res.body.should.have.property('content', '<p>another post</p>\n')
 			res.body.should.have.deep.property('User.username', 'username1')
 			res.body.should.have.deep.property('Thread.name', 'thread')