-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
223 lines (199 loc) · 6.34 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import koa from 'koa'
import Resource from 'koa-resource-router'
import koaBody from 'koa-better-body'
import knex from 'koa-knex'
import mount from 'koa-mount'
import path from 'path'
const PORT = 4000
// Export the app for use in the tests
const app = module.exports = koa()
// Add the body parser to parse both multipart forms and JSON (for later use)
app.use(koaBody({
extendTypes: {
json: [ 'application/x-javascript' ],
}
}))
app.use(knex({
client: 'postgresql',
connection: {
database: 'text_invaders_dev'
}
}))
const players = new Resource('players', {
// GET /players
index: function *(next) {
// this.body = yield { players: this.knex('players') }
this.body = yield { players: this.knex('players') }
},
// POST /players
create: function *(next) {
try {
// One method is to use knex to build the query for you
const res = yield this.knex('players').returning('*').insert({
name: this.request.body.fields.player.name,
high_score: this.request.body.fields.player.high_score,
created_at: new Date(),
updated_at: new Date()
})
this.type = 'application/json'
this.status = 201
this.set('Location', `/players/${res[0].id}`)
this.body = { player: res[0] }
} catch (e) {
console.log('error', e)
this.status = 422
}
},
// GET /players/:id
show: function *(next) {
const id = this.params.player
const res = yield this.knex.raw('SELECT * FROM PLAYERS WHERE ID = ?', [id])
if (res.rows.length === 1) {
this.body = { player: res.rows[0] }
} else {
this.status = 404
}
},
// PUT /players/:id
update: function *(next) {
const id = this.params.player
const prevObj = yield this.knex.raw('SELECT * FROM PLAYERS WHERE ID = ?', [id])
const res = yield this.knex('players').returning('*').where('id', id).update({
name: this.request.body.fields.player.name || prevObj.rows[0].name,
high_score: this.request.body.fields.player.high_score || prevObj.rows[0].high_score,
updated_at: new Date()
})
if (res) {
this.body = { player: res[0] }
} else {
this.status = 404
}
},
// DELETE /players/:id
destroy: function *(next) {
const id = this.params.player
const res = yield this.knex('players').returning('*').where('id', id).del()
this.body = { message: `Deleted player with name of ${res[0].name} and id of ${id}` }
}
})
const games = new Resource('games', {
// GET /games
index: function *(next) {
this.body = yield { games: this.knex('games') }
},
// POST /games
create: function *(next) {
try {
const res = yield this.knex('games').returning('*').insert({
content: this.request.body.fields.game.content,
created_at: new Date(),
updated_at: new Date()
})
this.type = 'application/json'
this.status = 201
this.set('Location', `/games/${res[0].id}`)
this.body = { games: res[0] }
} catch (e) {
console.log('error', e)
this.status = 422
}
},
// GET /games/:id
show: function *(next) {
const id = this.params.game
const res = yield this.knex.raw('SELECT * FROM GAMES WHERE ID = ?', [id])
if (res.rows.length === 1) {
this.body = { games: res.rows[0] }
} else {
this.status = 404
}
},
// PUT /games/:id
update: function *(next) {
const id = this.params.game
const prevObj = yield this.knex.raw('SELECT * FROM GAMES WHERE ID = ?', [id])
const res = yield this.knex('games').returning('*').where('id', id).update({
content: this.request.body.fields.game.content || prevObj.rows[0].content,
updated_at: new Date()
})
if (res) {
this.body = { game: res[0] }
} else {
this.status = 404
}
},
// DELETE /games/:id
destroy: function *(next) {
const id = this.params.game
const res = yield this.knex('games').returning('*').where('id', id).del()
console.log(res)
this.body = { message: `Deleted game with content of ${res[0].content} and id of ${id}` }
}
})
const plays = new Resource('plays', {
// GET /players
index: function *(next) {
this.body = yield { plays: this.knex('plays') }
},
// POST /players
create: function *(next) {
try {
const res = yield this.knex('plays').returning('*').insert({
player_id: this.request.body.fields.play.player_id,
game_id: this.request.body.fields.play.game_id,
name: this.request.body.fields.play.name,
score: this.request.body.fields.play.score,
created_at: new Date(),
updated_at: new Date()
})
this.type = 'application/json'
this.status = 201
this.set('Location', `/plays/${res[0].id}`)
this.body = { plays: res[0] }
} catch (e) {
console.log('error', e)
this.status = 422
}
},
// GET /plays/:id
show: function *(next) {
const id = this.params.play
const res = yield this.knex.raw('SELECT * FROM PLAYS WHERE ID = ?', [id])
if (res.rows.length === 1) {
this.body = { plays: res.rows[0] }
} else {
this.status = 404
}
},
// PUT /plays/:id
update: function *(next) {
const id = this.params.play
const prevObj = yield this.knex.raw('SELECT * FROM PLAYS WHERE ID = ?', [id])
const res = yield this.knex('plays').returning('*').where('id', id).update({
player_id: this.request.body.fields.play.player_id || prevObj.rows[0].player_id,
game_id: this.request.body.fields.play.game_id || prevObj.rows[0].game_id,
score: this.request.body.fields.play.score || prevObj.rows[0].score,
name: this.request.body.fields.play.name || prevObj.rows[0].name,
created_at: new Date(),
updated_at: new Date()
})
if (res) {
this.body = { player: res[0] }
} else {
this.status = 404
}
},
// DELETE /plays/:id
destroy: function *(next) {
const id = this.params.play
const res = yield this.knex('plays').returning('*').where('id', id).del()
console.log(res)
this.body = { message: `Deleted play with a game id of ${res[0].game_id}, a player id of ${res[0].player_id} and id of ${id}` }
}
})
app.use(mount('/api/v1', players.middleware()))
app.use(mount('/api/v1', games.middleware()))
app.use(mount('/api/v1', plays.middleware()))
app.listen(PORT, () => {
console.log(`Listening on port ${PORT} . . .`)
})