This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
model_check.go
85 lines (77 loc) · 1.96 KB
/
model_check.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
package main
import (
"errors"
"fmt"
r "github.com/dancannon/gorethink"
)
type Check struct {
Id string `gorethink:"id" json:"id"`
Type string `gorethink:"type,omitempty" json:"type,omitempty"`
Name string `gorethink:"name" json:"name"`
Level int `gorethink:"level" json:"level"`
Title string `gorethink:"title" json:"title"`
Description string `gorethink:"description" json:"description"`
}
func (c *Check) Validate() error {
if c.Type == "" {
return errors.New("missing required field, type")
} else if c.Name == "" {
return errors.New("missing required field, name")
} else if c.Level == 0 {
return errors.New("invalid field, level, must be greater than 0")
} else if c.Level > 3 {
return errors.New("invalid field, level, must be less than 3")
}
return nil
}
func (c *Check) Save() error {
c.Id = fmt.Sprintf("%s-%s", c.Type, c.Name)
resp, err := r.Table("checks").
Insert(c, r.InsertOpts{Conflict: "replace"}).
RunWrite(session)
if err != nil {
return err
}
if resp.Inserted == 0 && resp.Replaced == 0 {
return errors.New("Unable to insert/replace Check")
}
return nil
}
func (c *Check) Delete() error {
resp, err := r.Table("checks").
Get(c.Id).
Delete().
RunWrite(session)
if err != nil {
return err
}
if resp.Deleted == 0 {
return errors.New("Unable to delete Check")
}
return nil
}
func GetChecksByType(check_type string) ([]Check, error) {
cur, err := r.Table("checks").GetAllByIndex("type", check_type).Run(session)
if err != nil {
return nil, err
}
defer cur.Close()
var checks []Check
err = cur.All(&checks)
if err != nil {
return nil, err
}
return checks, nil
}
func GetCheckByTypeName(check_type, name string) (check Check, err error) {
cur, err := r.Table("checks").GetAllByIndex("type_name", []string{check_type, name}).Run(session)
if err != nil {
return check, err
}
defer cur.Close()
err = cur.One(&check)
if err != nil {
return check, err
}
return check, nil
}