forked from q6r/masteringdecisions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecision.go
359 lines (315 loc) · 10.7 KB
/
decision.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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
// Decision represent a decision owned by Person_ID
type Decision struct {
DecisionID int `db:"decision_id" json:"decision_id"`
PersonID int `db:"person_id" json:"person_id" binding:"required"`
Name string `db:"name" json:"name" binding:"required"`
Description string `db:"description" json:"description" binding:"required"`
Stage int `db:"stage" json:"stage" binding:"required"`
CriterionVoteStyle string `db:"criterion_vote_style" json:"criterion_vote_style" binding:"required"`
AlternativeVoteStyle string `db:"alternative_vote_style" json:"alternative_vote_style" binding:"required"`
ClientSettings string `db:"client_settings" json:"client_settings"`
DisplayName string `db:"display_name" json:"display_name"`
CriteriaInstruction string `db:"criteria_instruction" json:"criteria_instruction"`
AlternativeInstruction string `db:"alternative_instruction" json:"alternative_instruction"`
Image string `db:"image" json:"image"`
}
// HDecisionBallotsList returns a list of ballots beloning
// to a decision, show all their information Using
// an array of BallotAllInfo
func HDecisionBallotsList(c *gin.Context) {
did := c.Param("decision_id")
var ballots []Ballot
_, err := dbmap.Select(&ballots, "SELECT * FROM ballot WHERE decision_id=$1", did)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("Unable to find ballots for decision id %v", did)})
return
}
var ais []BallotAllInfo
for _, b := range ballots {
var ai BallotAllInfo
ai.Name = b.Name
ai.Email = b.Email
ai.Sent = b.Sent
ai.URLDecision = fmt.Sprintf("/decision/%s/ballot/%d", did, b.BallotID)
// Get the votes for this ballot
_, err = dbmap.Select(&ai.Votes, "SELECT * FROM vote where ballot_id=$1", b.BallotID)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find votes for ballot %v", b.BallotID)})
return
}
// Get the ratings for this ballot
_, err = dbmap.Select(&ai.Ratings, "SELECT * FROM rating where ballot_id=$1", b.BallotID)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find votes for ballot %v", b.BallotID)})
return
}
ais = append(ais, ai)
}
result := gin.H{"ballots": ais}
ServeResult(c, "decision_ballots.js", result)
}
// HDecisionAlternativesList returns a list of alternatives beloning
// to a decision
func HDecisionAlternativesList(c *gin.Context) {
did := c.Param("decision_id")
var alts []Alternative
_, err := dbmap.Select(&alts, "SELECT * FROM alternative WHERE decision_id=$1 ORDER BY \"order\", \"name\" ASC", did)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find alternatives for decision id %s", did)})
return
}
result := gin.H{"alternatives": alts}
ServeResult(c, "decision_alternatives.js", result)
}
// HDecisionCriterionsList returns a list of criterions beloning
// to a decision
func HDecisionCriterionsList(c *gin.Context) {
did := c.Param("decision_id")
var cris []Criterion
_, err := dbmap.Select(&cris, "SELECT * FROM criterion WHERE decision_id=$1 ORDER BY \"order\", \"name\" ASC", did)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find criterion for decision %s", did)})
return
}
result := gin.H{"criterions": cris}
ServeResult(c, "decision_criterions.js", result)
}
// HDecisionDuplicate duplicates a decision by cloning
// its information, criterions, and alternatives
func HDecisionDuplicate(c *gin.Context) {
did := c.Param("decision_id")
// Get the decision to duplicate
// and its alternatives and criterions
dobj, err := dbmap.Get(Decision{}, did)
if err != nil || dobj == nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find decision %s", did)})
return
}
dsrc := dobj.(*Decision)
var cris []Criterion
_, _ = dbmap.Select(&cris, "select * from criterion where decision_id=$1", dsrc.DecisionID)
var alts []Alternative
_, _ = dbmap.Select(&alts, "select * from alternative where decision_id=$1", dsrc.DecisionID)
// This is auto incr we need to inset and
// determine its new value
dsrc.DecisionID = 0
if err := dbmap.Insert(dsrc); err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": "Unable to insert duplicated decision"})
return
}
// Get the new auto incr id
if err := dbmap.SelectOne(dsrc,
"select * from decision where decision_id=(select max(decision_id) from decision) and person_id=$1 and name=$2",
dsrc.PersonID, dsrc.Name); err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": "Unable to get duplicated decision"})
return
}
// Now change the criterions and alternatives
// decision ownership
for _, cri := range cris {
cri.DecisionID = dsrc.DecisionID
if err := cri.Save(); err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": "Unable to save criterion of duplicated decision"})
return
}
}
for _, alt := range alts {
alt.DecisionID = dsrc.DecisionID
if err := alt.Save(); err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": "Unable to save alternative of duplicated decision"})
return
}
}
result := gin.H{"decision": dsrc}
ServeResult(c, "decision_duplicate.js", result)
}
// HDecisionsList returns a list of all decision defined
// in the database their name and url only
func HDecisionsList(c *gin.Context) {
var decisions []Decision
_, err := dbmap.Select(&decisions, "SELECT * FROM decision")
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "Unable to find decisions in database"})
return
}
type Link struct {
Name string `json:"name"`
URL string `json:"url"`
}
var links []Link
for _, d := range decisions {
l := Link{Name: d.Name, URL: fmt.Sprintf("/decision/%d", d.DecisionID)}
links = append(links, l)
}
result := gin.H{"decisions": links}
ServeResult(c, "decisions_list.js", result)
}
// HDecisionInfo returns a decision information
// a decision object not it's stats
func HDecisionInfo(c *gin.Context) {
did := c.Param("decision_id")
var decision Decision
err := dbmap.SelectOne(&decision, "SELECT * FROM decision where decision_id=$1", did)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("Unable to find decisions with id %v", did)})
return
}
result := gin.H{"decision": decision}
ServeResult(c, "decision_info.js", result)
}
// HDecisionUpdate updates a decision
func HDecisionUpdate(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
var d Decision
err = dbmap.SelectOne(&d, "SELECT * FROM decision WHERE decision_id=$1", did)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("decision %d not found", did)})
return
}
var json Decision
err = c.Bind(&json)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": "Unable to parse decision object"})
return
}
newDecision := Decision{
DecisionID: did,
PersonID: json.PersonID,
Name: json.Name,
Description: json.Description,
Stage: json.Stage,
CriterionVoteStyle: json.CriterionVoteStyle,
AlternativeVoteStyle: json.AlternativeVoteStyle,
ClientSettings: json.ClientSettings,
DisplayName: json.DisplayName,
CriteriaInstruction: json.CriteriaInstruction,
AlternativeInstruction: json.AlternativeInstruction,
Image: json.Image,
}
_, err = dbmap.Update(&newDecision)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to update decision %d", did)})
return
}
result := gin.H{"decision": newDecision}
ServeResult(c, "decision_update.js", result)
}
// HDecisionCreate creates a decision beloning to a specific
// person
func HDecisionCreate(c *gin.Context) {
var decision Decision
err := c.Bind(&decision)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "invalid decision object"})
return
}
err = decision.Save()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
result := gin.H{"decision": decision}
c.Writer.Header().Set("Location", fmt.Sprintf("/decision/%d", decision.DecisionID))
ServeResult(c, "decision_create.js", result)
}
// HDecisionDelete deletes a decision from database
func HDecisionDelete(c *gin.Context) {
id, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
d := &Decision{DecisionID: id}
err = d.Destroy()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
result := gin.H{"result": "deleted"}
ServeResult(c, "decision_deleted.js", result)
}
// Destroy a decision from the database
// and remove it's dependencies such as ballots
// when destroying ballots they'll destroy their votes..etc
func (d *Decision) Destroy() error {
if _, err := dbmap.Delete(d); err != nil {
return fmt.Errorf("Unable to delete decision %#v from database", d)
}
// Remove the ballots of this decision
// removes the votes
var ballots []Ballot
_, _ = dbmap.Select(&ballots, "SELECT * FROM ballot WHERE decision_id=$1", d.DecisionID)
for _, b := range ballots {
err := b.Destroy()
if err != nil {
return err
}
}
// Remove criterions
// Does not remove anything..
var cris []Criterion
_, _ = dbmap.Select(&cris, "select * from criterion where decision_id=$1", d.DecisionID)
for _, cri := range cris {
err := cri.Destroy()
if err != nil {
return err
}
}
// Removing the alternatives remove the votes related to it
var alts []Alternative
_, _ = dbmap.Select(&alts, "select * from alternative where decision_id=$1", d.DecisionID)
for _, alt := range alts {
err := alt.Destroy()
if err != nil {
return err
}
}
return nil
}
// Save saves decision in the database
// Restriction : Decision can't be created without an existing owner
// Restriction : Decision can't be owned by two different people
func (d *Decision) Save() error {
// See if there's a person that this decision belongs to
// otherwise we quit
var p Person
err := dbmap.SelectOne(&p, "SELECT * FROM person WHERE person_id=$1", d.PersonID)
if err != nil {
return fmt.Errorf("person %d does not exist, can't create a decision without an owner", d.PersonID)
}
// Check ownership of decisions
var ds []Decision
_, _ = dbmap.Select(&ds, "select * from decision where decision_id=$1", d.DecisionID)
for _, i := range ds {
if i.PersonID != d.PersonID {
return fmt.Errorf("decision %d already owned by person %d", d.DecisionID, i.PersonID)
}
}
if err = dbmap.Insert(d); err != nil {
return fmt.Errorf("Unable to insert decision %#v to database", d)
}
return nil
}