Skip to content

Commit

Permalink
MF-541 - Replace GroupRoles with GroupMember and remove GroupRoles
Browse files Browse the repository at this point in the history
  • Loading branch information
majabirmancevic committed Nov 11, 2024
1 parent 310af06 commit 0337309
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 131 deletions.
4 changes: 2 additions & 2 deletions pkg/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ func (svc *mainfluxThings) ListChannelsByGroup(_ context.Context, token, groupID
panic("not implemented")
}

func (svc *mainfluxThings) CreateRolesByGroup(_ context.Context, token, groupID string, gps ...things.GroupRoles) error {
func (svc *mainfluxThings) CreateRolesByGroup(_ context.Context, token string, gms ...things.GroupMember) error {
panic("not implemented")
}

func (svc *mainfluxThings) ListRolesByGroup(_ context.Context, token, groupID string, pm things.PageMetadata) (things.GroupMembersPage, error) {
panic("not implemented")
}

func (svc *mainfluxThings) UpdateRolesByGroup(_ context.Context, token, groupID string, gps ...things.GroupRoles) error {
func (svc *mainfluxThings) UpdateRolesByGroup(_ context.Context, token string, gms ...things.GroupMember) error {
panic("not implemented")
}

Expand Down
18 changes: 10 additions & 8 deletions things/api/http/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,16 +941,17 @@ func createRolesByGroupEndpoint(svc things.Service) endpoint.Endpoint {
return nil, err
}

var gps []things.GroupRoles
var gms []things.GroupMember
for _, g := range req.GroupMembers {
gp := things.GroupRoles{
gp := things.GroupMember{
MemberID: g.ID,
GroupID: req.groupID,
Role: g.Role,
}
gps = append(gps, gp)
gms = append(gms, gp)
}

if err := svc.CreateRolesByGroup(ctx, req.token, req.groupID, gps...); err != nil {
if err := svc.CreateRolesByGroup(ctx, req.token, gms...); err != nil {
return nil, err
}

Expand All @@ -965,16 +966,17 @@ func updateRolesByGroupEndpoint(svc things.Service) endpoint.Endpoint {
return nil, err
}

var gps []things.GroupRoles
var gms []things.GroupMember
for _, g := range req.GroupMembers {
gp := things.GroupRoles{
gp := things.GroupMember{
MemberID: g.ID,
GroupID: req.groupID,
Role: g.Role,
}
gps = append(gps, gp)
gms = append(gms, gp)
}

if err := svc.UpdateRolesByGroup(ctx, req.token, req.groupID, gps...); err != nil {
if err := svc.UpdateRolesByGroup(ctx, req.token, gms...); err != nil {
return nil, err
}

Expand Down
12 changes: 6 additions & 6 deletions things/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,16 +453,16 @@ func (lm *loggingMiddleware) ListChannelsByGroup(ctx context.Context, token, gro
return lm.svc.ListChannelsByGroup(ctx, token, groupID, pm)
}

func (lm *loggingMiddleware) CreateRolesByGroup(ctx context.Context, token, groupID string, gps ...things.GroupRoles) (err error) {
func (lm *loggingMiddleware) CreateRolesByGroup(ctx context.Context, token string, gms ...things.GroupMember) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method create_roles_by_group for id %s took %s to complete", groupID, time.Since(begin))
message := fmt.Sprintf("Method create_roles_by_group took %s to complete", time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
}(time.Now())

return lm.svc.CreateRolesByGroup(ctx, token, groupID, gps...)
return lm.svc.CreateRolesByGroup(ctx, token, gms...)
}

func (lm *loggingMiddleware) ListRolesByGroup(ctx context.Context, token, groupID string, pm things.PageMetadata) (gpp things.GroupMembersPage, err error) {
Expand All @@ -477,16 +477,16 @@ func (lm *loggingMiddleware) ListRolesByGroup(ctx context.Context, token, groupI
return lm.svc.ListRolesByGroup(ctx, token, groupID, pm)
}

func (lm *loggingMiddleware) UpdateRolesByGroup(ctx context.Context, token, groupID string, gps ...things.GroupRoles) (err error) {
func (lm *loggingMiddleware) UpdateRolesByGroup(ctx context.Context, token string, gms ...things.GroupMember) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update_roles_by_group for id %s took %s to complete", groupID, time.Since(begin))
message := fmt.Sprintf("Method update_roles_by_group took %s to complete", time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
}(time.Now())

return lm.svc.UpdateRolesByGroup(ctx, token, groupID, gps...)
return lm.svc.UpdateRolesByGroup(ctx, token, gms...)
}

func (lm *loggingMiddleware) RemoveRolesByGroup(ctx context.Context, token, groupID string, memberIDs ...string) (err error) {
Expand Down
8 changes: 4 additions & 4 deletions things/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,13 @@ func (ms *metricsMiddleware) ViewGroupByChannel(ctx context.Context, token, chan
return ms.svc.ViewGroupByChannel(ctx, token, channelID)
}

func (ms *metricsMiddleware) CreateRolesByGroup(ctx context.Context, token, groupID string, gps ...things.GroupRoles) error {
func (ms *metricsMiddleware) CreateRolesByGroup(ctx context.Context, token string, gms ...things.GroupMember) error {
defer func(begin time.Time) {
ms.counter.With("method", "create_roles_by_group").Add(1)
ms.latency.With("method", "create_roles_by_group").Observe(time.Since(begin).Seconds())
}(time.Now())

return ms.svc.CreateRolesByGroup(ctx, token, groupID, gps...)
return ms.svc.CreateRolesByGroup(ctx, token, gms...)
}

func (ms *metricsMiddleware) ListRolesByGroup(ctx context.Context, token, groupID string, pm things.PageMetadata) (things.GroupMembersPage, error) {
Expand All @@ -345,13 +345,13 @@ func (ms *metricsMiddleware) ListRolesByGroup(ctx context.Context, token, groupI
return ms.svc.ListRolesByGroup(ctx, token, groupID, pm)
}

func (ms *metricsMiddleware) UpdateRolesByGroup(ctx context.Context, token, groupID string, gps ...things.GroupRoles) error {
func (ms *metricsMiddleware) UpdateRolesByGroup(ctx context.Context, token string, gms ...things.GroupMember) error {
defer func(begin time.Time) {
ms.counter.With("method", "update_roles_by_group").Add(1)
ms.latency.With("method", "update_roles_by_group").Observe(time.Since(begin).Seconds())
}(time.Now())

return ms.svc.UpdateRolesByGroup(ctx, token, groupID, gps...)
return ms.svc.UpdateRolesByGroup(ctx, token, gms...)
}

func (ms *metricsMiddleware) RemoveRolesByGroup(ctx context.Context, token, groupID string, memberIDs ...string) error {
Expand Down
5 changes: 3 additions & 2 deletions things/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ func (ts *thingsService) CreateGroups(ctx context.Context, token string, groups
return []Group{}, err
}

role := GroupRoles{
role := GroupMember{
MemberID: ownerID,
GroupID: gr.ID,
Role: Admin,
}

if err := ts.roles.SaveRolesByGroup(ctx, gr.ID, role); err != nil {
if err := ts.roles.SaveRolesByGroup(ctx, role); err != nil {
return []Group{}, err
}

Expand Down
17 changes: 7 additions & 10 deletions things/mocks/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,24 @@ var _ things.RolesRepository = (*rolesRepositoryMock)(nil)
type rolesRepositoryMock struct {
mu sync.Mutex
groupRoles map[string]things.GroupMember
groupRolesByID map[string]things.GroupRoles
groupRolesByID map[string]things.GroupMember
}

// NewRolesRepository returns mock of roles repository
func NewRolesRepository() things.RolesRepository {
return &rolesRepositoryMock{
groupRoles: make(map[string]things.GroupMember),
groupRolesByID: make(map[string]things.GroupRoles),
groupRolesByID: make(map[string]things.GroupMember),
}
}

func (mrm *rolesRepositoryMock) SaveRolesByGroup(_ context.Context, groupID string, gps ...things.GroupRoles) error {
func (mrm *rolesRepositoryMock) SaveRolesByGroup(_ context.Context, gms ...things.GroupMember) error {
mrm.mu.Lock()
defer mrm.mu.Unlock()

for _, gp := range gps {
mrm.groupRoles[groupID] = things.GroupMember{
MemberID: gp.MemberID,
Role: gp.Role,
}
mrm.groupRolesByID[gp.MemberID] = gp
for _, g := range gms {
mrm.groupRoles[g.GroupID] = g
mrm.groupRolesByID[g.MemberID] = g
}

return nil
Expand Down Expand Up @@ -74,7 +71,7 @@ func (mrm *rolesRepositoryMock) RetrieveAllRolesByGroup(_ context.Context) ([]th

return gps, nil
}
func (mrm *rolesRepositoryMock) UpdateRolesByGroup(_ context.Context, groupID string, gps ...things.GroupRoles) error {
func (mrm *rolesRepositoryMock) UpdateRolesByGroup(_ context.Context, gms ...things.GroupMember) error {
panic("not implemented")
}

Expand Down
28 changes: 8 additions & 20 deletions things/postgres/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@ func NewRolesRepository(db Database) things.RolesRepository {
}
}

func (pr rolesRepository) SaveRolesByGroup(ctx context.Context, groupID string, gps ...things.GroupRoles) error {
func (pr rolesRepository) SaveRolesByGroup(ctx context.Context, gms ...things.GroupMember) error {
tx, err := pr.db.BeginTxx(ctx, nil)
if err != nil {
return err
}

q := `INSERT INTO group_roles (member_id, group_id, role) VALUES (:member_id, :group_id, :role);`

for _, g := range gps {
gp := things.GroupMember{
MemberID: g.MemberID,
GroupID: groupID,
Role: g.Role,
}
dbgp := toDBGroupMembers(gp)

if _, err := pr.db.NamedExecContext(ctx, q, dbgp); err != nil {
for _, g := range gms {
dbgm := toDBGroupMembers(g)
if _, err := pr.db.NamedExecContext(ctx, q, dbgm); err != nil {
tx.Rollback()
pgErr, ok := err.(*pgconn.PgError)
if ok {
Expand Down Expand Up @@ -182,18 +176,12 @@ func (pr rolesRepository) RemoveRolesByGroup(ctx context.Context, groupID string
return nil
}

func (pr rolesRepository) UpdateRolesByGroup(ctx context.Context, groupID string, gps ...things.GroupRoles) error {
func (pr rolesRepository) UpdateRolesByGroup(ctx context.Context, gms ...things.GroupMember) error {
q := `UPDATE group_roles SET role = :role WHERE member_id = :member_id AND group_id = :group_id;`

for _, g := range gps {
gp := things.GroupMember{
MemberID: g.MemberID,
GroupID: groupID,
Role: g.Role,
}
dbgp := toDBGroupMembers(gp)

row, err := pr.db.NamedExecContext(ctx, q, dbgp)
for _, g := range gms {
dbgm := toDBGroupMembers(g)
row, err := pr.db.NamedExecContext(ctx, q, dbgm)
if err != nil {
pgErr, ok := err.(*pgconn.PgError)
if ok {
Expand Down
Loading

0 comments on commit 0337309

Please sign in to comment.