ch role list
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
2026-09-30 13:42:14 +05:00
parent 19a1f70588
commit 5ab17b1539
2 changed files with 23 additions and 8 deletions

View File

@@ -45,20 +45,25 @@ func (r *repository) List(ctx context.Context, filter role.ListFilter) (role.Lis
}
args = append(args, filter.Limit, (filter.Page-1)*filter.Limit)
query := fmt.Sprintf(`SELECT %s FROM roles r%s ORDER BY r.id LIMIT $%d OFFSET $%d`,
roleColumns, where, len(args)-1, len(args))
query := fmt.Sprintf(`SELECT r.id, r.code, r.scope,
(SELECT COUNT(*) FROM users u WHERE u.role_id = r.id AND u.deleted_at IS NULL)
FROM roles r%s ORDER BY r.id LIMIT $%d OFFSET $%d`,
where, len(args)-1, len(args))
rows, err := r.client.Query(ctx, query, args...)
if err != nil {
return role.ListRoleDTO{}, err
}
defer rows.Close()
items := []role.RoleDTO{}
items := []role.RoleListItemDTO{}
for rows.Next() {
item, err := scanRole(rows)
if err != nil {
var item role.RoleListItemDTO
var scope string
if err := rows.Scan(&item.Id, &item.Code, &scope, &item.UsersCount); err != nil {
return role.ListRoleDTO{}, err
}
item.Scope = rbac.Scope(scope)
item.ScopeName = item.Scope.Name()
items = append(items, item)
}
if err := rows.Err(); err != nil {

View File

@@ -51,7 +51,17 @@ type ListFilter struct {
Limit int
}
type ListRoleDTO struct {
Count int64 `json:"count"`
Roles []RoleDTO `json:"roles"`
// RoleListItemDTO is the list row; name, permissions and the rest come from
// GET /roles/:id.
type RoleListItemDTO struct {
Id int64 `json:"id"`
Code string `json:"code"`
Scope rbac.Scope `json:"scope"`
ScopeName string `json:"scopeName"`
UsersCount int64 `json:"usersCount"`
}
type ListRoleDTO struct {
Count int64 `json:"count"`
Roles []RoleListItemDTO `json:"roles"`
}