33 lines
896 B
Go
33 lines
896 B
Go
package deviceauth
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/jackc/pgx/v5"
|
||
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
|
)
|
||
|
|
|
||
|
|
var ErrDeviceNotFound = errors.New("device identity was not found")
|
||
|
|
|
||
|
|
type Repository struct{ pool *pgxpool.Pool }
|
||
|
|
|
||
|
|
func NewRepository(pool *pgxpool.Pool) *Repository { return &Repository{pool: pool} }
|
||
|
|
|
||
|
|
func (r *Repository) FindByPublicID(ctx context.Context, publicID string) (Identity, error) {
|
||
|
|
var identity Identity
|
||
|
|
err := r.pool.QueryRow(ctx, `
|
||
|
|
SELECT id::text, public_id::text, tenant_id::text, status, credential_ref
|
||
|
|
FROM device
|
||
|
|
WHERE public_id = $1::uuid`, publicID,
|
||
|
|
).Scan(&identity.ID, &identity.PublicID, &identity.TenantID, &identity.Status, &identity.CredentialRef)
|
||
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
||
|
|
return Identity{}, ErrDeviceNotFound
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
return Identity{}, fmt.Errorf("find device identity: %w", err)
|
||
|
|
}
|
||
|
|
return identity, nil
|
||
|
|
}
|