/
securities.go
265 lines (228 loc) · 6.8 KB
/
securities.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
package handlers
//go:generate make
import (
"errors"
"github.com/aclindsa/moneygo/internal/models"
"github.com/aclindsa/moneygo/internal/store"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
func SearchSecurityTemplates(search string, _type models.SecurityType, limit int64) []*models.Security {
upperSearch := strings.ToUpper(search)
var results []*models.Security
for i, security := range SecurityTemplates {
if strings.Contains(strings.ToUpper(security.Name), upperSearch) ||
strings.Contains(strings.ToUpper(security.Description), upperSearch) ||
strings.Contains(strings.ToUpper(security.Symbol), upperSearch) {
if _type == 0 || _type == security.Type {
results = append(results, &SecurityTemplates[i])
if limit != -1 && int64(len(results)) >= limit {
break
}
}
}
}
return results
}
func FindSecurityTemplate(name string, _type models.SecurityType) *models.Security {
for _, security := range SecurityTemplates {
if name == security.Name && _type == security.Type {
return &security
}
}
return nil
}
func FindCurrencyTemplate(iso4217 int64) *models.Security {
iso4217string := strconv.FormatInt(iso4217, 10)
for _, security := range SecurityTemplates {
if security.Type == models.Currency && security.AlternateId == iso4217string {
return &security
}
}
return nil
}
func UpdateSecurity(tx store.Tx, s *models.Security) (err error) {
user, err := tx.GetUser(s.UserId)
if err != nil {
return
} else if user.DefaultCurrency == s.SecurityId && s.Type != models.Currency {
return errors.New("Cannot change security which is user's default currency to be non-currency")
}
err = tx.UpdateSecurity(s)
if err != nil {
return
}
return nil
}
func ImportGetCreateSecurity(tx store.Tx, userid int64, security *models.Security) (*models.Security, error) {
security.UserId = userid
if len(security.AlternateId) == 0 {
// Always create a new local security if we can't match on the AlternateId
err := tx.InsertSecurity(security)
if err != nil {
return nil, err
}
return security, nil
}
securities, err := tx.FindMatchingSecurities(security)
if err != nil {
return nil, err
}
// First try to find a case insensitive match on the name or symbol
upperName := strings.ToUpper(security.Name)
upperSymbol := strings.ToUpper(security.Symbol)
for _, s := range *securities {
if (len(s.Name) > 0 && strings.ToUpper(s.Name) == upperName) ||
(len(s.Symbol) > 0 && strings.ToUpper(s.Symbol) == upperSymbol) {
return s, nil
}
}
// if strings.Contains(strings.ToUpper(security.Name), upperSearch) ||
// Try to find a partial string match on the name or symbol
for _, s := range *securities {
sUpperName := strings.ToUpper(s.Name)
sUpperSymbol := strings.ToUpper(s.Symbol)
if (len(upperName) > 0 && len(s.Name) > 0 && (strings.Contains(upperName, sUpperName) || strings.Contains(sUpperName, upperName))) ||
(len(upperSymbol) > 0 && len(s.Symbol) > 0 && (strings.Contains(upperSymbol, sUpperSymbol) || strings.Contains(sUpperSymbol, upperSymbol))) {
return s, nil
}
}
// Give up and return the first security in the list
if len(*securities) > 0 {
return (*securities)[0], nil
}
// If there wasn't even one security in the list, make a new one
err = tx.InsertSecurity(security)
if err != nil {
return nil, err
}
return security, nil
}
func SecurityHandler(r *http.Request, context *Context) ResponseWriterWriter {
user, err := GetUserFromSession(context.Tx, r)
if err != nil {
return NewError(1 /*Not Signed In*/)
}
if r.Method == "POST" {
if !context.LastLevel() {
securityid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if context.NextLevel() != "prices" {
return NewError(3 /*Invalid Request*/)
}
return PriceHandler(r, context, user, securityid)
}
var security models.Security
if err := ReadJSON(r, &security); err != nil {
return NewError(3 /*Invalid Request*/)
}
security.SecurityId = -1
security.UserId = user.UserId
err = context.Tx.InsertSecurity(&security)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return ResponseWrapper{201, &security}
} else if r.Method == "GET" {
if context.LastLevel() {
//Return all securities
var sl models.SecurityList
securities, err := context.Tx.GetSecurities(user.UserId)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
sl.Securities = securities
return &sl
} else {
securityid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if !context.LastLevel() {
if context.NextLevel() != "prices" {
return NewError(3 /*Invalid Request*/)
}
return PriceHandler(r, context, user, securityid)
}
security, err := context.Tx.GetSecurity(securityid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
return security
}
} else {
securityid, err := context.NextID()
if err != nil {
return NewError(3 /*Invalid Request*/)
}
if !context.LastLevel() {
if context.NextLevel() != "prices" {
return NewError(3 /*Invalid Request*/)
}
return PriceHandler(r, context, user, securityid)
}
if r.Method == "PUT" {
var security models.Security
if err := ReadJSON(r, &security); err != nil || security.SecurityId != securityid {
return NewError(3 /*Invalid Request*/)
}
security.UserId = user.UserId
err = UpdateSecurity(context.Tx, &security)
if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return &security
} else if r.Method == "DELETE" {
security, err := context.Tx.GetSecurity(securityid, user.UserId)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
err = context.Tx.DeleteSecurity(security)
if _, ok := err.(store.SecurityInUseError); ok {
return NewError(7 /*In Use Error*/)
} else if err != nil {
log.Print(err)
return NewError(999 /*Internal Error*/)
}
return SuccessWriter{}
}
}
return NewError(3 /*Invalid Request*/)
}
func SecurityTemplateHandler(r *http.Request, context *Context) ResponseWriterWriter {
if r.Method == "GET" {
var sl models.SecurityList
query, _ := url.ParseQuery(r.URL.RawQuery)
var limit int64 = -1
search := query.Get("search")
var _type models.SecurityType = 0
typestring := query.Get("type")
if len(typestring) > 0 {
_type = models.GetSecurityType(typestring)
if _type == 0 {
return NewError(3 /*Invalid Request*/)
}
}
limitstring := query.Get("limit")
if limitstring != "" {
limitint, err := strconv.ParseInt(limitstring, 10, 0)
if err != nil {
return NewError(3 /*Invalid Request*/)
}
limit = limitint
}
securities := SearchSecurityTemplates(search, _type, limit)
sl.Securities = &securities
return &sl
} else {
return NewError(3 /*Invalid Request*/)
}
}