add Comments and Votes

This commit is contained in:
data 2019-07-04 09:16:49 -04:00
parent d44824e563
commit 9ed521c298
7 changed files with 139 additions and 7 deletions

34
api/comments.js Normal file
View 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)
}
})

View File

@ -6,6 +6,9 @@ router.use('/tasks', require('./tasks'))
router.use('/projects', require('./projects'))
router.use('/articles', require('./articles'))
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) => {
try {

53
api/votes.js Normal file
View 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
View 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

View File

@ -3,11 +3,23 @@ const Project = require('./project')
const User = require('./user')
const Article = require('./article')
const Tag = require('./tag')
const Comment = require('./comment')
const Vote = require('./vote')
User.hasMany(Article)
User.hasMany(Comment)
User.hasMany(Vote)
Project.hasMany(Task)
Task.belongsTo(Project)
Article.hasMany(Comment)
Article.hasMany(Tag)
Article.belongsTo(User)
Tag.belongsTo(Article)
Comment.belongsTo(Article)
Comment.belongsTo(User)
Comment.hasMany(Vote)
Vote.belongsTo(Comment)
Vote.belongsTo(User)
User.belongsToMany(Project, { through: 'projectUser' })
Project.hasMany(User)
@ -15,4 +27,4 @@ Project.hasMany(User)
Task.belongsToMany(User, { through: 'userTask' })
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
View 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

View File

@ -1,5 +1,5 @@
const db = require('../db')
const { Task, Project, User, Article, Tag } = require('./models')
const { Task, Project, User, Article, Tag, Comment, Vote } = require('./models')
const testTasks = [
{
@ -28,7 +28,11 @@ const testUsers = [
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() {
await db.sync({ force: true })
@ -52,10 +56,16 @@ async function runSeed() {
await u1.addTasks([t1, t2])
await u2.addTask(t3)
const projects = await Project.bulkCreate(testProjects)
const tasks = await Task.bulkCreate(testTasks)
const articles = await Article.bulkCreate(testArticles)
const tags = await Tag.bulkCreate(testTags)
const a1 = await Article.create(testArticles[0])
const t4 = await Tag.create(testTags[0])
const c1 = await Comment.create(testComments[0])
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')
} catch (err) {