summaryrefslogtreecommitdiff
path: root/models.go
diff options
context:
space:
mode:
authorJan Dittberner <jan@dittberner.info>2017-04-19 00:05:42 +0200
committerJan Dittberner <jan@dittberner.info>2017-04-22 00:12:38 +0200
commitbc194e8943bdec58ef462e393ef2a8a59d928718 (patch)
tree1d0ecdfe5c2eb131437ced9dfd2ec44484ad3763 /models.go
parentcc0f5c0b7b931440704bc31a858d52ae92585616 (diff)
downloadcacert-boardvoting-bc194e8943bdec58ef462e393ef2a8a59d928718.tar.gz
cacert-boardvoting-bc194e8943bdec58ef462e393ef2a8a59d928718.tar.xz
cacert-boardvoting-bc194e8943bdec58ef462e393ef2a8a59d928718.zip
Implement motion editing
Diffstat (limited to 'models.go')
-rw-r--r--models.go46
1 files changed, 44 insertions, 2 deletions
diff --git a/models.go b/models.go
index a8d24ad..8194dc9 100644
--- a/models.go
+++ b/models.go
@@ -19,6 +19,7 @@ const (
sqlCountOlderThanDecision
sqlCountOlderThanUnvotedDecision
sqlCreateDecision
+ sqlUpdateDecision
)
var sqlStatements = map[sqlKey]string{
@@ -92,6 +93,11 @@ INSERT INTO decisions (
BETWEEN date(:proposed) AND date(:proposed, '1 day')
)
)`,
+ sqlUpdateDecision: `
+UPDATE decisions
+SET proponent=:proponent, title=:title, content=:content,
+ votetype=:votetype, due=:due, modified=:modified
+WHERE id=:id`,
}
var db *sqlx.DB
@@ -376,7 +382,7 @@ func (d *Decision) OlderExists(unvoted bool, voter *Voter) (result bool, err err
return
}
-func (d *Decision) Save() (err error) {
+func (d *Decision) Create() (err error) {
insertDecisionStmt, err := db.PrepareNamed(sqlStatements[sqlCreateDecision])
if err != nil {
logger.Println("Error preparing statement:", err)
@@ -393,8 +399,8 @@ func (d *Decision) Save() (err error) {
lastInsertId, err := result.LastInsertId()
if err != nil {
logger.Println("Error getting id of inserted motion:", err)
+ return
}
- logger.Println("DEBUG new motion has id", lastInsertId)
getDecisionStmt, err := db.Preparex(sqlStatements[sqlLoadDecisionById])
if err != nil {
@@ -411,6 +417,42 @@ func (d *Decision) Save() (err error) {
return
}
+func (d *Decision) Update() (err error) {
+ updateDecisionStmt, err := db.PrepareNamed(sqlStatements[sqlUpdateDecision])
+ if err != nil {
+ logger.Println("Error preparing statement:", err)
+ return
+ }
+ defer updateDecisionStmt.Close()
+
+ result, err := updateDecisionStmt.Exec(d)
+ if err != nil {
+ logger.Println("Error updating motion:", err)
+ return
+ }
+ affectedRows, err := result.RowsAffected()
+ if err != nil {
+ logger.Print("Problem determining the affected rows")
+ return
+ } else if affectedRows != 1 {
+ logger.Printf("WARNING wrong number of affected rows: %d (1 expected)\n", affectedRows)
+ }
+
+ getDecisionStmt, err := db.Preparex(sqlStatements[sqlLoadDecisionById])
+ if err != nil {
+ logger.Println("Error preparing statement:", err)
+ return
+ }
+ defer getDecisionStmt.Close()
+
+ err = getDecisionStmt.Get(d, d.Id)
+ if err != nil {
+ logger.Println("Error loading updated motion:", err)
+ }
+
+ return
+}
+
func FindVoterByAddress(emailAddress string) (voter *Voter, err error) {
findVoterStmt, err := db.Preparex(sqlStatements[sqlLoadEnabledVoterByEmail])
if err != nil {