forked from fabric8-services/fabric8-wit
/
tracker_repository.go
163 lines (145 loc) · 5.21 KB
/
tracker_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
package remoteworkitem
import (
"time"
"context"
"github.com/fabric8-services/fabric8-wit/application/repository"
"github.com/fabric8-services/fabric8-wit/errors"
"github.com/fabric8-services/fabric8-wit/log"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
govalidator "gopkg.in/asaskevich/govalidator.v4"
)
// APIStringTypeTracker helps to avoid string literal
const APIStringTypeTrackers = "trackers"
const trackersTableName = "trackers"
// GormTrackerRepository implements TrackerRepository using gorm
type GormTrackerRepository struct {
db *gorm.DB
}
// TrackerRepository encapsulate storage & retrieval of tracker configuration
type TrackerRepository interface {
repository.Exister
Load(ctx context.Context, ID uuid.UUID) (*Tracker, error)
Save(ctx context.Context, t *Tracker) (*Tracker, error)
Delete(ctx context.Context, ID uuid.UUID) error
Create(ctx context.Context, t *Tracker) error
List(ctx context.Context) ([]Tracker, error)
}
// NewTrackerRepository constructs a TrackerRepository
func NewTrackerRepository(db *gorm.DB) *GormTrackerRepository {
return &GormTrackerRepository{db}
}
// Create creates a new tracker configuration in the repository
// returns BadParameterError, ConversionError or InternalError
func (r *GormTrackerRepository) Create(ctx context.Context, t *Tracker) error {
//URL Validation
isValid := govalidator.IsURL(t.URL)
if isValid != true {
return BadParameterError{parameter: "url", value: t.URL}
}
_, present := RemoteWorkItemImplRegistry[t.Type]
// Ensure we support this remote tracker.
if present != true {
return BadParameterError{parameter: "type", value: t.Type}
}
if err := r.db.Create(&t).Error; err != nil {
return InternalError{simpleError{err.Error()}}
}
log.Info(ctx, map[string]interface{}{
"tracker": t,
}, "Tracker reposity created")
return nil
}
// Load returns the tracker configuration for the given id
// returns NotFoundError, ConversionError or InternalError
func (r *GormTrackerRepository) Load(ctx context.Context, ID uuid.UUID) (*Tracker, error) {
defer goa.MeasureSince([]string{"goa", "db", "tracker", "load"}, time.Now())
res := Tracker{}
tx := r.db.Where("id = ?", ID).Find(&res)
if tx.RecordNotFound() {
log.Error(ctx, map[string]interface{}{
"err": tx.Error,
"tracker_id": ID,
}, "tracker repository not found")
return nil, errors.NewNotFoundError("tracker", ID.String())
}
if tx.Error != nil {
log.Error(ctx, map[string]interface{}{
"err": tx.Error,
"tracker_id": ID,
}, "unable to load the tracker by ID")
return nil, errors.NewInternalError(ctx, tx.Error)
}
return &res, nil
}
// CheckExists returns nil if the given ID exists otherwise returns an error
func (r *GormTrackerRepository) CheckExists(ctx context.Context, id uuid.UUID) error {
return repository.CheckExists(ctx, r.db, trackersTableName, id)
}
// List returns tracker selected by the given criteria.Expression, starting with start (zero-based) and returning at most limit items
func (r *GormTrackerRepository) List(ctx context.Context) ([]Tracker, error) {
defer goa.MeasureSince([]string{"goa", "db", "tracker", "query"}, time.Now())
var objs []Tracker
err := r.db.Find(&objs).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return objs, nil
}
// Save updates the given tracker in storage.
// returns NotFoundError, ConversionError or InternalError
func (r *GormTrackerRepository) Save(ctx context.Context, t *Tracker) (*Tracker, error) {
defer goa.MeasureSince([]string{"goa", "db", "tracker", "save"}, time.Now())
res := Tracker{}
tx := r.db.Where("id = ?", t.ID).Find(&res)
if tx.RecordNotFound() {
log.Error(ctx, map[string]interface{}{
"err": tx.Error,
"tracker_id": t.ID,
}, "tracker repository not found")
return nil, errors.NewNotFoundError("tracker", t.ID.String())
}
_, present := RemoteWorkItemImplRegistry[t.Type]
// Ensure we support this remote tracker.
if present != true {
return nil, errors.NewBadParameterError("type", t.Type)
}
if err := tx.Save(&t).Error; err != nil {
log.Error(ctx, map[string]interface{}{
"tracker_id": t.ID,
"err": err,
}, "unable to save tracker repository")
return nil, errors.NewInternalError(ctx, err)
}
return t, nil
}
// Delete deletes the tracker with the given id
// returns NotFoundError or InternalError
func (r *GormTrackerRepository) Delete(ctx context.Context, ID uuid.UUID) error {
defer goa.MeasureSince([]string{"goa", "db", "tracker", "delete"}, time.Now())
if ID == uuid.Nil {
log.Error(ctx, map[string]interface{}{
"err": errors.NewNotFoundError("tracker", ID.String()),
"tracker_id": ID.String(),
}, "unable to find the tracker by ID")
return errors.NewNotFoundError("tracker", ID.String())
}
var t = Tracker{ID: ID}
tx := r.db.Delete(t)
if err := tx.Error; err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"tracker_id": ID.String(),
}, "unable to delete the space")
return errors.NewInternalError(ctx, err)
}
if tx.RowsAffected == 0 {
log.Error(ctx, map[string]interface{}{
"err": tx.Error,
"space_id": ID.String(),
}, "none row was affected by the deletion operation")
return errors.NewNotFoundError("space", ID.String())
}
return nil
}