-
Notifications
You must be signed in to change notification settings - Fork 2
/
Server.coffee
32 lines (30 loc) · 1.38 KB
/
Server.coffee
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
Environment = require './Environment'
module.exports.koaResponse = (ctx, next) ->
locale = ctx.params.locale
id = ctx.params.id
environment = new Environment locale
return ctx.throw 404, "Can't find environment #{locale}" unless environment
card = JSON.parse(JSON.stringify(environment[id])) # Clone
return ctx.throw 404, "Can't find card #{id} in environment #{locale}" unless card
body = ctx.request.body
body = JSON.parse body if typeof body == 'string'
if body
card.attribute = environment.attributeName card if body.translateAttribute
card.race = environment.raceName card if body.translateRace
card.type = environment.prettyTypeName card if body.translateType
ctx.response.statusCode = 200
ctx.body = card
module.exports.expressResponse = (req, res) ->
locale = req.params.locale
id = req.params.id
environment = new Environment locale
return res.status(404).end "Can't find environment #{locale}" unless environment
card = JSON.parse(JSON.stringify(environment[id]))
return res.status(404).end "Can't find card #{id} in environment #{locale}" unless card
body = req.body
body = JSON.parse body if typeof body == 'string'
if body
card.attribute = environment.attributeName card if body.translateAttribute
card.race = environment.raceName card if body.translateRace
card.type = environment.prettyTypeName card if body.translateType
res.json card