forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_repository.go
132 lines (108 loc) · 3.11 KB
/
mongodb_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
package oauth2
import (
"github.com/asaskevich/govalidator"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
log "github.com/sirupsen/logrus"
)
const (
collectionName = "oauth_servers"
)
// Repository defines the behavior of a OAuth Server repo
type Repository interface {
FindAll() ([]*OAuth, error)
FindByName(name string) (*OAuth, error)
Add(oauth *OAuth) error
Save(oauth *OAuth) error
Remove(id string) error
}
// MongoRepository represents a mongodb repository
type MongoRepository struct {
session *mgo.Session
}
// NewMongoRepository creates a mongodb OAuth Server repo
func NewMongoRepository(session *mgo.Session) (*MongoRepository, error) {
return &MongoRepository{session}, nil
}
// FindAll fetches all the OAuth Servers available
func (r *MongoRepository) FindAll() ([]*OAuth, error) {
session, coll := r.getSession()
defer session.Close()
result := []*OAuth{}
err := coll.Find(nil).All(&result)
if err != nil {
return result, err
}
return result, nil
}
// FindByName find an OAuth Server by name
func (r *MongoRepository) FindByName(name string) (*OAuth, error) {
session, coll := r.getSession()
defer session.Close()
result := NewOAuth()
if err := coll.Find(bson.M{"name": name}).One(result); err != nil {
if err == mgo.ErrNotFound {
return nil, ErrOauthServerNotFound
}
return nil, err
}
return result, nil
}
// Add add a new OAuth Server to the repository
func (r *MongoRepository) Add(oauth *OAuth) error {
session, coll := r.getSession()
defer session.Close()
isValid, err := govalidator.ValidateStruct(oauth)
if !isValid && err != nil {
log.WithField("errors", err.Error()).Error("Validation errors")
return err
}
if err = coll.Insert(oauth); err != nil {
log.WithField("name", oauth.Name).
WithError(err).
Error("There was an error persisting the resource")
if mgo.IsDup(err) {
return ErrOauthServerNameExists
}
return err
}
log.WithField("name", oauth.Name).Debug("Resource persisted")
return nil
}
// Save saves OAuth Server to the repository
func (r *MongoRepository) Save(oauth *OAuth) error {
session, coll := r.getSession()
defer session.Close()
isValid, err := govalidator.ValidateStruct(oauth)
if !isValid && err != nil {
log.WithField("errors", err.Error()).Error("Validation errors")
return err
}
_, err = coll.Upsert(bson.M{"name": oauth.Name}, oauth)
if err != nil {
log.WithField("name", oauth.Name).
WithError(err).
Error("There was an error adding the resource")
return err
}
log.WithField("name", oauth.Name).Debug("Resource added")
return nil
}
// Remove removes an OAuth Server from the repository
func (r *MongoRepository) Remove(name string) error {
session, coll := r.getSession()
defer session.Close()
if err := coll.Remove(bson.M{"name": name}); err != nil {
log.WithField("name", name).
WithError(err).
Error("There was an error removing the resource")
return err
}
log.WithField("name", name).Debug("Resource removed")
return nil
}
func (r *MongoRepository) getSession() (*mgo.Session, *mgo.Collection) {
session := r.session.Copy()
coll := session.DB("").C(collectionName)
return session, coll
}