forked from CaryLandholt/AngularFun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.coffee
40 lines (29 loc) · 894 Bytes
/
routes.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
33
34
35
36
37
38
39
40
nextId = 0
people = [
{"id": "#{nextId++}", "name": "Saasha", "age": "5"}
{"id": "#{nextId++}", "name": "Planet", "age": "7"}
]
isUniqueName = (name) ->
(name for person in people when person.name is name).length is 0
module.exports = (app, dir) ->
app.get '/', (req, res) ->
res.render "#{dir}/index.html"
app.get '/people', (req, res) ->
res.json people
app.post '/people', (req, res) ->
name = req.body.name
age = req.body.age
message =
"title": "Duplicate!"
"message": "#{name} is a duplicate. Please enter a new name."
return res.send(message, 403) if not isUniqueName name
person =
"id": "#{nextId++}"
"name": "#{name}"
"age": "#{age}"
people.push person
res.json person
app.get '/people/:id', (req, res) ->
id = req.params.id
current = person for person in people when parseInt(person.id, 10) is parseInt(id, 10)
res.json current