forked from fabric8-services/fabric8-wit
/
trackerquery_repository.go
230 lines (200 loc) · 6.87 KB
/
trackerquery_repository.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package remoteworkitem
import (
"fmt"
"strconv"
"github.com/fabric8io/almighty-core/app"
"github.com/fabric8io/almighty-core/log"
"github.com/fabric8io/almighty-core/rest"
"context"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
// GormTrackerQueryRepository implements TrackerRepository using gorm
type GormTrackerQueryRepository struct {
db *gorm.DB
}
// NewTrackerQueryRepository constructs a TrackerQueryRepository
func NewTrackerQueryRepository(db *gorm.DB) *GormTrackerQueryRepository {
return &GormTrackerQueryRepository{db}
}
// Create creates a new tracker query in the repository
// returns BadParameterError, ConversionError or InternalError
func (r *GormTrackerQueryRepository) Create(ctx context.Context, query string, schedule string, tracker string, spaceID uuid.UUID) (*app.TrackerQuery, error) {
tid, err := strconv.ParseUint(tracker, 10, 64)
if err != nil || tid == 0 {
// treating this as a not found error: the fact that we're using number internal is implementation detail
return nil, NotFoundError{"tracker", tracker}
}
log.Info(ctx, map[string]interface{}{
"tracker_id": tid,
}, "Tracker ID to be created")
tq := TrackerQuery{
Query: query,
Schedule: schedule,
TrackerID: tid,
SpaceID: spaceID,
}
tx := r.db
if err := tx.Create(&tq).Error; err != nil {
log.Error(ctx, map[string]interface{}{
"tracker_id": tid,
"tracker_query": query,
}, "unable to create the tracker query")
return nil, InternalError{simpleError{err.Error()}}
}
spaceSelfURL := rest.AbsoluteURL(goa.ContextRequest(ctx), app.SpaceHref(spaceID.String()))
tq2 := app.TrackerQuery{
ID: strconv.FormatUint(tq.ID, 10),
Query: query,
Schedule: schedule,
TrackerID: tracker,
Relationships: &app.TrackerQueryRelationships{
Space: app.NewSpaceRelation(spaceID, spaceSelfURL),
},
}
log.Info(ctx, map[string]interface{}{
"tracker_id": tid,
"tracker_query": tq,
}, "Created tracker query")
return &tq2, nil
}
// Load returns the tracker query for the given id
// returns NotFoundError, ConversionError or InternalError
func (r *GormTrackerQueryRepository) Load(ctx context.Context, ID string) (*app.TrackerQuery, error) {
id, err := strconv.ParseUint(ID, 10, 64)
if err != nil || id == 0 {
// treating this as a not found error: the fact that we're using number internal is implementation detail
return nil, NotFoundError{"tracker query", ID}
}
log.Info(ctx, map[string]interface{}{
"tracker_query_id": id,
}, "Loading the tracker query")
res := TrackerQuery{}
if r.db.First(&res, id).RecordNotFound() {
log.Error(ctx, map[string]interface{}{
"tracker_id": id,
}, "tracker resource not found")
return nil, NotFoundError{"tracker query", ID}
}
spaceSelfURL := rest.AbsoluteURL(goa.ContextRequest(ctx), app.SpaceHref(res.SpaceID.String()))
tq := app.TrackerQuery{
ID: strconv.FormatUint(res.ID, 10),
Query: res.Query,
Schedule: res.Schedule,
TrackerID: strconv.FormatUint(res.TrackerID, 10),
Relationships: &app.TrackerQueryRelationships{
Space: app.NewSpaceRelation(res.SpaceID, spaceSelfURL),
},
}
return &tq, nil
}
// Save updates the given tracker query in storage.
// returns NotFoundError, ConversionError or InternalError
func (r *GormTrackerQueryRepository) Save(ctx context.Context, tq app.TrackerQuery) (*app.TrackerQuery, error) {
res := TrackerQuery{}
id, err := strconv.ParseUint(tq.ID, 10, 64)
if err != nil || id == 0 {
return nil, NotFoundError{entity: "trackerquery", ID: tq.ID}
}
tid, err := strconv.ParseUint(tq.TrackerID, 10, 64)
if err != nil || tid == 0 {
// treating this as a not found error: the fact that we're using number internal is implementation detail
return nil, NotFoundError{"tracker", tq.TrackerID}
}
log.Info(ctx, map[string]interface{}{
"tracker_id": id,
}, "looking tracker query")
tx := r.db.First(&res, id)
if tx.RecordNotFound() {
log.Error(ctx, map[string]interface{}{
"tracker_id": id,
}, "tracker query not found")
return nil, NotFoundError{entity: "TrackerQuery", ID: tq.ID}
}
if tx.Error != nil {
return nil, InternalError{simpleError{fmt.Sprintf("could not load tracker query: %s", tx.Error.Error())}}
}
tx = r.db.First(&Tracker{}, tid)
if tx.RecordNotFound() {
log.Error(ctx, map[string]interface{}{
"tracker_id": id,
}, "tracker ID not found")
return nil, NotFoundError{entity: "tracker", ID: tq.TrackerID}
}
if tx.Error != nil {
return nil, InternalError{simpleError{fmt.Sprintf("could not load tracker: %s", tx.Error.Error())}}
}
newTq := TrackerQuery{
ID: id,
Schedule: tq.Schedule,
Query: tq.Query,
TrackerID: tid,
SpaceID: *tq.Relationships.Space.Data.ID,
}
if err := tx.Save(&newTq).Error; err != nil {
log.Error(ctx, map[string]interface{}{
"tracker_query": tq.Query,
"tracker_id": tid,
"err": err,
}, "unable to save the tracker query")
return nil, InternalError{simpleError{err.Error()}}
}
log.Info(ctx, map[string]interface{}{
"tracker_query": newTq,
}, "Updated tracker query")
spaceSelfURL := rest.AbsoluteURL(goa.ContextRequest(ctx), app.SpaceHref(tq.Relationships.Space.Data.ID.String()))
t2 := app.TrackerQuery{
ID: tq.ID,
Schedule: tq.Schedule,
Query: tq.Query,
TrackerID: tq.TrackerID,
Relationships: &app.TrackerQueryRelationships{
Space: app.NewSpaceRelation(*tq.Relationships.Space.Data.ID, spaceSelfURL),
},
}
return &t2, nil
}
// Delete deletes the tracker query with the given id
// returns NotFoundError or InternalError
func (r *GormTrackerQueryRepository) Delete(ctx context.Context, ID string) error {
var tq = TrackerQuery{}
id, err := strconv.ParseUint(ID, 10, 64)
if err != nil || id == 0 {
// treat as not found: clients don't know it must be a number
return NotFoundError{entity: "trackerquery", ID: ID}
}
tq.ID = id
tx := r.db
tx = tx.Delete(tq)
if err = tx.Error; err != nil {
return InternalError{simpleError{err.Error()}}
}
if tx.RowsAffected == 0 {
return NotFoundError{entity: "trackerquery", ID: ID}
}
return nil
}
// List returns tracker query selected by the given criteria.Expression, starting with start (zero-based) and returning at most limit items
func (r *GormTrackerQueryRepository) List(ctx context.Context) ([]*app.TrackerQuery, error) {
var rows []TrackerQuery
if err := r.db.Find(&rows).Error; err != nil {
return nil, errors.WithStack(err)
}
result := make([]*app.TrackerQuery, len(rows))
for i, tq := range rows {
spaceSelfURL := rest.AbsoluteURL(goa.ContextRequest(ctx), app.SpaceHref(tq.SpaceID.String()))
t := app.TrackerQuery{
ID: strconv.FormatUint(tq.ID, 10),
Schedule: tq.Schedule,
Query: tq.Query,
TrackerID: strconv.FormatUint(tq.TrackerID, 10),
Relationships: &app.TrackerQueryRelationships{
Space: app.NewSpaceRelation(tq.SpaceID, spaceSelfURL),
},
}
result[i] = &t
}
return result, nil
}