forked from kennygrant/gohackernews
/
query.go
58 lines (46 loc) · 1.47 KB
/
query.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
package resource
import (
"time"
"github.com/fragmenta/query"
)
// Query creates a new query relation referencing this specific resource by id.
func (r *Base) Query() *query.Query {
return query.New(r.Table(), r.PrimaryKey()).Where("id=?", r.ID)
}
// ValidateParams allows only those params by AllowedParams()
// to perform more sophisticated validation override it.
func (r *Base) ValidateParams(params map[string]string, allowed []string) map[string]string {
for k := range params {
paramAllowed := false
for _, v := range allowed {
if k == v {
paramAllowed = true
}
}
if !paramAllowed {
delete(params, k)
}
}
return params
}
// Create inserts a new database record and returns the id or an error
func (r *Base) Create(params map[string]string) (int64, error) {
// Make sure updated_at and created_at are set to the current time
now := query.TimeString(time.Now().UTC())
params["created_at"] = now
params["updated_at"] = now
// Insert a record into the database
id, err := query.New(r.Table(), r.PrimaryKey()).Insert(params)
return id, err
}
// Update the database record for this resource with the given params.
func (r *Base) Update(params map[string]string) error {
// Make sure updated_at is set to the current time
now := query.TimeString(time.Now().UTC())
params["updated_at"] = now
return r.Query().Update(params)
}
// Destroy deletes this resource by removing the database record.
func (r *Base) Destroy() error {
return r.Query().Delete()
}