Selaa lähdekoodia

Use single association and add replyingToUsername field

sbkwgh 8 vuotta sitten
vanhempi
commit
862541b2eb
4 muutettua tiedostoa jossa 16 lisäystä ja 11 poistoa
  1. 13 6
      models/post.js
  2. 0 3
      models/thread.js
  3. 1 0
      routes/thread.js
  4. 2 2
      test/thread_post.js

+ 13 - 6
models/post.js

@@ -7,14 +7,24 @@ module.exports = (sequelize, DataTypes) => {
 			set (val) {
 				this.setDataValue('content', marked(val))
 			}
-		}
+		},
+		replyingToUsername: DataTypes.STRING
 	}, {
+		instanceMethods: {
+			getReplyingTo () {
+				return Post.findByPrimary(this.replyId)
+			},
+			setReplyingTo (post) {
+				return post.getUser().then(user => {
+					return this.update({ replyingToUsername: user.username, replyId: post.id })
+				})
+			}
+		},
 		classMethods: {
 			associate (models) {
 				Post.belongsTo(models.User)
 				Post.belongsTo(models.Thread)
-				Post.hasMany(models.Post, { as: 'Replies' })
-				Post.hasOne(models.Post, { as: 'ReplyingTo', foreignKey: 'ReplyId' })
+				Post.hasMany(models.Post, { as: 'Replies', foreignKey: 'replyId' })
 			},
 			includeOptions () {
 				let models = sequelize.models
@@ -23,9 +33,6 @@ module.exports = (sequelize, DataTypes) => {
 					{ model: models.User, attributes: ['username', 'createdAt', 'id'] }, 
 					{ model: models.Thread, include: [models.Category]} ,
 					{
-						model: models.Post, as: 'ReplyingTo', include:
-						[{ model: models.User, attributes: ['username', 'id'] }]
-					}, {
 						model: models.Post, as: 'Replies', include:
 						[{ model: models.User, attributes: ['username', 'id'] }]	
 					}

+ 0 - 3
models/thread.js

@@ -28,9 +28,6 @@ module.exports = (sequelize, DataTypes) => {
 						include: [
 							{ model: models.User, attributes: ['username', 'createdAt', 'id'] }, 
 							{
-								model: models.Post, as: 'ReplyingTo', include:
-								[{ model: models.User, attributes: ['username', 'id'] }]
-							}, {
 								model: models.Post, as: 'Replies', include:
 								[{ model: models.User, attributes: ['username', 'id'] }]	
 							}

+ 1 - 0
routes/thread.js

@@ -17,6 +17,7 @@ router.get('/:thread_id', async (req, res) => {
 				errors: [e]
 			})
 		} else {
+			console.log(e)
 			res.status(500)
 			res.json({
 				errors: [Errors.unknown]

+ 2 - 2
test/thread_post.js

@@ -278,7 +278,7 @@ describe('Thread and post', () => {
 			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')
-			res.body.should.have.deep.property('ReplyingTo.User.username', 'username')
+			res.body.should.have.property('replyingToUsername', 'username')
 			res.body.should.have.property('Replies').that.deep.equals([])
 		})
 		it('should return any replies to a post', async () => {
@@ -286,7 +286,7 @@ describe('Thread and post', () => {
 
 			res.should.be.json
 			res.should.have.status(200)
-			res.body.should.have.deep.property('ReplyingTo', null)
+			res.body.should.have.deep.property('replyingToUsername', null)
 			res.body.should.have.deep.property('Replies.0.content', '<p>another post</p>\n')
 		})
 		it('should return an error if reply id does not exist', async () => {