/
company.go
189 lines (161 loc) · 4.12 KB
/
company.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
/*
* SPDX-FileCopyrightText: 2022 Brian Welty
*
* SPDX-License-Identifier: MPL-2.0
*/
package model
import (
"log"
)
type Company struct {
Model
Name string `form:"company.Name"`
Symbol string `form:"company.Symbol"`
oldName string `gorm:"-:all"`
oldSymbol string `gorm:"-:all"`
// UserID only used from Security.Update
UserID uint `gorm:"-:all"`
}
func (c *Company) sanitizeInputs() {
sanitizeString(&c.Name)
sanitizeString(&c.Symbol)
}
func (c Company) GetName() string {
if c.Name != "" {
return c.Name
}
return c.Symbol
}
// Query if Company already exists and return.
func (c *Company) Get() *Company {
if c.Name == "" && c.Symbol == "" {
return nil
}
result := new(Company)
db := getDbManager()
db.Where("name = ?", c.Name).
Where("symbol = ?", c.Symbol).
First(result)
if result.ID > 0 {
log.Printf("[MODEL] FOUND EXISTING COMPANY(%d)", c.ID)
}
return result
}
// Query if Companies already exists and return them.
// XXX Possibly should disallow multiple Companies for same Symbol.
func (c *Company) GetBySymbol() []Company {
if c.Symbol == "" {
return nil
}
results := []Company{}
db := getDbManager()
db.Where("symbol = ?", c.Symbol).Find(&results)
if len(results) > 0 {
log.Printf("[MODEL] FOUND EXISTING COMPANIES(%d) FOR(%s)", c.ID)
}
return results
}
// Query if Company already exists and return.
// If non-existent, create one. But if this is an Update, first
// try to update the calling object Company first if only attached
// to one Security.
// XXX Possibly should disallow multiple Companies for same Symbol.
// Or in future have defined set of fixed Companies if ever consider
// to add support for financial statements.
func (c *Company) Create(isUpdate bool) *Company {
result := c.Get()
if result == nil {
return nil
} else if result.ID > 0 {
c = result
} else {
db := getDbManager()
// Before creating new Company, test if can just update the
// calling object Company if only has one Security (assumed to
// be the Security being updated). Racy! Maybe need the Save()
// to be in a hook attached to the Find().
if c.ID > 0 && isUpdate {
var numSecurities int64
db.Model(&Security{}).Where(&Security{CompanyID: c.ID}).
Count(&numSecurities)
if numSecurities == 1 {
db.Save(c)
log.Printf("[MODEL] UPDATED COMPANY(%d)", c.ID)
return c
}
}
db.Create(c)
spewModel(c)
log.Printf("[MODEL] CREATE COMPANY(%d)", c.ID)
}
return c
}
func (c *Company) updateAllowed() bool {
return (c.Name != "" || c.Symbol != "")
}
// Updates Company Name, if Symbol unchanged.
// But do not modify a Company that is attached to the
// Securities of other Users.
// Return true if Company.Name was updated.
func (c *Company) updateName() bool {
var err error
if !c.updateAllowed() {
return false
}
if c.oldSymbol == c.Symbol &&
c.oldName != c.Name {
db := getDbManager()
var numSecurities int64
// Verify Company not used by other Users' Securities
if c.UserID > 0 {
db.Model(&Security{}).
Where(&Security{CompanyID: c.ID}).
Where("user_id != ?", c.UserID).
Joins("Account").Count(&numSecurities)
}
if numSecurities == 0 {
result := db.Model(c).Update("Name", c.Name)
err = result.Error
if err == nil {
log.Printf("[MODEL] UPDATED COMPANY(%d) NAME(%s)",
c.ID, c.Name)
return true
}
}
}
return false
}
// Return true if Company.ID was updated.
func (c *Company) Update() bool {
if !c.updateAllowed() ||
(c.oldSymbol == c.Symbol && c.oldName == c.Name) {
return false
}
// First try update of just c.Name
if c.updateName() {
return false
}
newCompany := c.Create(true)
if newCompany == nil {
return false
}
c.ID = newCompany.ID
return true
}
// Find() for use with rails/ruby like REPL console (gomacro);
// controllers should not expose this as are no access controls
func (*Company) Find(ID uint) *Company {
db := getDbManager()
c := new(Company)
db.First(&c, ID)
return c
}
func (*Company) FindBySymbol(symbol string) *Company {
db := getDbManager()
c := new(Company)
db.Where("symbol = ?", symbol).First(&c)
return c
}
func (c *Company) Print() {
forceSpewModel(c, 0)
}