-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (145 loc) · 3.72 KB
/
main.go
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
package main
import (
"github.com/gorilla/mux"
"github.com/Sirupsen/logrus"
"fmt"
"net/http"
)
var log = logrus.New();
func main() {
log.Println("Hello Memosyne")
r := mux.NewRouter()
r.HandleFunc("/sessions/{id}", HandleSessionRead).Methods("GET")
r.HandleFunc("/sessions", HandleSessionCreate).Methods("POST")
r.HandleFunc("/users/{id}/contacts", HandleContactsRead).Methods("GET")
r.HandleFunc("/users/{id}/memos", HandleMemoRead).Methods("GET")
r.HandleFunc("/users/{id}", HandleUserRead).Methods("GET")
r.HandleFunc("/users", HandleUserCreate).Methods("POST")
r.HandleFunc("/memos", HandleMemoCreate).Methods("POST")
r.HandleFunc("/contacts", HandleContactsCreate).Methods("POST")
r.HandleFunc("/", HandleHelloWorld)
http.Handle("/", r)
log.Println("Now listening on port 8080")
http.ListenAndServe(":8080", nil)
}
func HandleSessionRead(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sessionId := vars["id"]
output := fmt.Sprintf("Hello, session %v", sessionId)
_ = output
s := `{ "session_id": 120, }`
w.Write([]byte(s))
}
func HandleSessionCreate(w http.ResponseWriter, r *http.Request) {
/* TODO get the json out of the request.
{
"user": {
"phone_number": "412-445-3171",
"password": "foobar",
}
}
*/
// TODO make a new session in the db. Get back its id
// return that id
s := `{
"session_id": 29,
"user_id": 120,
}`
w.Write([]byte(s))
}
func HandleContactsRead(w http.ResponseWriter, r *http.Request) {
s := `{
“contacts”: [
{
“contact_id”: 1,
“phone_number”: “412-445-3171”,
“first_name”: “Robbie”,
“last_name”: “McKinstry”,
}, {
“contact_id”: 2,
“phone_number”: “412-661-2963”,
“first_name”: “Malcolm”,
“last_name”: “Reynolds”,
}
],
}`
w.Write([]byte(s))
}
func HandleMemoRead(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sessionId := vars["id"]
_ = sessionId
// get all memos out of the database for a user.
// seralize those memos into json.
// then write that json
s := `{
“sender_id”: 0,
“contact_id”: 0,
“body”: “”,
“time”: “”,
"memos": [
{
"sender_id": 7,
"contact_id": 99,
"body": "Please, Jenny! I love you!",
"time": "2006-01-02T15:04:05Z07:00",
}, {
"sender_id": 8,
"contact_id": 100,
"body": "I aim to misbehave!"
"time": "2006-01-02T15:04:05Z07:00",
},
]
}`
w.Write([]byte(s))
}
// TODO implement this so that we use the userId variable to get the struct out of the database.
func HandleUserRead(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userId := vars["id"]
_ = userId
s := `{
"user": {
"user_id": 12,
"first_name": "Nigel",
"last_name": "Thornberry",
"email_address": "[email protected]",
},
}`
w.Write([]byte(s))
}
// what do i give back after a successful User New?
// TODO determine what I should be giving back. Then give it back.
func HandleUserCreate(w http.ResponseWriter, r *http.Request) {
s := `{
"session_id": 13,
"user_id": 201,
}`
w.Write([]byte(s))
}
func HandleMemoCreate(w http.ResponseWriter, r *http.Request) {
s :=` {
"memos": {
"sender_id": 7,
"contact_id": 99,
"body": "Please, Jenny! I love you!",
"time": "2006-01-02T15:04:05Z07:00",
},
}`
w.Write([]byte(s))
}
func HandleContactsCreate(w http.ResponseWriter, r *http.Request) {
// TODO implement
s := `{
“contact”: {
“contact_id”: 3,
“phone_number”: “724-903-3112”,
“first_name”: “Robert”,
“last_name”: “Floyd”,
},
}`
w.Write([]byte(s))
}
func HandleHelloWorld(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, Memosyne!"))
}