-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
237 lines (185 loc) · 5.9 KB
/
mysql.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
package flourish
import (
"github.com/jmoiron/sqlx"
"github.com/Masterminds/squirrel"
)
type MysqlStrainRepository struct {
DB *sqlx.DB
}
func (r MysqlStrainRepository) Create(s *Strain) (error) {
tx, err := r.DB.Begin()
if err != nil {
return err
}
res, err := tx.Exec(`INSERT INTO strains (name, race) VALUES (?, ?)`, s.Name, s.Race)
if err != nil {
tx.Rollback()
return err
}
lastInserted, err := res.LastInsertId()
if err != nil {
tx.Rollback()
return err
}
strainId := uint64(lastInserted)
s.Id = strainId
for _, flavor := range s.Flavors {
_, err = tx.Exec(`
INSERT INTO strain_flavors (strain_id, flavor_id) VALUES (?, (SELECT id FROM flavors WHERE name = ?))
`, strainId, flavor)
if err != nil {
tx.Rollback()
return err
}
}
for _, effect := range s.Effects.Positive {
_, err = tx.Exec(`INSERT INTO strain_effects (strain_id, effect_id) VALUES (?, (SELECT id FROM effects WHERE name = ? ))`, strainId, effect)
if err != nil {
tx.Rollback()
return err
}
}
for _, effect := range s.Effects.Negative {
_, err = tx.Exec(`INSERT INTO strain_effects (strain_id, effect_id) VALUES (?, (SELECT id FROM effects WHERE name = ? ))`, strainId, effect)
if err != nil {
tx.Rollback()
return err
}
}
for _, effect := range s.Effects.Medical {
_, err = tx.Exec(`INSERT INTO treatments (strain_id, symptom_id) VALUES (?, (SELECT id FROM symptoms WHERE name = ? ))`, strainId, effect)
if err != nil {
tx.Rollback()
return err
}
}
err = tx.Commit()
return nil
}
func (r MysqlStrainRepository) Save(s *Strain) error {
tx, err := r.DB.Begin()
if err != nil {
return err
}
// Go ahead and update the race/name in the event it was changed
_, err = tx.Exec(`
UPDATE strains SET
race = ?,
name = ?
WHERE id = ?
`, s.Race, s.Name, s.Id)
if err != nil {
tx.Rollback()
return err
}
// Since there's not a cache/relationship structure in the application, we will delete all
// associated records then re-insert
tx.Exec("DELETE FROM strain_effects WHERE strain_id = ?", s.Id)
tx.Exec("DELETE FROM strain_flavors WHERE strain_id = ?", s.Id)
tx.Exec("DELETE FROM treatments WHERE strain_id = ?", s.Id)
if err != nil {
tx.Rollback()
return err
}
for _, flavor := range s.Flavors {
_, err = tx.Exec(`
INSERT INTO strain_flavors (strain_id, flavor_id) VALUES (?, (SELECT id FROM flavors WHERE name = ?))
`, s.Id, flavor)
if err != nil {
tx.Rollback()
return err
}
}
for _, effect := range s.Effects.Positive {
_, err = tx.Exec(`INSERT INTO strain_effects (strain_id, effect_id) VALUES (?, (SELECT id FROM effects WHERE name = ? ))`, s.Id, effect)
if err != nil {
tx.Rollback()
return err
}
}
for _, effect := range s.Effects.Negative {
_, err = tx.Exec(`INSERT INTO strain_effects (strain_id, effect_id) VALUES (?, (SELECT id FROM effects WHERE name = ? ))`, s.Id, effect)
if err != nil {
tx.Rollback()
return err
}
}
for _, effect := range s.Effects.Medical {
_, err = tx.Exec(`INSERT INTO treatments (strain_id, symptom_id) VALUES (?, (SELECT id FROM symptoms WHERE name = ? ))`, s.Id, effect)
if err != nil {
tx.Rollback()
return err
}
}
return tx.Commit()
}
// Removes the strain from the database, and will cascade to any associative tables
func (r MysqlStrainRepository) Delete(id uint64) error {
_, err := r.DB.Exec("DELETE FROM strains WHERE id = ?", id)
return err
}
// Fetches records within the strains table based upon the provided optios
func (r MysqlStrainRepository) Search(options StrainSearchOptions) ([]*Strain, error) {
q := squirrel.Select("strains.id", "strains.name").From("strains")
if options.Name != "" {
q = q.Where(squirrel.Eq{"name": options.Name})
}
if options.Race != "" {
q = q.Where(squirrel.Eq{"race": options.Race })
}
if options.Flavor != "" {
q = q.Join("strain_flavors ON strains.id = strain_flavors.strain_id").Join("flavors ON flavors.id = strain_flavors.flavor_id AND flavors.name = ?", options.Flavor)
}
if options.Effect != "" {
q = q.Join("strain_effects effect ON strains.id = effect.strain_id").Join("effects e ON effect.effect_id = e.id AND e.name = ?", options.Effect)
}
if options.Treatment != "" {
q = q.Join("treatments t ON strains.id = t.strain_id").Join("symptoms s ON t.symptom_id = s.id AND s.name = ?", options.Treatment)
}
s, args, err := q.ToSql()
if err != nil {
return nil, err
}
var strains []*Strain
err = r.DB.Select(&strains, s, args...)
if err != nil {
return nil, err
}
return strains, nil
}
// Fetches the strain from the repository and builds the strain object from associations
func (r MysqlStrainRepository) Get(id uint64) (*Strain, error) {
var strain Strain
err := r.DB.Get(&strain, "SELECT * FROM strains WHERE id = ?", id)
if err != nil {
return nil, err
}
var flavors []string
err = r.DB.Select(&flavors, "SELECT name FROM flavors JOIN strain_flavors ON flavors.id = strain_flavors.flavor_id AND strain_flavors.strain_id = ?", id)
if err != nil {
return nil, err
}
var positiveEffects []string
err = r.DB.Select(&positiveEffects, "SELECT name FROM effects JOIN strain_effects ON effects.id = strain_effects.effect_id AND strain_effects.strain_id = ? AND effects.rating = 'positive'", id)
if err != nil {
return nil, err
}
var negativeEffects []string
err = r.DB.Select(&negativeEffects, "SELECT name FROM effects JOIN strain_effects ON effects.id = strain_effects.effect_id AND strain_effects.strain_id = ? AND effects.rating = 'negative'", id)
if err != nil {
return nil, err
}
var medicalTreatments []string
err = r.DB.Select(&medicalTreatments, "SELECT name FROM symptoms JOIN treatments ON symptoms.id = treatments.symptom_id AND treatments.strain_id = ?", id)
if err != nil {
return nil, err
}
effects := StrainEffects{
Positive: positiveEffects,
Negative: negativeEffects,
Medical: medicalTreatments,
}
strain.Flavors = flavors
strain.Effects = effects
return &strain, nil
}