forked from evergreen-ci/evergreen
/
subscription.go
66 lines (52 loc) · 1.85 KB
/
subscription.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
package data
import (
"net/http"
"github.com/evergreen-ci/evergreen/model/event"
restModel "github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/gimlet"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
type DBSubscriptionConnector struct{}
func (dc *DBSubscriptionConnector) SaveSubscriptions(subscriptions []event.Subscription) error {
catcher := grip.NewSimpleCatcher()
for _, subscription := range subscriptions {
catcher.Add(subscription.Upsert())
}
return catcher.Resolve()
}
func (dc *DBSubscriptionConnector) GetSubscriptions(owner string, ownerType event.OwnerType) ([]restModel.APISubscription, error) {
if len(owner) == 0 {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: "no subscription owner provided",
}
}
subs, err := event.FindSubscriptionsByOwner(owner, ownerType)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch subscriptions")
}
apiSubs := make([]restModel.APISubscription, len(subs))
for i := range subs {
err = apiSubs[i].BuildFromService(subs[i])
if err != nil {
return nil, errors.Wrap(err, "failed to marshal subscriptions")
}
}
return apiSubs, nil
}
func (dc *DBSubscriptionConnector) DeleteSubscription(id string) error {
return event.RemoveSubscription(id)
}
type MockSubscriptionConnector struct {
MockSubscriptions []event.Subscription
}
func (mc *MockSubscriptionConnector) GetSubscriptions(user string, ownerType event.OwnerType) ([]restModel.APISubscription, error) {
return nil, errors.New("MockSubscriptionConnector unimplemented")
}
func (mc *MockSubscriptionConnector) SaveSubscriptions(subscriptions []event.Subscription) error {
return errors.New("MockSubscriptionConnector unimplemented")
}
func (dc *MockSubscriptionConnector) DeleteSubscription(id string) error {
return errors.New("MockSubscriptionConnector unimplemented")
}