/
yobs.go
219 lines (196 loc) · 5.26 KB
/
yobs.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"fmt"
_ "github.com/lib/pq"
"net/http"
"os"
"strconv"
"encoding/json"
"log"
)
func ToJson(v interface{}) []byte{
result, _ := json.MarshalIndent(v, "", " ")
return result
}
func LogRequest(req *http.Request) {
log.Printf("%s %s\n", req.Method, req.URL)
}
func HasFacebookOrOwnerId(req *http.Request) (bool, string) {
params := req.URL.Query()
if len(params["facebook_id"]) < 1 {
if len(params["owner_id"]) < 1 {
return false, ""
} else {
return true, "owner_id"
}
} else {
if !(len(params["owner_id"]) < 1) {
// both were passed
return false, ""
} else {
return true, "facebook_id"
}
}
return false, ""
}
// HTTP handlers
func new_user(res http.ResponseWriter, req *http.Request) {
LogRequest(req)
params := req.URL.Query()
var facebook_id int64
facebook_id_int, _ := strconv.Atoi(params["facebook_id"][0])
facebook_id = int64(facebook_id_int)
result := NewUser(facebook_id)
user := result.Insert()
user_json := ToJson(user)
res.Header().Set("Content-Type", "application/json")
fmt.Fprintf(res, string(user_json))
}
func user(res http.ResponseWriter, req *http.Request) {
LogRequest(req)
params := req.URL.Query()
if len(params["facebook_id"]) > 0 {
facebook_id_int, _ := strconv.Atoi(params["facebook_id"][0])
// facebook id is present
facebook_id := int64(facebook_id_int)
user := UserFromFB(facebook_id)
if user.Id == 0 {
// no such user
NoSuchUser(res)
return
}
user_json := ToJson(user)
fmt.Fprintf(res, string(user_json))
} else if len(params["id"]) > 0 {
owner_id_int, _ := strconv.Atoi(params["id"][0])
owner_id := int64(owner_id_int)
user := UserFromID(owner_id)
if user.Facebook_id == 0 {
// no such user
NoSuchUser(res)
return
}
user_json := ToJson(user)
fmt.Fprintf(res, string(user_json))
} else {
var errors = map[string] string {
"error" : "Missing facebook_id or id",
}
e_json := ToJson(errors)
fmt.Fprintf(res, string(e_json))
}
}
func NoSuchUser(res http.ResponseWriter) {
var message = map[string] string {
"error" : "No such user.",
}
message_json := ToJson(message)
fmt.Fprintf(res, string(message_json))
}
func users(res http.ResponseWriter, req *http.Request) {
LogRequest(req)
res.Header().Set("Content-Type", "application/json")
users := Users()
users_json := ToJson(users)
fmt.Fprintf(res, string(users_json))
}
func NoSuchTransaction(res http.ResponseWriter) {
var message = map[string] string {
"error" : "No such transaction",
}
message_json := ToJson(message)
fmt.Fprintf(res, string(message_json))
}
func transaction(res http.ResponseWriter, req *http.Request) {
LogRequest(req)
res.Header().Set("Content-Type", "application/json")
params := req.URL.Query()
if len(params["id"]) > 0 {
id_int, _ := strconv.Atoi(params["id"][0])
id := int64(id_int)
transaction := TransactionFromID(id)
if transaction.Owner_id == 0 {
// no such transaction
NoSuchTransaction(res)
return
}
t_json := ToJson(transaction)
fmt.Fprintf(res, string(t_json))
} else {
// missing ID parameter
var errors = map[string] string {
"error" : "Missing id",
}
e_json := ToJson(errors)
fmt.Fprintf(res, string(e_json))
}
}
func transactions(res http.ResponseWriter, req *http.Request) {
LogRequest(req)
res.Header().Set("Content-Type", "application/json")
params := req.URL.Query()
if len(params["facebook_id"]) > 0 {
facebook_id_int, _ := strconv.Atoi(params["facebook_id"][0])
facebook_id := int64(facebook_id_int)
user := UserFromFB(facebook_id)
transactions := user.Transactions()
t_json := ToJson(transactions)
fmt.Fprintf(res, string(t_json))
} else {
transactions := Transactions()
t_json := ToJson(transactions)
fmt.Fprintf(res, string(t_json))
}
}
func new_transaction(res http.ResponseWriter, req *http.Request) {
LogRequest(req)
res.Header().Set("Content-Type", "application/json")
params := req.URL.Query()
valid, key := HasFacebookOrOwnerId(req)
if !valid {
var errors = map[string] string {
"error" : "Missing facebook_id or owner_id",
}
e_json := ToJson(errors)
fmt.Fprintf(res, string(e_json))
return
}
var owner_id int64
var facebook_id int64
var amount int64
var description string
if key == "owner_id" {
owner_id_int, _ := strconv.Atoi(params["owner_id"][0])
owner_id = int64(owner_id_int)
}
if key == "facebook_id" {
facebook_id_int, _ := strconv.Atoi(params["facebook_id"][0])
facebook_id = int64(facebook_id_int)
}
amount_int, _ := strconv.Atoi(params["amount"][0])
amount = int64(amount_int)
description = params["description"][0]
var result *Transaction
if key == "facebook_id" {
result, _ = NewTransactionByFB(facebook_id, amount, description)
} else {
result, _ = NewTransaction(owner_id, amount, description)
}
transaction := result.Insert()
t_json := ToJson(transaction)
fmt.Fprintf(res, string(t_json))
}
// entry point
func main() {
http.HandleFunc("/users", users)
http.HandleFunc("/user", user)
http.HandleFunc("/users/new", new_user)
http.HandleFunc("/transactions", transactions)
http.HandleFunc("/transaction", transaction)
http.HandleFunc("/transactions/new", new_transaction)
log.Printf("Listening on localhost:%s...\n", os.Getenv("PORT"))
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
if err != nil {
panic(err)
}
}