-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
143 lines (117 loc) · 2.84 KB
/
routes.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
package main
import (
"net/http"
"stockmark/model"
"strconv"
"github.com/pocketbase/pocketbase/core"
)
func OnPostLogin(e *core.RequestEvent) error {
body := struct {
Email string `json:"email"`
Password string `json:"password"`
}{}
err := e.BindBody(&body)
if err != nil {
return failure(e, err)
}
permit, err := model.Login(body.Email, body.Password)
if err != nil {
return failure(e, err)
}
page, err := permit.RenderPortfolio()
if err != nil {
return failure(e, err)
}
return e.JSON(http.StatusOK, map[string]any{
"success": true,
"permit": permit,
"data": page,
})
}
func OnPostRegister(e *core.RequestEvent) error {
body := struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
Password string `json:"password"`
}{}
err := e.BindBody(&body)
if err != nil {
return failure(e, err)
}
permit, err := model.Register(body.FirstName, body.LastName, body.Email, body.Password)
if err != nil {
return failure(e, err)
}
presentation, err := permit.RenderPortfolio()
if err != nil {
return failure(e, err)
}
return e.JSON(http.StatusOK, map[string]any{
"success": true,
"permit": permit,
"data": presentation,
})
}
func OnGetPortfolio(e *core.RequestEvent) error {
permit := model.Permit(e.Request.URL.Query().Get("permit"))
page, err := permit.RenderPortfolio()
if err != nil {
return failure(e, err)
}
return e.JSON(http.StatusOK, map[string]any{
"success": true,
"data": page,
})
}
func OnGetTrade(e *core.RequestEvent) error {
permit := model.Permit(e.Request.URL.Query().Get("permit"))
page, err := permit.RenderTrade()
if err != nil {
return failure(e, err)
}
return e.JSON(http.StatusOK, map[string]any{
"success": true,
"data": page,
})
}
func OnGetTransact(e *core.RequestEvent) error {
query := e.Request.URL.Query()
permit := model.Permit(query.Get("permit"))
action := query.Get("action")
ticker := query.Get("ticker")
amount := query.Get("amount")
i64, err := strconv.ParseInt(amount, 10, 64)
if err != nil {
return failure(e, model.ErrInvalidArguments)
}
switch action {
case "buy":
err = permit.Buy(ticker, int(i64))
case "sell":
err = permit.Sell(ticker, int(i64))
default:
err = model.ErrInvalidArguments
}
if err != nil {
return failure(e, err)
}
page, _ := permit.RenderTrade()
return e.JSON(http.StatusOK, map[string]any{
"success": true,
"data": page,
})
}
func OnGetDeposit(e *core.RequestEvent) error {
permit := model.Permit(e.Request.URL.Query().Get("permit"))
amount := e.Request.URL.Query().Get("amount")
i64, err := strconv.ParseInt(amount, 10, 64)
if err != nil {
return failure(e, model.ErrInvalidArguments)
}
err = permit.Deposit(int(i64))
if err != nil {
return failure(e, err)
}
return e.JSON(http.StatusOK, map[string]any{"success": true})
}