add roles routes

This commit is contained in:
2026-07-06 23:32:38 +07:00
parent 728fcc6fda
commit 82ffda5c0a
9 changed files with 1029 additions and 39 deletions
+33
View File
@@ -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
}