-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
369 lines (304 loc) · 9.98 KB
/
handlers.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path"
"github.com/gorilla/mux"
)
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the HomePage!")
fmt.Println("Endpoint Hit: homePage")
}
func createUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "Welcome to the create User!")
fmt.Println("Endpoint Hit: create user")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err.Error())
}
keyVal := make(map[string]string)
json.Unmarshal(body, &keyVal)
username := keyVal["username"]
fmt.Println("New username is: ", username)
_, err = db.Exec("INSERT INTO users (username) VALUES (?)", username)
if err != nil {
panic(err.Error())
}
fmt.Fprintf(w, "New user was created")
}
func returnAllUsers(w http.ResponseWriter, r *http.Request) {
if db == nil {
http.Error(w, "Database not initialized", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "Welcome to the returnAllUsers!")
fmt.Println("Endpoint Hit: returnAllUsers")
var users []User
results, err := db.Query("Select * from users")
if err != nil {
panic(err.Error())
}
for results.Next() {
var user User
err = results.Scan(&user.ID, &user.Username)
if err != nil {
panic(err.Error())
}
users = append(users, user)
}
json.NewEncoder(w).Encode(users)
}
func returnAllStocks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "Welcome to the returnAllStocks!")
fmt.Println("Endpoint Hit: returnAllStocks")
var stocks []Stock
results, err := db.Query("Select * from stocks")
if err != nil {
panic(err.Error())
}
for results.Next() {
var stock Stock
err = results.Scan(&stock.ID, &stock.Symbol, &stock.Name, &stock.Price, &stock.TotalShares)
if err != nil {
panic(err.Error())
}
stocks = append(stocks, stock)
}
json.NewEncoder(w).Encode(stocks)
}
func returnAllAccounts(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the returnAllAccounts!")
fmt.Println("Endpoint Hit: returnAllAccounts")
}
func returnAllTrades(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the returnAllTrades!")
fmt.Println("Endpoint Hit: returnAllTrades")
}
func returnAllTransfers(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the returnAllTransfers!")
fmt.Println("Endpoint Hit: returnAllTransfers")
}
// Return all orders in the DB
func returnAllOrders(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "Welcome to the returnAllOrders!")
fmt.Println("Endpoint Hit: returnAllOrders")
var orders []Orders
results, err := db.Query("select orders.id, users.username, stocks.symbol, shares from orders inner join users on orders.user_id = users.id inner join stocks on orders.stock_id = stocks.id")
if err != nil {
panic(err.Error())
}
for results.Next() {
var order Orders
err = results.Scan(&order.ID, &order.Username, &order.Symbol, &order.Shares)
if err != nil {
panic(err.Error())
}
orders = append(orders, order)
}
json.NewEncoder(w).Encode(orders)
}
func createOrder(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "Welcome to the Create Orders!")
fmt.Println("Endpoint Hit: CreateOrders")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err.Error())
}
// grab the attributes from the request
keyVal := make(map[string]int)
json.Unmarshal(body, &keyVal)
userid := keyVal["userid"]
stockid := keyVal["stockid"]
shares := keyVal["shares"]
// prepare insert statement
_, err = db.Exec("INSERT INTO orders (user_id, stock_id, shares) VALUES (?,?,?)", userid, stockid, shares)
if err != nil {
panic(err.Error())
}
fmt.Fprintf(w, "New order was created")
}
func createStock(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "Welcome to the Create Stock!")
fmt.Println("Endpoint Hit: CreateStock")
var stock Stock
// grab the values from the request body
err := json.NewDecoder(r.Body).Decode(&stock)
if err != nil {
fmt.Println("oh no, that didn't work", err)
//http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// grab the attributes from the request
symbol := stock.Symbol
name := stock.Name
price := stock.Price
totalshares := stock.TotalShares
// prepare insert statement
_, err = db.Exec("INSERT INTO stocks (symbol, name, price, total_shares) VALUES (?,?,?,?)", symbol, name, price, totalshares)
if err != nil {
panic(err.Error())
}
fmt.Fprintf(w, "New stock was created")
}
// User must be specified in request and then return users' portfolio (stocks owned and values)
func returnUsersAccounts(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var users []User
var orders []Orders
//fmt.Println("Request URI: ", path.Base(r.RequestURI))
username := path.Base(r.RequestURI)
// query for userid
results, err := db.Query("Select * from users where username = ?", username)
if err != nil {
panic(err.Error())
}
for results.Next() {
var user User
err := results.Scan(&user.ID, &user.Username)
if err != nil {
panic(err.Error())
}
users = append(users, user)
fmt.Println("Userid: ", users[0].ID)
}
// query the orders table with userid and return all rows
//select orders.id, users.username, stocks.symbol, shares from orders inner join users on orders.user_id = users.id inner join stocks on orders.stock_id = stocks.id
results, err = db.Query("select orders.id, users.username, stocks.symbol, shares from orders inner join users on orders.user_id = users.id inner join stocks on orders.stock_id = stocks.id where orders.user_id = ?", users[0].ID)
if err != nil {
panic(err.Error())
}
for results.Next() {
var order Orders
err = results.Scan(&order.ID, &order.Username, &order.Symbol, &order.Shares)
if err != nil {
panic(err.Error())
}
orders = append(orders, order)
}
json.NewEncoder(w).Encode(orders)
}
func getOrderHistory(w http.ResponseWriter, r *http.Request) {
// Authenticate user and get user_id from session or request
userID := 1 // Placeholder; replace with actual user authentication and retrieval
rows, err := db.Query("SELECT id, stock_id, shares FROM orders WHERE user_id = ?", userID)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
defer rows.Close()
var orders []map[string]interface{}
for rows.Next() {
var id, stockID, shares int
if err := rows.Scan(&id, &stockID, &shares); err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
order := map[string]interface{}{
"id": id,
"stock_id": stockID,
"shares": shares,
}
orders = append(orders, order)
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(orders); err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
func GetUserProfile(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userID := vars["id"]
// Replace with your database connection logic
db, err := sql.Open("mysql", "tradergo:password1@tcp(db:3306)/trader_go")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer db.Close()
var username string
err = db.QueryRow("SELECT username FROM users WHERE id = ?", userID).Scan(&username)
if err != nil {
if err == sql.ErrNoRows {
http.Error(w, "User not found", http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
userProfile := map[string]string{"id": userID, "username": username}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(userProfile)
}
func searchStocks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
queryParams := r.URL.Query()
symbol := queryParams.Get("symbol")
name := queryParams.Get("name")
var stocks []Stock
var rows *sql.Rows
var err error
if symbol != "" && name != "" {
rows, err = db.Query("SELECT * FROM stocks WHERE symbol LIKE ? OR name LIKE ?", "%"+symbol+"%", "%"+name+"%")
} else if symbol != "" {
rows, err = db.Query("SELECT * FROM stocks WHERE symbol LIKE ?", "%"+symbol+"%")
} else if name != "" {
rows, err = db.Query("SELECT * FROM stocks WHERE name LIKE ?", "%"+name+"%")
} else {
http.Error(w, "Please provide either a symbol or name query parameter", http.StatusBadRequest)
return
}
if err != nil {
panic(err.Error())
}
defer rows.Close()
for rows.Next() {
var stock Stock
err := rows.Scan(&stock.ID, &stock.Symbol, &stock.Name, &stock.Price, &stock.TotalShares)
if err != nil {
panic(err.Error())
}
stocks = append(stocks, stock)
}
if err := rows.Err(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(stocks)
}
func getKeyMetrics(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Example metrics: total number of users, total number of stocks, total number of orders
var metrics struct {
TotalUsers int `json:"total_users"`
TotalStocks int `json:"total_stocks"`
TotalOrders int `json:"total_orders"`
}
// Get total number of users
err := db.QueryRow("SELECT COUNT(*) FROM users").Scan(&metrics.TotalUsers)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
// Get total number of stocks
err = db.QueryRow("SELECT COUNT(*) FROM stocks").Scan(&metrics.TotalStocks)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
// Get total number of orders
err = db.QueryRow("SELECT COUNT(*) FROM orders").Scan(&metrics.TotalOrders)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(metrics)
}