-
Notifications
You must be signed in to change notification settings - Fork 1
/
alternative.go
170 lines (146 loc) · 4.63 KB
/
alternative.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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
// Alternative represent an alternative in a decision
type Alternative struct {
AlternativeID int `db:"alternative_id" json:"alternative_id"`
DecisionID int `db:"decision_id" json:"decision_id"`
Name string `db:"name" json:"name" binding:"required"`
Description string `db:"description" json:"description"`
Cost float32 `db:"cost" json:"cost"`
Order float32 `db:"order" json:"order"`
}
// HAlternativeCreate create a ballot that belongs
// to a decision
func HAlternativeCreate(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "invalid decision_id"})
return
}
var alt Alternative
if err := c.Bind(&alt); err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "invalid alternative object"})
return
}
alt.DecisionID = did
err = alt.Save()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
result := gin.H{"alternative": alt}
c.Writer.Header().Set("Location", fmt.Sprintf("/decision/%d/alternative/%d", alt.DecisionID, alt.AlternativeID))
ServeResult(c, "alternative_create.js", result)
}
// Save inserts a ballot into the database
func (alt *Alternative) Save() error {
// Check if decision exists or not
if dobj, err := dbmap.Get(Decision{}, alt.DecisionID); err != nil || dobj == nil {
return fmt.Errorf("Decision %d does not exists, can't create alternative without a decision", alt.DecisionID)
}
if err := dbmap.Insert(alt); err != nil {
return fmt.Errorf("Unable to insert alternative %#v to database", alt)
}
return nil
}
// HAlternativeUpdate updates an alternative
func HAlternativeUpdate(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
aid, err := strconv.Atoi(c.Param("alternative_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
var alt Alternative
err = dbmap.SelectOne(&alt, "SELECT * FROM alternative WHERE decision_id=$1 and alternative_id=$2", did, aid)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("alternative %d for decision %d not found", aid, did)})
return
}
var json Alternative
err = c.Bind(&json)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": "Unable to parse alternative object"})
return
}
newAlternative := Alternative{
AlternativeID: aid,
DecisionID: did,
Name: json.Name,
Description: json.Description,
Cost: json.Cost,
Order: json.Order,
}
_, err = dbmap.Update(&newAlternative)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to update alternative %d for decision %d", aid, did)})
return
}
result := gin.H{"alternative": newAlternative}
ServeResult(c, "alternative_update.js", result)
}
// HAlternativeDelete deletes an alternative from a decision
func HAlternativeDelete(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
aid, err := strconv.Atoi(c.Param("alternative_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
alt := &Alternative{AlternativeID: aid, DecisionID: did}
err = alt.Destroy()
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": err.Error()})
return
}
result := gin.H{"result": "deleted"}
ServeResult(c, "alternative_deleted.js", result)
}
// HAlternativeInfo gets the information for a specific
// alternative in a decision and returns an json object
// of the found alternative
func HAlternativeInfo(c *gin.Context) {
did := c.Param("decision_id")
aid := c.Param("alternative_id")
var alt Alternative
err := dbmap.SelectOne(&alt, "SELECT * FROM alternative where alternative_id=$1 and decision_id=$2", aid, did)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find alternative %v for decision %v", aid, did)})
return
}
result := gin.H{"alternative": alt}
ServeResult(c, "alternative_info.js", result)
}
// Destroy an alternative
func (alt *Alternative) Destroy() error {
if _, err := dbmap.Delete(alt); err != nil {
return fmt.Errorf("Unable to delete alternative %#v from database", alt)
}
// Remove votes beloning to this alternative
var votes []Vote
_, _ = dbmap.Select(&votes, "SELECT * FROM vote WHERE alternative_id=$1", alt.AlternativeID)
for _, v := range votes {
if err := v.Destroy(); err != nil {
return err
}
}
return nil
}