forked from q6r/masteringdecisions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriterion.go
183 lines (157 loc) · 4.91 KB
/
criterion.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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
// Criterion represent a criterion for a decision
type Criterion struct {
CriterionID int `db:"criterion_id" json:"criterion_id"`
DecisionID int `db:"decision_id" json:"decision_id"` // inherited
Name string `db:"name" json:"name" binding:"required"`
Description string `db:"description" json:"description"`
Order float32 `db:"order" json:"order"`
}
// HCriterionInfo get the information of a specific
// criterion in a decision and return it as a json
// object
func HCriterionInfo(c *gin.Context) {
did := c.Param("decision_id")
cid := c.Param("criterion_id")
var cri Criterion
err := dbmap.SelectOne(&cri, "select * from criterion where criterion_id=$1 and decision_id=$2", cid, did)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("criterion id %v for decision id %v not found", cid, did)})
return
}
result := gin.H{"criterion": cri}
ServeResult(c, "criterion_info.js", result)
}
// HCriterionDelete deletes a criterion from a decision
func HCriterionDelete(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
cid, err := strconv.Atoi(c.Param("criterion_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
cri := &Criterion{CriterionID: cid, DecisionID: did}
err = cri.Destroy()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
result := gin.H{"result": "deleted"}
ServeResult(c, "criterion_deleted.js", result)
}
// HCriterionCreate creates a criterion for a decision
func HCriterionCreate(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 cri Criterion
err = c.Bind(&cri)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "Invalid criterion object"})
return
}
cri.DecisionID = did // inherited
err = cri.Save()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
result := gin.H{"criterion": cri}
c.Writer.Header().Set("Location",
fmt.Sprintf("/decision/%d/criterion/%d", did, cri.CriterionID))
ServeResult(c, "criterion_create.js", result)
}
// HCriterionUpdate updates a criterion
func HCriterionUpdate(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
cid, err := strconv.Atoi(c.Param("criterion_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
var cri Criterion
err = dbmap.SelectOne(&cri, "SELECT * FROM criterion WHERE decision_id=$1 and criterion_id=$2", did, cid)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("criterion %d for decision %d not found", cid, did)})
return
}
var json Criterion
err = c.Bind(&json)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": "Unable to parse decision object"})
return
}
newCriterion := Criterion{
CriterionID: cid,
DecisionID: did,
Name: json.Name,
Description: json.Description,
Order: json.Order,
}
_, err = dbmap.Update(&newCriterion)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to update criterion %d for decision %d", cid, did)})
return
}
result := gin.H{"criterion": newCriterion}
ServeResult(c, "criterion_update.js", result)
}
// Destroy removes a criterion from a decision
func (cri *Criterion) Destroy() error {
_, err := dbmap.Exec("DELETE FROM criterion WHERE criterion_id=$1 and decision_id=$2",
cri.CriterionID, cri.DecisionID)
if err != nil {
return fmt.Errorf("Unable to delete criterion %#v from database", cri)
}
// Remove the votes that have a vote for this destroied
// criterion and destroy them
var votes []Vote
_, _ = dbmap.Select(&votes, "select * from vote where criterion_id=$1", cri.CriterionID)
for _, vote := range votes {
if err := vote.Destroy(); err != nil {
return err
}
}
// Remove the ratings that have a rate for this destroied
// criterion and destroy them
var ratings []Rating
_, _ = dbmap.Select(&ratings, "select * from rating where criterion_id=$1", cri.CriterionID)
for _, rating := range ratings {
if err := rating.Destroy(); err != nil {
return err
}
}
return nil
}
// Save saves a criterion in the database
// Restrictions decision should exist
func (cri *Criterion) Save() error {
// See if there's a decision this belongs to
cobj, err := dbmap.Get(Decision{}, cri.DecisionID)
if err != nil || cobj == nil {
return fmt.Errorf("decision %d does not exist, criterion should belong to an existing decision", cri.DecisionID)
}
if err := dbmap.Insert(cri); err != nil {
return fmt.Errorf("Unable to insert criterion %#v to database", cri)
}
return nil
}