add Comments and Votes
This commit is contained in:
parent
d44824e563
commit
9ed521c298
34
api/comments.js
Normal file
34
api/comments.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
const router = require('express').Router()
|
||||||
|
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']})
|
||||||
|
res.send(comments)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.get('/:id', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const comment = await Comment.findByPk(req.params.id, {
|
||||||
|
attributes: ['id', 'text']})
|
||||||
|
res.json(comment)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.post('/', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const comment = await Comment.create(req.body)
|
||||||
|
res.json(comment)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
})
|
@ -6,6 +6,9 @@ router.use('/tasks', require('./tasks'))
|
|||||||
router.use('/projects', require('./projects'))
|
router.use('/projects', require('./projects'))
|
||||||
router.use('/articles', require('./articles'))
|
router.use('/articles', require('./articles'))
|
||||||
router.use('/tags', require('./tags'))
|
router.use('/tags', require('./tags'))
|
||||||
|
router.use('/comments', require('./comments'))
|
||||||
|
router.use('/votes', require('./votes'))
|
||||||
|
router.use('/users', require('./users'))
|
||||||
|
|
||||||
router.get('/', async (req, res, next) => {
|
router.get('/', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
|
53
api/votes.js
Normal file
53
api/votes.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
const router = require('express').Router()
|
||||||
|
const { Comment, User, Vote } = require('../db/models')
|
||||||
|
module.exports = router
|
||||||
|
|
||||||
|
router.get('/', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const votes = await Vote.findAll()
|
||||||
|
res.send(votes)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.post('/', async (req, res, next) => {
|
||||||
|
const {userId, commentId, upvote, downvote} = req.body
|
||||||
|
try {
|
||||||
|
const votes = await Vote.create({userId, commentId, upvote, downvote})
|
||||||
|
res.send(votes)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.get('/:id', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const vote = await Vote.findByPk(+req.params.id)
|
||||||
|
res.json(vote)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.post('/:id/delete', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const vote = await Vote.findByPk(+req.params.id)
|
||||||
|
await vote.destroy()
|
||||||
|
res.json(vote)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.put('/:id/update', async (req, res, next) => {
|
||||||
|
const upvote = req.body.downvote
|
||||||
|
const downvote = req.body.upvote
|
||||||
|
try {
|
||||||
|
const vote = await Vote.findByPk(+req.params.id)
|
||||||
|
await vote.update({upvote, downvote})
|
||||||
|
res.json(vote)
|
||||||
|
} catch (err) {
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
})
|
11
db/models/comment.js
Normal file
11
db/models/comment.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const Sequelize = require('sequelize')
|
||||||
|
const db = require('../db')
|
||||||
|
|
||||||
|
const Comment = db.define('comments', {
|
||||||
|
text: {
|
||||||
|
type: Sequelize.TEXT,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = Comment
|
@ -3,11 +3,23 @@ const Project = require('./project')
|
|||||||
const User = require('./user')
|
const User = require('./user')
|
||||||
const Article = require('./article')
|
const Article = require('./article')
|
||||||
const Tag = require('./tag')
|
const Tag = require('./tag')
|
||||||
|
const Comment = require('./comment')
|
||||||
|
const Vote = require('./vote')
|
||||||
|
|
||||||
|
User.hasMany(Article)
|
||||||
|
User.hasMany(Comment)
|
||||||
|
User.hasMany(Vote)
|
||||||
Project.hasMany(Task)
|
Project.hasMany(Task)
|
||||||
Task.belongsTo(Project)
|
Task.belongsTo(Project)
|
||||||
|
Article.hasMany(Comment)
|
||||||
Article.hasMany(Tag)
|
Article.hasMany(Tag)
|
||||||
|
Article.belongsTo(User)
|
||||||
Tag.belongsTo(Article)
|
Tag.belongsTo(Article)
|
||||||
|
Comment.belongsTo(Article)
|
||||||
|
Comment.belongsTo(User)
|
||||||
|
Comment.hasMany(Vote)
|
||||||
|
Vote.belongsTo(Comment)
|
||||||
|
Vote.belongsTo(User)
|
||||||
|
|
||||||
User.belongsToMany(Project, { through: 'projectUser' })
|
User.belongsToMany(Project, { through: 'projectUser' })
|
||||||
Project.hasMany(User)
|
Project.hasMany(User)
|
||||||
@ -15,4 +27,4 @@ Project.hasMany(User)
|
|||||||
Task.belongsToMany(User, { through: 'userTask' })
|
Task.belongsToMany(User, { through: 'userTask' })
|
||||||
User.hasMany(Task)
|
User.hasMany(Task)
|
||||||
|
|
||||||
module.exports = { Task, Project, User, Article, Tag }
|
module.exports = { Task, Project, User, Article, Tag, Comment, Vote }
|
||||||
|
9
db/models/vote.js
Normal file
9
db/models/vote.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
const Sequelize = require('sequelize')
|
||||||
|
const db = require('../db')
|
||||||
|
|
||||||
|
const Vote = db.define('votes', {
|
||||||
|
upvote: Sequelize.INTEGER,
|
||||||
|
downvote: Sequelize.INTEGER,
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = Vote
|
22
db/seed.js
22
db/seed.js
@ -1,5 +1,5 @@
|
|||||||
const db = require('../db')
|
const db = require('../db')
|
||||||
const { Task, Project, User, Article, Tag } = require('./models')
|
const { Task, Project, User, Article, Tag, Comment, Vote } = require('./models')
|
||||||
|
|
||||||
const testTasks = [
|
const testTasks = [
|
||||||
{
|
{
|
||||||
@ -28,7 +28,11 @@ const testUsers = [
|
|||||||
|
|
||||||
const testArticles = [{ title: 'latest news', text: 'waddup?!' }]
|
const testArticles = [{ title: 'latest news', text: 'waddup?!' }]
|
||||||
|
|
||||||
const testTags = [{ name: 'dox', articleId: 1 }]
|
const testTags = [{ name: 'dox' }]
|
||||||
|
|
||||||
|
const testComments = [{ text: 'pretty good' }]
|
||||||
|
|
||||||
|
const testVotes = [{ upvote: 0, downvote: 1 }]
|
||||||
|
|
||||||
async function runSeed() {
|
async function runSeed() {
|
||||||
await db.sync({ force: true })
|
await db.sync({ force: true })
|
||||||
@ -52,10 +56,16 @@ async function runSeed() {
|
|||||||
await u1.addTasks([t1, t2])
|
await u1.addTasks([t1, t2])
|
||||||
await u2.addTask(t3)
|
await u2.addTask(t3)
|
||||||
|
|
||||||
const projects = await Project.bulkCreate(testProjects)
|
const a1 = await Article.create(testArticles[0])
|
||||||
const tasks = await Task.bulkCreate(testTasks)
|
const t4 = await Tag.create(testTags[0])
|
||||||
const articles = await Article.bulkCreate(testArticles)
|
const c1 = await Comment.create(testComments[0])
|
||||||
const tags = await Tag.bulkCreate(testTags)
|
const v1 = await Vote.create(testVotes[0])
|
||||||
|
|
||||||
|
await a1.setUser(u1)
|
||||||
|
await a1.addTag(t4)
|
||||||
|
await a1.addComment(c1)
|
||||||
|
await c1.setUser(u2)
|
||||||
|
await c1.addVote(v1)
|
||||||
|
|
||||||
console.log('seeded successfully')
|
console.log('seeded successfully')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
Loading…
Reference in New Issue
Block a user