2026-08-26 06:41:07 +08:00
|
|
|
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, `
|
2026-09-06 17:04:11 +08:00
|
|
|
SELECT d.id::text,d.public_id::text,d.tenant_id::text,d.status,d.activation_state,d.credential_ref
|
|
|
|
|
FROM device d
|
|
|
|
|
WHERE d.public_id = $1::uuid`, publicID,
|
|
|
|
|
).Scan(&identity.ID, &identity.PublicID, &identity.TenantID, &identity.Status, &identity.ActivationState, &identity.CredentialRef)
|
2026-08-26 06:41:07 +08:00
|
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
|
|
|
return Identity{}, ErrDeviceNotFound
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Identity{}, fmt.Errorf("find device identity: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return identity, nil
|
|
|
|
|
}
|