-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-app.js
106 lines (94 loc) · 2.49 KB
/
simple-app.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
const express = require('express')
module.exports = client => {
const app = express()
app.get('/events', async (req, res) => {
try {
if (req.query.page == 'all') {
let page = 0
let next = await client.events.findAll({ page })
let results = next.slice()
while (next.length > 0) {
page++
next = await client.events.findAll({ page })
results = results.concat(next)
}
return res.json(results)
} else {
const result = await client.events.findAll({
page: req.query.page || 0
})
return res.json(result)
}
} catch (ex) {
console.error(ex)
return res.status(400).send(ex)
}
})
app.get('/events/:id', async (req, res) => {
try {
const result = await client.events.one(req.params.id)
return res.json(result)
} catch (ex) {
console.error(ex)
return res.status(400).send(ex)
}
})
app.post('/events', async (req, res) => {
try {
const result = await client.events.create(req.body)
return res.json(result)
} catch (ex) {
console.error(ex)
return res.status(400).send(ex)
}
})
app.post('/events/:id', async (req, res) => {
try {
const result = await client.events.edit(req.params.id, req.body)
return res.json(result)
} catch (ex) {
console.error(ex)
return res.status(400).send(ex)
}
})
app.get('/events/:id/rsvps', async (req, res) => {
try {
const result = await client.attendances.findAll({
event: req.params.id,
page: req.query.page || 0
})
return res.json(result)
} catch (ex) {
console.error(ex)
return res.status(400).send(ex)
}
})
app.get('/events/:id/rsvp-count', async (req, res) => {
try {
const result = await client.attendances.count({ event: req.params.id })
return res.json(result)
} catch (ex) {
console.error(ex)
return res.status(400).send(ex)
}
})
app.delete('/events/:id', async (req, res) => {
try {
const result = await client.events.delete(req.params.id)
return res.json(result)
} catch (ex) {
console.error(ex)
return res.status(400).send(ex)
}
})
app.get('/people/:id', async (req, res) => {
try {
const result = await client.people.one(req.params.id)
return res.json(result)
} catch (ex) {
console.error(ex)
return res.status(400).send(ex)
}
})
return app
}