-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclarification.go
executable file
·136 lines (115 loc) · 2.64 KB
/
clarification.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
// Copyright 2014 The Cactus Authors. All rights reserved.
package data
import (
"database/sql"
"time"
"labix.org/v2/mgo/bson"
)
type Response int
const (
Unresponded Response = iota
Ignored
Answered
Broadcasted
)
type Clarification struct {
Id int64 `bson:"-" json:"id"`
AskerId int64 `json:"askerId"`
ProblemId int64 `json:"problemId"`
Question string `json:"question"`
Response Response `json:"response"`
Message string `json:"message"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
}
func ListClarifications() ([]*Clarification, error) {
rows, err := db.Query("SELECT id, struct FROM clarifications")
if err != nil {
return nil, err
}
clars := []*Clarification{}
for rows.Next() {
id := int64(0)
b := []byte{}
err = rows.Scan(&id, &b)
if err != nil {
return nil, err
}
c := &Clarification{}
err = bson.Unmarshal(b, c)
if err != nil {
return nil, err
}
c.Id = id
clars = append(clars, c)
}
return clars, nil
}
func ListClarificationsForAccount(acc *Account) ([]*Clarification, error) {
rows, err := db.Query("SELECT id, struct FROM clarifications WHERE asker_id=? OR response=?", acc.Id, Broadcasted)
if err != nil {
return nil, err
}
clars := []*Clarification{}
for rows.Next() {
id := int64(0)
b := []byte{}
err = rows.Scan(&id, &b)
if err != nil {
return nil, err
}
c := &Clarification{}
err = bson.Unmarshal(b, c)
if err != nil {
return nil, err
}
c.Id = id
clars = append(clars, c)
}
return clars, nil
}
func GetClarification(id int64) (*Clarification, error) {
b := []byte{}
err := db.QueryRow("SELECT struct FROM clarifications WHERE id=?", id).
Scan(&b)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
c := &Clarification{}
err = bson.Unmarshal(b, c)
if err != nil {
return nil, err
}
c.Id = id
return c, nil
}
func (c *Clarification) Put() error {
c.Modified = time.Now()
if c.Id == 0 {
c.Created = c.Modified
b, err := bson.Marshal(c)
if err != nil {
return err
}
res, err := db.Exec("INSERT INTO clarifications(asker_id, response, struct, created, modified) VALUES(?, ?, ?, ?, ?)", c.AskerId, c.Response, b, c.Created, c.Modified)
if err != nil {
return err
}
c.Id, err = res.LastInsertId()
return err
} else {
b, err := bson.Marshal(c)
if err != nil {
return err
}
_, err = db.Exec("UPDATE clarifications SET asker_id=?, response=?, struct=?, modified=? WHERE id=?", c.AskerId, c.Response, b, c.Modified, c.Id)
return err
}
}
func (c *Clarification) Del() error {
_, err := db.Exec("DELETE FROM clarifications WHERE id=?", c.Id)
return err
}