-
Notifications
You must be signed in to change notification settings - Fork 477
/
RbacRoleDataRepository.go
68 lines (60 loc) · 2.12 KB
/
RbacRoleDataRepository.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
package repository
import (
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
"go.uber.org/zap"
)
type RbacRoleDataRepository interface {
GetRoleDataForAllRoles() ([]*RbacRoleData, error)
CreateNewRoleDataForRoleWithTxn(model *RbacRoleData, tx *pg.Tx) (*RbacRoleData, error)
UpdateRoleDataForRoleWithTxn(model *RbacRoleData, tx *pg.Tx) (*RbacRoleData, error)
}
type RbacRoleDataRepositoryImpl struct {
logger *zap.SugaredLogger
dbConnection *pg.DB
}
func NewRbacRoleDataRepositoryImpl(logger *zap.SugaredLogger,
dbConnection *pg.DB) *RbacRoleDataRepositoryImpl {
return &RbacRoleDataRepositoryImpl{
logger: logger,
dbConnection: dbConnection,
}
}
type RbacRoleData struct {
TableName struct{} `sql:"rbac_role_data" pg:",discard_unknown_columns"`
Id int `sql:"id"`
Entity string `sql:"entity"`
AccessType string `sql:"access_type"`
Role string `sql:"role"`
RoleData string `sql:"role_data"`
RoleDisplayName string `sql:"role_display_name"`
RoleDescription string `sql:"role_description"`
IsPresetRole bool `sql:"is_preset_role,notnull"`
Deleted bool `sql:"deleted,notnull"`
sql.AuditLog
}
func (repo *RbacRoleDataRepositoryImpl) GetRoleDataForAllRoles() ([]*RbacRoleData, error) {
var models []*RbacRoleData
err := repo.dbConnection.Model(&models).Where("deleted = ?", false).Select()
if err != nil {
repo.logger.Errorw("error in getting role data for all roles", "err", err)
return nil, err
}
return models, nil
}
func (repo *RbacRoleDataRepositoryImpl) CreateNewRoleDataForRoleWithTxn(model *RbacRoleData, tx *pg.Tx) (*RbacRoleData, error) {
_, err := tx.Model(model).Insert()
if err != nil {
repo.logger.Errorw("error in creating role data for a role", "err", err)
return nil, err
}
return model, nil
}
func (repo *RbacRoleDataRepositoryImpl) UpdateRoleDataForRoleWithTxn(model *RbacRoleData, tx *pg.Tx) (*RbacRoleData, error) {
_, err := tx.Model(model).UpdateNotNull()
if err != nil {
repo.logger.Errorw("error in updating role data for a role", "err", err)
return nil, err
}
return model, nil
}