From 9ed521c2988278ef06eb077311538ba6d61c897f Mon Sep 17 00:00:00 2001 From: data Date: Thu, 4 Jul 2019 09:16:49 -0400 Subject: [PATCH] add Comments and Votes --- api/comments.js | 34 ++++++++++++++++++++++++++++ api/index.js | 3 +++ api/votes.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ db/models/comment.js | 11 +++++++++ db/models/index.js | 14 +++++++++++- db/models/vote.js | 9 ++++++++ db/seed.js | 22 +++++++++++++----- 7 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 api/comments.js create mode 100644 api/votes.js create mode 100644 db/models/comment.js create mode 100644 db/models/vote.js diff --git a/api/comments.js b/api/comments.js new file mode 100644 index 0000000..54e4a69 --- /dev/null +++ b/api/comments.js @@ -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) + } +}) diff --git a/api/index.js b/api/index.js index f53c504..b9c1704 100755 --- a/api/index.js +++ b/api/index.js @@ -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 { diff --git a/api/votes.js b/api/votes.js new file mode 100644 index 0000000..ad522de --- /dev/null +++ b/api/votes.js @@ -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) + } +}) diff --git a/db/models/comment.js b/db/models/comment.js new file mode 100644 index 0000000..2cc827f --- /dev/null +++ b/db/models/comment.js @@ -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 diff --git a/db/models/index.js b/db/models/index.js index 90c55c3..dd8533e 100755 --- a/db/models/index.js +++ b/db/models/index.js @@ -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 } diff --git a/db/models/vote.js b/db/models/vote.js new file mode 100644 index 0000000..820b8e9 --- /dev/null +++ b/db/models/vote.js @@ -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 diff --git a/db/seed.js b/db/seed.js index 15d123c..c5ae2b6 100755 --- a/db/seed.js +++ b/db/seed.js @@ -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) {