/
template.go
71 lines (57 loc) · 1.61 KB
/
template.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
package events
import (
"context"
"github.com/pkg/errors"
"github.com/sol-armada/admin/stores"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Template struct {
Id string `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
AutoStart bool `json:"auto_start" bson:"auto_start"`
Description string `json:"description" bson:"description"`
Cover string `json:"cover" bson:"cover"`
Positions []*Position `json:"positions" bson:"positions"`
CommsTier CommsTier `json:"comms_tier" bson:"comms_tier"`
}
func TemplateExists(name string) (bool, error) {
cur, err := stores.Templates.List(
bson.D{{Key: "name", Value: name}},
)
if err != nil {
return false, err
}
templates := []*Template{}
if err := cur.All(context.Background(), &templates); err != nil {
return false, err
}
for _, template := range templates {
if template.Name == name {
return true, nil
}
}
return false, nil
}
func GetAllTemplates() ([]*Template, error) {
cur, err := stores.Templates.List(bson.D{})
if err != nil {
return nil, err
}
templates := []*Template{}
if err := cur.All(context.Background(), &templates); err != nil {
return nil, err
}
return templates, nil
}
func (t *Template) Delete() error {
return stores.Templates.Delete(t.Name)
}
func (t *Template) Save() error {
store := stores.Templates
opts := options.Replace().SetUpsert(true)
if _, err := store.ReplaceOne(store.GetContext(), bson.D{{Key: "name", Value: t.Name}}, t, opts); err != nil {
return errors.Wrap(err, "saving event")
}
return nil
}