-
Notifications
You must be signed in to change notification settings - Fork 77
/
team.go
205 lines (172 loc) · 4.74 KB
/
team.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
package team
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/luizalabs/teresa/pkg/server/database"
"github.com/luizalabs/teresa/pkg/server/teamext"
"github.com/luizalabs/teresa/pkg/server/teresa_errors"
"github.com/luizalabs/teresa/pkg/server/user"
"github.com/pkg/errors"
)
type Operations interface {
Create(name, email, url string) error
AddUser(name, userEmail string) error
List() ([]*database.Team, error)
ListByUser(userEmail string) ([]*database.Team, error)
RemoveUser(name, userEmail string) error
Rename(oldName, newName string) error
HasUser(name, userEmail string) (bool, error)
SetTeamExt(ext teamext.TeamExt)
}
type DatabaseOperations struct {
DB *gorm.DB
UserOps user.Operations
Ext teamext.TeamExt
}
func (dbt *DatabaseOperations) Create(name, email, url string) error {
t := new(database.Team)
if !dbt.DB.Where(&database.Team{Name: name}).First(t).RecordNotFound() {
return ErrTeamAlreadyExists
}
t.Name = name
t.Email = email
t.URL = url
return dbt.save(t)
}
func (dbt *DatabaseOperations) save(t *database.Team) error {
if err := dbt.DB.Save(t).Error; err != nil {
return teresa_errors.New(
teresa_errors.ErrInternalServerError,
errors.Wrap(err, fmt.Sprintf("saving team %s", t.Name)),
)
}
return nil
}
func (dbt *DatabaseOperations) AddUser(name, userEmail string) error {
t, err := dbt.getTeam(name)
if err != nil {
return err
}
u, err := dbt.UserOps.GetUser(userEmail)
if err != nil {
return err
}
usersOfTeam := []database.User{}
dbt.DB.Model(t).Association("Users").Find(&usersOfTeam)
for _, userOfTeam := range usersOfTeam {
if userOfTeam.Email == userEmail {
return ErrUserAlreadyInTeam
}
}
return dbt.DB.Model(t).Association("Users").Append(u).Error
}
func (dbt *DatabaseOperations) HasUser(name, userEmail string) (bool, error) {
t, err := dbt.getTeam(name)
if err != nil {
return false, err
}
usersOfTeam := []database.User{}
dbt.DB.Model(t).Association("Users").Find(&usersOfTeam)
for _, userOfTeam := range usersOfTeam {
if userOfTeam.Email == userEmail {
return true, nil
}
}
return false, nil
}
func (dbt *DatabaseOperations) List() ([]*database.Team, error) {
var teams []*database.Team
if err := dbt.DB.Find(&teams).Error; err != nil {
return nil, teresa_errors.New(
teresa_errors.ErrInternalServerError,
errors.Wrap(err, "finding teams"),
)
}
if err := dbt.findTeamUsers(teams); err != nil {
return nil, err
}
return teams, nil
}
func (dbt *DatabaseOperations) ListByUser(userEmail string) ([]*database.Team, error) {
u, err := dbt.UserOps.GetUser(userEmail)
if err != nil {
return nil, err
}
var teams []*database.Team
if err = dbt.DB.Model(u).Association("Teams").Find(&teams).Error; err != nil {
return nil, teresa_errors.New(
teresa_errors.ErrInternalServerError,
errors.Wrap(err, fmt.Sprintf("finding teams of user %s", userEmail)),
)
}
if err = dbt.findTeamUsers(teams); err != nil {
return nil, err
}
return teams, nil
}
func (dbt *DatabaseOperations) findTeamUsers(teams []*database.Team) error {
for _, t := range teams {
if err := dbt.DB.Model(t).Association("Users").Find(&t.Users).Error; err != nil {
return teresa_errors.New(
teresa_errors.ErrInternalServerError,
errors.Wrap(err, fmt.Sprintf("associating team %s with its users", t.Name)),
)
}
}
return nil
}
func (dbt *DatabaseOperations) getTeam(name string) (*database.Team, error) {
t := new(database.Team)
if dbt.DB.Where(&database.Team{Name: name}).First(t).RecordNotFound() {
return nil, ErrNotFound
}
return t, nil
}
func (dbt *DatabaseOperations) RemoveUser(name, userEmail string) error {
t, err := dbt.getTeam(name)
if err != nil {
return err
}
u, err := dbt.UserOps.GetUser(userEmail)
if err != nil {
return err
}
if err := dbt.DB.Model(t).Association("Users").Find(u).Error; err != nil {
return ErrUserNotInTeam
}
if err := dbt.DB.Model(t).Association("Users").Delete(u).Error; err != nil {
return teresa_errors.NewInternalServerError(err)
}
return nil
}
func (dbt *DatabaseOperations) Rename(oldName, newName string) error {
t := new(database.Team)
if !dbt.DB.Where(&database.Team{Name: newName}).First(t).RecordNotFound() {
return ErrTeamAlreadyExists
}
t, err := dbt.getTeam(oldName)
if err != nil {
return err
}
t.Name = newName
if err = dbt.save(t); err != nil {
return err
}
apps, err := dbt.Ext.ListByTeam(oldName)
if err != nil {
return err
}
for _, a := range apps {
if err = dbt.Ext.ChangeTeam(a, newName); err != nil {
return err
}
}
return nil
}
func (dbt *DatabaseOperations) SetTeamExt(ext teamext.TeamExt) {
dbt.Ext = ext
}
func NewDatabaseOperations(db *gorm.DB, uOps user.Operations) Operations {
db.AutoMigrate(&database.Team{})
return &DatabaseOperations{DB: db, UserOps: uOps}
}