-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttendeeController.swift
More file actions
44 lines (36 loc) · 1.19 KB
/
AttendeeController.swift
File metadata and controls
44 lines (36 loc) · 1.19 KB
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
import Vapor
import HTTP
final class AttendeeController: ResourceRepresentable {
typealias Item = Attendee
let drop: Droplet
init(droplet: Droplet) {
drop = droplet
}
func index(request: Request) throws -> ResponseRepresentable {
return try Attendee.all().makeNode().converted(to: JSON.self)
}
func store(request: Request) throws -> ResponseRepresentable {
var attendee = Attendee(
id: nil,
name: request.data["name"]?.string ?? "",
bio: request.data["bio"]?.string ?? "",
languages: request.data["languages"]?.string ?? ""
)
try attendee.save()
return attendee
}
func show(request: Request, item attendee: Attendee) throws -> ResponseRepresentable {
//User can be used like JSON with JsonRepresentable
return try JSON(node: [
"controller": "AttendeeController.show",
"attendee": attendee
])
}
func makeResource() -> Resource<Attendee> {
return Resource(
index: index,
store: store,
show: show
)
}
}