-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (91 loc) · 2.64 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var koa = require('koa')
var Resource = require('koa-resource-router')
var koaBody = require('koa-better-body')
var knex = require('koa-knex')
var mount = require('koa-mount')
var path = require('path')
var cors = require('koa-cors')
const PORT = process.env.PORT || 4000
const app = module.exports = koa()
app.use(koaBody({
extendTypes: {
json: [ 'application/x-javascript' ],
}
}))
var database = 'eda_sim_dev'
const TARGET = process.env.npm_lifecycle_event
if (TARGET === 'test') {
database = 'eda_sim_test'
}
app.use(knex({
client: 'pg',
connection: {
host : process.env.DBHOST,
port : '5432',
database : process.env.DBNAME || database,
user: process.env.DBUSER,
password: process.env.DBPASSWORD
},
searchPath: 'public'
}))
const scores = new Resource('scores', {
index: function *(next) {
this.body = yield { scores: this.knex('scores') }
},
create: function *(next) {
try {
const res = yield this.knex('scores').returning('*').insert({
name: this.request.body.fields.score.name,
score: this.request.body.fields.score.score,
created_at: new Date(),
updated_at: new Date()
})
this.type = 'application/json'
this.status = 201
this.set('Location', `/scores/${res[0].id}`)
this.body = { scores: res[0] }
} catch (e) {
if (TARGET !== 'test') {
console.log('error', e)
}
this.status = 422
}
},
show: function *(next) {
const id = this.params.score
const res = yield this.knex.raw('SELECT * FROM SCORES WHERE ID = ?', [id])
if (res.rows.length === 1) {
this.body = { score: res.rows[0] }
} else {
this.status = 404
}
},
update: function *(next) {
const id = this.params.score
const prevObj = yield this.knex.raw('SELECT * FROM SCORES WHERE ID = ?', [id])
const res = yield this.knex('scores').returning('*').where('id', id).update({
name: this.request.body.fields.score.name || prevObj.rows[0].name,
score: this.request.body.fields.score.score || prevObj.rows[0].score,
updated_at: new Date()
})
if (res) {
this.body = { score: res[0] }
} else {
this.status = 404
}
},
destroy: function *(next) {
const id = this.params.score
const res = yield this.knex('scores').returning('*').where('id', id).del()
this.body = { message: `Deleted score with name of ${res[0].name} and id of ${id}` }
}
})
app.use(mount('/api/v1', scores.middleware()))
app.listen(PORT, () => {
console.log(`Listening on port ${PORT} . . .`)
})
const options = {
origin: '*',
methods: ['GET', 'POST']
}
app.use(cors(options))