first commit

This commit is contained in:
data 2019-06-23 11:58:15 -04:00
commit 80ad0c3261
17 changed files with 3821 additions and 0 deletions

20
.eslintrc Normal file
View File

@ -0,0 +1,20 @@
{
"extends": ["eslint:recommended"],
"parserOptions": {
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true
},
"rules": {
"quotes": ["warn", "single"],
"semi": ["warn", "never"],
"indent": ["warn", 2],
"no-unused-vars": ["warn"],
"no-console": 0
}
}

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# dependencies
/node_modules
# production
/build
npm-debug.log*

4
.prettierrc Normal file
View File

@ -0,0 +1,4 @@
trailingComma: "es5"
tabWidth: 2
semi: false
singleQuote: true

0
README.md Normal file
View File

21
api/index.js Executable file
View File

@ -0,0 +1,21 @@
const router = require('express').Router()
module.exports = router
const ascii = require('../ascii')
router.use('/tasks', require('./tasks'))
router.use('/projects', require('./projects'))
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!!!!!!!')
error.status = 404
next(error)
})

54
api/projects.js Normal file
View File

@ -0,0 +1,54 @@
const router = require('express').Router()
const { Project } = 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({ name: req.body.name })
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)
project.destroy()
res.json(project)
} catch (err) {
next(err)
}
})

61
api/tasks.js Executable file
View File

@ -0,0 +1,61 @@
const router = require('express').Router()
const { Project, Task } = require('../db/models')
module.exports = router
/* CREATE */
router.post('/', async (req, res, next) => {
console.log(req.body)
const project = await Project.findOne({
where: { name: req.body.projectName },
})
await console.log('Project\n', project.id)
try {
const task = await Task.create(req.body)
await task.setProject(project.id)
await console.log('task.projectId:', task.projectId)
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({ name: req.body.name })
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)
task.destroy({ force: true })
res.json(task)
} catch (err) {
next(err)
}
})

26
ascii.js Normal file
View File

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

18
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
db/index.js Executable file
View File

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

7
db/models/index.js Executable file
View File

@ -0,0 +1,7 @@
const Project = require('./project')
const Task = require('./task')
Project.hasMany(Task)
Task.belongsTo(Project)
module.exports = { Task, Project }

11
db/models/project.js Normal file
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

16
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

42
db/seed.js Executable file
View File

@ -0,0 +1,42 @@
const db = require('../db')
const { Task, Project } = require('./models')
const testTasks = [
{
desc: 'make app',
completed: false,
projectId: 1,
},
{
desc: 'update backend',
completed: false,
projectId: 1,
},
{
desc: 'eat dinner',
completed: false,
},
]
const testProjects = [{ name: 'Anarchy Planet' }]
async function runSeed() {
await db.sync({ force: true })
console.log('db synced!')
console.log('seeding...')
try {
const projects = await Project.bulkCreate(testProjects)
const tasks = await Task.bulkCreate(testTasks)
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()

36
index.js Executable file
View File

@ -0,0 +1,36 @@
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.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'public', 'index.html'))
})
// 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}`)
})

3471
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

21
package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "tasks-backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"axios": "^0.19.0",
"concurrently": "^4.0.1",
"cors": "^2.8.5",
"express": "^4.17.1",
"http-proxy-middleware": "^0.19.0",
"morgan": "^1.9.1",
"nodemon": "^1.19.1",
"pg": "^7.11.0",
"sequelize": "^5.8.11"
},
"scripts": {
"seed": "node db/seed.js",
"start": "nodemon index.js"
}
}