and another one

This commit is contained in:
2026-02-10 18:07:44 +11:00
parent 8d5cdd1fc8
commit dd382faa08
10 changed files with 52 additions and 60 deletions

View File

@@ -2,7 +2,6 @@ package db
import (
"context"
"database/sql"
"git.haelnorr.com/h/oslstats/internal/permissions"
"github.com/pkg/errors"
@@ -34,7 +33,7 @@ func GetPermissionByName(ctx context.Context, tx bun.Tx, name permissions.Permis
if name == "" {
return nil, errors.New("name cannot be empty")
}
return GetByField[Permission](tx, "name", name).GetFirst(ctx)
return GetByField[Permission](tx, "name", name).Get(ctx)
}
// GetPermissionByID queries the database for a permission matching the given ID
@@ -43,7 +42,7 @@ func GetPermissionByID(ctx context.Context, tx bun.Tx, id int) (*Permission, err
if id <= 0 {
return nil, errors.New("id must be positive")
}
return GetByID[Permission](tx, id).GetFirst(ctx)
return GetByID[Permission](tx, id).Get(ctx)
}
// GetPermissionsByResource queries for all permissions for a given resource
@@ -51,21 +50,13 @@ func GetPermissionsByResource(ctx context.Context, tx bun.Tx, resource string) (
if resource == "" {
return nil, errors.New("resource cannot be empty")
}
perms, err := GetByField[[]*Permission](tx, "resource", resource).GetAll(ctx)
return *perms, err
return GetList[Permission](tx).
Where("resource = ?", resource).GetAll(ctx)
}
// ListAllPermissions returns all permissions
func ListAllPermissions(ctx context.Context, tx bun.Tx) ([]*Permission, error) {
var perms []*Permission
err := tx.NewSelect().
Model(&perms).
Order("resource ASC", "action ASC").
Scan(ctx)
if err != nil && errors.Is(err, sql.ErrNoRows) {
return nil, errors.Wrap(err, "tx.NewSelect")
}
return perms, nil
return GetList[Permission](tx).GetAll(ctx)
}
// CreatePermission creates a new permission