generated from VLADIMIR/template
add roles routes
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"evening_detective_server/internal/repos"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
@@ -175,3 +176,35 @@ func (s *UsersRepo) GetUsers(
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (s *UsersRepo) AddUserRole(
|
||||
ctx context.Context,
|
||||
userId int,
|
||||
role string,
|
||||
) error {
|
||||
_, err := s.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE users
|
||||
SET roles = roles || $1::jsonb
|
||||
WHERE id = $2 AND NOT roles @> $1::jsonb`,
|
||||
fmt.Sprintf(`["%s"]`, role),
|
||||
userId,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *UsersRepo) DeleteUserRole(
|
||||
ctx context.Context,
|
||||
userId int,
|
||||
role string,
|
||||
) error {
|
||||
_, err := s.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE users
|
||||
SET roles = roles - $1::text
|
||||
WHERE id = $2`,
|
||||
role,
|
||||
userId,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user