comment threading
This commit is contained in:
parent
d4d4f1a6e3
commit
f56c686eb2
@ -3,11 +3,11 @@ const { Comment, User, Vote } = require('../db/models')
|
||||
|
||||
module.exports = router
|
||||
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
const comments = await Comment.findAll({
|
||||
attributes: ['id', 'text']})
|
||||
include: ['replies', 'user'],
|
||||
})
|
||||
res.send(comments)
|
||||
} catch (err) {
|
||||
next(err)
|
||||
@ -17,7 +17,8 @@ router.get('/', async (req, res, next) => {
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const comment = await Comment.findByPk(req.params.id, {
|
||||
attributes: ['id', 'text']})
|
||||
attributes: ['id', 'text'],
|
||||
})
|
||||
res.json(comment)
|
||||
} catch (err) {
|
||||
next(err)
|
||||
|
@ -18,6 +18,11 @@ Tag.belongsToMany(Article, { through: 'articleTags' })
|
||||
Comment.belongsTo(Article)
|
||||
Comment.belongsTo(User)
|
||||
Comment.hasMany(Vote)
|
||||
Comment.belongsTo(Comment, { as: 'parent' })
|
||||
Comment.hasMany(Comment, {
|
||||
as: { singular: 'reply', plural: 'replies' },
|
||||
foreignKey: 'parentId',
|
||||
})
|
||||
Vote.belongsTo(Comment)
|
||||
Vote.belongsTo(User)
|
||||
|
||||
|
27
db/seed.js
27
db/seed.js
@ -30,7 +30,12 @@ const testArticles = [{ title: 'latest news', text: 'waddup?!' }]
|
||||
|
||||
const testTags = [{ name: 'dox' }]
|
||||
|
||||
const testComments = [{ text: 'pretty good' }]
|
||||
const tc1 = { text: 'pretty good' }
|
||||
const tc2 = { text: 'yeah!' }
|
||||
const tc3 = { text: 'could be more detailed tho' }
|
||||
const tc4 = { text: 'BUT THE INSECTS' }
|
||||
const tc5 = { text: 'HAHAHAHAHA' }
|
||||
const tc6 = { text: 'oh shut up' }
|
||||
|
||||
const testVotes = [{ upvote: 0, downvote: 1 }]
|
||||
|
||||
@ -58,14 +63,30 @@ async function runSeed() {
|
||||
|
||||
const a1 = await Article.create(testArticles[0])
|
||||
const t4 = await Tag.create(testTags[0])
|
||||
const c1 = await Comment.create(testComments[0])
|
||||
const c1 = await Comment.create(tc1)
|
||||
const c2 = await Comment.create(tc2)
|
||||
const c3 = await Comment.create(tc3)
|
||||
const c4 = await Comment.create(tc4)
|
||||
const c5 = await Comment.create(tc5)
|
||||
const c6 = await Comment.create(tc6)
|
||||
const v1 = await Vote.create(testVotes[0])
|
||||
|
||||
await a1.setUser(u1)
|
||||
await a1.addTag(t4)
|
||||
await a1.addComment(c1)
|
||||
await a1.addComments(c1, c2, c3, c4, c5, c6)
|
||||
|
||||
await c1.setUser(u2)
|
||||
await c2.setUser(u1)
|
||||
await c3.setUser(u2)
|
||||
await c4.setUser(u1)
|
||||
await c5.setUser(u1)
|
||||
await c6.setUser(u2)
|
||||
|
||||
await c1.addVote(v1)
|
||||
await c1.addReply(2)
|
||||
await c2.addReplies([c3, c4])
|
||||
await c5.setParent(c4)
|
||||
await c6.setParent(c1)
|
||||
|
||||
console.log('seeded successfully')
|
||||
} catch (err) {
|
||||
|
@ -12,7 +12,8 @@
|
||||
"morgan": "^1.9.1",
|
||||
"nodemon": "^1.19.1",
|
||||
"pg": "^7.11.0",
|
||||
"sequelize": "^5.8.11"
|
||||
"sequelize": "^5.8.11",
|
||||
"uuid": "^3.3.2"
|
||||
},
|
||||
"scripts": {
|
||||
"seed": "node db/seed.js",
|
||||
|
Loading…
Reference in New Issue
Block a user