integrate backend

This commit is contained in:
data 2019-09-12 13:43:11 +01:00
parent 0cf3c2e58b
commit d128262c31
24 changed files with 1540 additions and 2 deletions

View File

@ -0,0 +1,18 @@
you need postgres!
- localhost needs to be trusted
```
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
```
- create a user
`sudo -u postgres createuser --interactive`
- database: tasks-mockup
`createdb tasks-backend`
- seed database
`npm run seed`

745
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,16 @@
{
"name": "my-app",
"name": "tasks-mockup",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"concurrently": "^4.0.1",
"cors": "^2.8.5",
"express": "^4.17.1",
"morgan": "^1.9.1",
"nodemon": "^1.19.1",
"pg": "^7.11.0",
"sequelize": "^5.8.11",
"bootstrap-imageupload": "^1.1.3",
"http-proxy-middleware": "^0.19.1",
"react": "^16.8.6",
@ -12,7 +19,8 @@
"uuid": "^3.3.2"
},
"scripts": {
"start": "react-scripts start",
"start": "concurrently \"react-scripts start\" \"nodemon server\"",
"seed": "node server/db/seed.js",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"

56
server/api/articles.js Normal file
View File

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

35
server/api/comments.js Normal file
View File

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

27
server/api/index.js Executable file
View File

@ -0,0 +1,27 @@
const router = require('express').Router()
module.exports = router
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 {
res.json({ ascii })
} catch (err) {
console.log(err)
next()
}
})
router.use((req, res, next) => {
const error = new Error(`Not Found: ${req.url}`)
console.log(error.message)
error.status = 404
next()
})

59
server/api/projects.js Normal file
View File

@ -0,0 +1,59 @@
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 project = await Project.create(req.body)
res.json(project)
} catch (err) {
next(err)
}
})
/* READ */
router.get('/', async (req, res, next) => {
try {
const projects = await Project.findAll()
res.send(projects)
} catch (err) {
next(err)
}
})
router.get('/:id', async (req, res, next) => {
try {
const project = await Project.findByPk(req.params.id)
res.json(project)
} catch (err) {
next(err)
}
})
/* UPDATE */
router.put('/:id', async (req, res, next) => {
try {
const project = await Project.findByPk(req.params.id)
await project.update(req.body)
res.json(project)
} catch (err) {
next(err)
}
})
/* DELETE */
router.post('/:id/delete', async (req, res, next) => {
try {
const project = await Project.findByPk(req.params.id)
await project.destroy({ force: true })
await Task.destroy({
where: {
projectId: req.params.id,
},
})
res.json(project)
} catch (err) {
next(err)
}
})

100
server/api/tags.js Normal file
View File

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

61
server/api/tasks.js Executable file
View File

@ -0,0 +1,61 @@
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 task = await Task.create(req.body)
if (req.body.projectId) {
const project = await Project.findOne({
where: { id: req.body.projectId },
})
await task.setProject(project.id)
}
res.json(task)
} catch (err) {
next(err)
}
})
/* READ */
router.get('/', async (req, res, next) => {
try {
const tasks = await Task.findAll()
res.send(tasks)
} catch (err) {
next(err)
}
})
router.get('/:id', async (req, res, next) => {
try {
const task = await Task.findByPk(req.params.id)
res.json(task)
} catch (err) {
next(err)
}
})
/* UPDATE */
router.put('/:id', async (req, res, next) => {
try {
const task = await Task.findByPk(req.params.id)
await task.update(req.body)
res.json(task)
} catch (err) {
next(err)
}
})
/* DELETE */
router.post('/:id/delete', async (req, res, next) => {
try {
const task = await Task.findByPk(req.params.id)
await task.destroy({ force: true })
res.json(task)
} catch (err) {
next(err)
}
})

54
server/api/users.js Normal file
View File

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

53
server/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)
}
})

26
server/ascii.js Normal file
View File

@ -0,0 +1,26 @@
const ascii = String.raw`
. .
* . . . . *
. . . . . .
o . .
. . . .
0 .
. . , , ,
. \ . .
. . \ ,
. o . . .
. . . \ , .
#\##\# . .
# #O##\### .
. . #*# #\##\### .
. ##*# #\##\## .
. . ##*# #o##\# .
. *# #\# . .
\ . .
____^/\___^--____/\____O______________/\/\---/\___________--
/\^ ^ ^ ^ ^^ ^ '\ ^ ^
-- - -- - - --- __ ^
-- __ ___-- ^ ^`
module.exports = ascii

18
server/db/db.js Executable file
View File

@ -0,0 +1,18 @@
const Sequelize = require('sequelize')
const pkg = require('../../package.json')
const databaseName = pkg.name
const createDB = () => {
const db = new Sequelize(
process.env.DATABASE_URL || `postgres://localhost:5432/${databaseName}`,
{
logging: false,
},
)
return db
}
const db = createDB()
module.exports = db

6
server/db/index.js Executable file
View File

@ -0,0 +1,6 @@
const db = require('./db')
// register models
require('./models')
module.exports = db

View File

@ -0,0 +1,15 @@
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

@ -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

43
server/db/models/index.js Executable file
View File

@ -0,0 +1,43 @@
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)
Task.belongsToMany(User, { through: 'userTask' })
User.hasMany(Task)
module.exports = {
Task,
Project,
User,
Article,
Tag,
Comment,
Vote,
}

View File

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

11
server/db/models/tag.js Normal file
View File

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

16
server/db/models/task.js Executable file
View File

@ -0,0 +1,16 @@
const Sequelize = require('sequelize')
const db = require('../db')
const Task = db.define('tasks', {
desc: {
type: Sequelize.STRING,
allowNull: false,
},
completed: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
})
module.exports = Task

20
server/db/models/user.js Normal file
View File

@ -0,0 +1,20 @@
const Sequelize = require('sequelize')
const db = require('../db')
const User = db.define('users', {
name: {
type: Sequelize.STRING,
allowNull: false,
},
email: {
type: Sequelize.STRING,
},
avatar: {
type: Sequelize.STRING,
defaultValue: 'default-user-img.png',
},
})
module.exports = User

9
server/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

102
server/db/seed.js Executable file
View File

@ -0,0 +1,102 @@
const db = require('../db')
const { Task, Project, User, Article, Tag, Comment, Vote } = require('./models')
const testTasks = [
{
desc: 'make app',
completed: false,
projectId: 1,
},
{
desc: 'update backend',
completed: false,
projectId: 1,
},
{
desc: 'eat dinner',
completed: false,
projectId: 1,
},
]
const testProjects = [{ name: 'Anarchy Planet' }, { name: 'Tilde' }]
const testUsers = [
{ name: 'nn', email: 'nn@ap.org' },
{ 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!')
console.log('seeding...')
try {
const p1 = await Project.create(testProjects[0])
const p2 = await Project.create(testProjects[1])
const u1 = await User.create(testUsers[0])
const u2 = await User.create(testUsers[1])
const t1 = await Task.create(testTasks[0])
const t2 = await Task.create(testTasks[1])
const t3 = await Task.create(testTasks[2])
await p1.addTask(t1)
await p1.addTask(t2)
await p2.addTask(t3)
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)
process.exitCode = 1
} finally {
console.log('closing db connection')
await db.close()
console.log('db connection closed')
}
}
runSeed()

34
server/index.js Executable file
View File

@ -0,0 +1,34 @@
const express = require('express')
const path = require('path')
const app = express()
const morgan = require('morgan')
const ascii = require('./ascii')
const cors = require('cors')
const port = process.env.PORT || 1337
app.use(morgan('tiny'))
app.use(cors())
// body parsing middleware
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(require('body-parser').text())
app.use('/api', require('./api'))
if (process.env.NODE_ENV === 'production') {
// Express will serve up production assets
app.use(express.static(path.join(__dirname, 'dist')))
}
app.use('/', express.static(path.resolve(__dirname, '..', 'build')))
// error handling endware
app.use((err, req, res, next) => {
console.error(err)
console.error(err.stack)
res.status(err.status || 500).send(err.message || 'Internal server error.')
next()
})
app.listen(port, () => {
console.log('\n' + ascii + '\n')
console.log(`Doin' haxor stuff on port ${port}`)
})