Skip to content

Commit

Permalink
Standardising the editing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
COMTOP1 committed Jun 24, 2024
1 parent 298efc7 commit ec611bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
21 changes: 14 additions & 7 deletions permission/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,26 @@ func (s *Store) addPermission(ctx context.Context, p Permission) (Permission, er

// editPermission edits an existing permission
func (s *Store) editPermission(ctx context.Context, p Permission) (Permission, error) {
res, err := s.db.NamedExecContext(ctx, `UPDATE people.permissions
SET name = :name,
description = :description
WHERE permission_id = :permission_id`, p)
builder := utils.PSQL().Update("people.permissions").
SetMap(map[string]interface{}{
"name": p.Name,
"description": p.Description,
}).
Where(sq.Eq{"permission_id": p.PermissionID})
sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for editPermission: %w", err))
}
res, err := s.db.ExecContext(ctx, sql, args...)
if err != nil {
return Permission{}, fmt.Errorf("failed to update permission: %w", err)
return Permission{}, fmt.Errorf("failed to edit permission: %w", err)
}
rows, err := res.RowsAffected()
if err != nil {
return Permission{}, fmt.Errorf("failed to update permission: %w", err)
return Permission{}, fmt.Errorf("failed to edit permission: %w", err)
}
if rows < 1 {
return Permission{}, fmt.Errorf("failed to update permissions: invalid rows affected: %d", rows)
return Permission{}, fmt.Errorf("failed to edit permissions: invalid rows affected: %d", rows)
}
return p, nil
}
Expand Down
21 changes: 14 additions & 7 deletions role/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,26 @@ func (s *Store) addRole(ctx context.Context, r Role) (Role, error) {

// editRole edits an existing Role
func (s *Store) editRole(ctx context.Context, r Role) (Role, error) {
res, err := s.db.NamedExecContext(ctx, `UPDATE people.roles
SET name = :name,
description = :description
WHERE role_id = :role_id`, r)
builder := utils.PSQL().Update("people.roles").
SetMap(map[string]interface{}{
"name": r.Name,
"description": r.Description,
}).
Where(sq.Eq{"role_id": r.RoleID})
sql, args, err := builder.ToSql()
if err != nil {
panic(fmt.Errorf("failed to build sql for editRole: %w", err))
}
res, err := s.db.ExecContext(ctx, sql, args...)
if err != nil {
return Role{}, fmt.Errorf("failed to update role: %w", err)
return Role{}, fmt.Errorf("failed to edit role: %w", err)
}
rows, err := res.RowsAffected()
if err != nil {
return Role{}, fmt.Errorf("failed to update role: %w", err)
return Role{}, fmt.Errorf("failed to edit role: %w", err)
}
if rows < 1 {
return Role{}, fmt.Errorf("failed to update role: invalid rows affected: %d", rows)
return Role{}, fmt.Errorf("failed to edit role: invalid rows affected: %d", rows)
}
return r, nil
}
Expand Down

0 comments on commit ec611bc

Please sign in to comment.