-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.go
More file actions
182 lines (135 loc) · 4.14 KB
/
template.go
File metadata and controls
182 lines (135 loc) · 4.14 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package mongodb
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/sinfo/deck2/src/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// TemplateType holds collection info
type TemplateType struct {
Collection *mongo.Collection
}
// CreateTemplateData holds data needed to create a template
type TemplateData struct {
Requirements *[]models.Requirement `json:"requirements"`
}
// ParseBody fills the CreateTemplateData from a body
func (ctd *TemplateData) ParseCreateBody(body io.Reader) error {
if err := json.NewDecoder(body).Decode(ctd); err != nil {
return err
}
if ctd.Requirements == nil {
return errors.New("invalid name")
}
return nil
}
// ParseBody fills the CreateTemplateData from a body
func (ctd *TemplateData) ParseFillBody(body io.Reader) error {
if err := json.NewDecoder(body).Decode(ctd); err != nil {
return err
}
//TODO check body fields
if ctd.Requirements == nil {
return errors.New("invalid name")
}
return nil
}
func (t *TemplateType) UpdateTemplateUrl(templateID primitive.ObjectID, url string) (*models.Template, error) {
var updateQuery = bson.M{
"$set": bson.M{
"url": url,
},
}
var filterQuery = bson.M{"_id": templateID}
var optionsQuery = options.FindOneAndUpdate()
optionsQuery.SetReturnDocument(options.After)
var updatedTemplate models.Template
if err := t.Collection.FindOneAndUpdate(ctx, filterQuery, updateQuery, optionsQuery).Decode(&updatedTemplate); err != nil {
log.Println("error updating template:", err)
return nil, err
}
return &updatedTemplate, nil
}
// GetTemplate gets a template by its ID.
func (t *TemplateType) GetTemplate(templateID primitive.ObjectID) (*models.Template, error) {
ctx := context.Background()
var template models.Template
err := t.Collection.FindOne(ctx, bson.M{"_id": templateID}).Decode(&template)
if err != nil {
return nil, err
}
return &template, nil
}
// GetTemplatesOptions is the options to give to GetTemplates.
// All the fields are optional, and as such we use pointers as a "hack" to deal
// with non-existent fields.
// The field is non-existent if it has a nil value.
// This filter will behave like a logical *and*.
type GetTemplatesOptions struct {
EventID *int
Name *string
}
// GetTemplates gets all templates specified with a query
func (t *TemplateType) GetTemplates(tempOptions GetTemplatesOptions) ([]*models.Template, error) {
var template = make([]*models.Template, 0)
ctx := context.Background()
filter := bson.M{}
if tempOptions.EventID != nil {
filter["event"] = *tempOptions.EventID
}
if tempOptions.Name != nil {
filter["name"] = bson.M{
"$regex": fmt.Sprintf(".*%s.*", *tempOptions.Name),
"$options": "i",
}
}
cur, err := t.Collection.Find(ctx, filter)
if err != nil {
return nil, err
}
for cur.Next(ctx) {
// create a value into which the single document can be decoded
var t models.Template
err := cur.Decode(&t)
if err != nil {
return nil, err
}
template = append(template, &t)
}
if err := cur.Err(); err != nil {
return nil, err
}
cur.Close(ctx)
return template, nil
}
// CreateTemplate inserts a new template document and returns it.
func (t *TemplateType) CreateTemplate(template models.Template) (*models.Template, error) {
ctx := context.Background()
// ensure an ID is set
if template.ID.IsZero() {
template.ID = primitive.NewObjectID()
}
if _, err := t.Collection.InsertOne(ctx, template); err != nil {
return nil, err
}
return &template, nil
}
// UpdateTemplate updates name/event/kind fields of a template
func (t *TemplateType) UpdateTemplate(templateID primitive.ObjectID, update bson.M) (*models.Template, error) {
ctx := context.Background()
var filterQuery = bson.M{"_id": templateID}
var optionsQuery = options.FindOneAndUpdate()
optionsQuery.SetReturnDocument(options.After)
var updatedTemplate models.Template
if err := t.Collection.FindOneAndUpdate(ctx, filterQuery, bson.M{"$set": update}, optionsQuery).Decode(&updatedTemplate); err != nil {
return nil, err
}
return &updatedTemplate, nil
}