Compare commits

..

No commits in common. "articles" and "master" have entirely different histories.

14 changed files with 7 additions and 372 deletions

View File

@ -1,56 +0,0 @@
const router = require('express').Router()
const { Project, Task, Article, Tag } = require('../db/models')
module.exports = router
/* CREATE */
router.post('/', async (req, res, next) => {
try {
const article = await Article.create(req.body)
res.json(article)
} catch (err) {
next(err)
}
})
/* READ */
router.get('/', async (req, res, next) => {
try {
const articles = await Article.findAll({
include: { model: Tag },
})
res.send(articles)
} catch (err) {
next(err)
}
})
router.get('/:id', async (req, res, next) => {
try {
const article = await Article.findByPk(req.params.id)
res.json(article)
} catch (err) {
next(err)
}
})
/* UPDATE */
router.put('/:id', async (req, res, next) => {
try {
const article = await Article.findByPk(req.params.id)
await article.update(req.body)
res.json(article)
} catch (err) {
next(err)
}
})
/* DELETE */
router.post('/:id/delete', async (req, res, next) => {
try {
const article = await Article.findByPk(req.params.id)
await article.destroy({ force: true })
res.json(article)
} catch (err) {
next(err)
}
})

View File

@ -1,35 +0,0 @@
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({
include: ['replies', 'user'],
})
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

@ -4,11 +4,6 @@ const ascii = require('../ascii')
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 {

View File

@ -1,5 +1,5 @@
const router = require('express').Router()
const { Project, Task, Article, Tag } = require('../db/models')
const { Project, Task } = require('../db/models')
module.exports = router
/* CREATE */
@ -46,7 +46,7 @@ router.put('/:id', async (req, res, next) => {
router.post('/:id/delete', async (req, res, next) => {
try {
const project = await Project.findByPk(req.params.id)
await project.destroy({ force: true })
project.destroy({ force: true })
await Task.destroy({
where: {
projectId: req.params.id,

View File

@ -1,100 +0,0 @@
const router = require('express').Router()
const { Project, Task, Article, Tag } = require('../db/models')
module.exports = router
/* CREATE */
router.post('/', async (req, res, next) => {
try {
// find or create tag and add article relation
var tag = await Tag.findOne({
where: { name: req.body.name },
include: { model: Article },
})
if (!tag) {
const newTag = { name: req.body.name }
tag = await Tag.create(newTag, { include: { model: Article } })
}
tag.addArticle(req.body.articleId)
// find selected article and add tag relation
var article = await Article.findByPk(req.body.articleId, {
include: { model: Tag },
})
article.addTag(tag.id)
// update article and tag after adding the relation
article = await Article.findByPk(req.body.articleId, {
include: { model: Tag },
})
tag = await Tag.findOne({
where: { name: req.body.name },
include: { model: Article },
})
res.json({ tag: tag, article: article })
} catch (err) {
next(err)
}
})
/* READ */
router.get('/', async (req, res, next) => {
try {
const tags = await Tag.findAll({
include: { model: Article },
})
res.send(tags)
} catch (err) {
next(err)
}
})
router.get('/:id', async (req, res, next) => {
try {
const tag = await Tag.findByPk(req.params.id)
res.json(tag)
} catch (err) {
next(err)
}
}) // remove tag from article
/* UPDATE */ router.put('/:id', async (req, res, next) => {
try {
//const tag = await Tag.findByPk(req.params.id)
//await tag.update(req.body)
//res.json(tag)
// find or create tag and add article relation
var tag = await Tag.findByPk(req.body.id, {
include: { model: Article },
})
tag.removeArticle(req.body.articleId)
// find selected article and add tag relation
var article = await Article.findByPk(req.body.articleId, {
include: { model: Tag },
})
article.removeTag(tag.id)
// update article and tag after adding the relation
article = await Article.findByPk(req.body.articleId, {
include: { model: Tag },
})
tag = await Tag.findByPk(req.body.id, {
include: { model: Article },
})
res.json({ tag: tag, article: article })
} catch (err) {
next(err)
}
})
/* DELETE */
router.post('/:id/delete', async (req, res, next) => {
try {
const tag = await Tag.findByPk(req.params.id)
await tag.destroy({ force: true })
res.json(tag)
} catch (err) {
next(err)
}
})

View File

@ -1,5 +1,5 @@
const router = require('express').Router()
const { Project, Task, Article, Tag } = require('../db/models')
const { Project, Task } = require('../db/models')
module.exports = router
/* CREATE */
@ -53,7 +53,7 @@ router.put('/:id', async (req, res, next) => {
router.post('/:id/delete', async (req, res, next) => {
try {
const task = await Task.findByPk(req.params.id)
await task.destroy({ force: true })
task.destroy({ force: true })
res.json(task)
} catch (err) {
next(err)

View File

@ -1,53 +0,0 @@
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)
}
})

View File

@ -1,15 +0,0 @@
const Sequelize = require('sequelize')
const db = require('../db')
const Article = db.define('articles', {
title: {
type: Sequelize.STRING,
allowNull: false,
},
text: {
type: Sequelize.TEXT,
allowNull: true,
},
})
module.exports = Article

View File

@ -1,11 +0,0 @@
const Sequelize = require('sequelize')
const db = require('../db')
const Comment = db.define('comments', {
text: {
type: Sequelize.TEXT,
allowNull: false,
},
})
module.exports = Comment

View File

@ -1,30 +1,9 @@
const Task = require('./task')
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.belongsToMany(Tag, { through: 'articleTags' })
Article.belongsTo(User)
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)
User.belongsToMany(Project, { through: 'projectUser' })
Project.hasMany(User)
@ -32,12 +11,4 @@ Project.hasMany(User)
Task.belongsToMany(User, { through: 'userTask' })
User.hasMany(Task)
module.exports = {
Task,
Project,
User,
Article,
Tag,
Comment,
Vote,
}
module.exports = { Task, Project, User }

View File

@ -1,11 +0,0 @@
const Sequelize = require('sequelize')
const db = require('../db')
const Tag = db.define('tags', {
name: {
type: Sequelize.STRING,
allowNull: false,
},
})
module.exports = Tag

View File

@ -1,9 +0,0 @@
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, Comment, Vote } = require('./models')
const { Task, Project, User } = require('./models')
const testTasks = [
{
@ -26,19 +26,6 @@ const testUsers = [
{ name: 'dn', email: 'dn@ap.org' },
]
const testArticles = [{ title: 'latest news', text: 'waddup?!' }]
const testTags = [{ name: 'dox' }]
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 }]
async function runSeed() {
await db.sync({ force: true })
console.log('db synced!')
@ -61,33 +48,6 @@ async function runSeed() {
await u1.addTasks([t1, t2])
await u2.addTask(t3)
const a1 = await Article.create(testArticles[0])
const t4 = await Tag.create(testTags[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.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) {
console.error(err)

View File

@ -12,8 +12,7 @@
"morgan": "^1.9.1",
"nodemon": "^1.19.1",
"pg": "^7.11.0",
"sequelize": "^5.8.11",
"uuid": "^3.3.2"
"sequelize": "^5.8.11"
},
"scripts": {
"seed": "node db/seed.js",