- store/sqlite: 75.4% (repos + db coverage) - host/sub2api: 80.8% (httptest mock server, pure function tests) - app: 74.2% (handler error paths, NewActionSet closures) - pack: 72.4% - provision: 75.2% - access: 77.3% - config: 94.7% (lookup mock tests) All tests pass: build, vet, race, coverage gates.
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Host struct {
|
|
ID int64
|
|
HostID string
|
|
BaseURL string
|
|
HostVersion string
|
|
CapabilityProbeJSON string
|
|
}
|
|
|
|
type HostsRepo struct {
|
|
db execQuerier
|
|
}
|
|
|
|
func newHostsRepo(db execQuerier) *HostsRepo {
|
|
return &HostsRepo{db: db}
|
|
}
|
|
|
|
func (r *HostsRepo) GetByID(ctx context.Context, id int64) (Host, error) {
|
|
if id <= 0 {
|
|
return Host{}, fmt.Errorf("id is required")
|
|
}
|
|
|
|
var host Host
|
|
if err := r.db.QueryRowContext(ctx, `SELECT id, host_id, base_url, host_version, capability_probe_json FROM hosts WHERE id = ?`, id).Scan(&host.ID, &host.HostID, &host.BaseURL, &host.HostVersion, &host.CapabilityProbeJSON); err != nil {
|
|
return Host{}, err
|
|
}
|
|
return host, nil
|
|
}
|
|
|
|
func (r *HostsRepo) GetByHostID(ctx context.Context, hostID string) (Host, error) {
|
|
hostID = strings.TrimSpace(hostID)
|
|
if hostID == "" {
|
|
return Host{}, fmt.Errorf("host_id is required")
|
|
}
|
|
|
|
var host Host
|
|
if err := r.db.QueryRowContext(ctx, `SELECT id, host_id, base_url, host_version, capability_probe_json FROM hosts WHERE host_id = ?`, hostID).Scan(&host.ID, &host.HostID, &host.BaseURL, &host.HostVersion, &host.CapabilityProbeJSON); err != nil {
|
|
return Host{}, err
|
|
}
|
|
return host, nil
|
|
}
|
|
|
|
func (r *HostsRepo) Create(ctx context.Context, host Host) (int64, error) {
|
|
hostID := strings.TrimSpace(host.HostID)
|
|
baseURL := strings.TrimSpace(host.BaseURL)
|
|
hostVersion := strings.TrimSpace(host.HostVersion)
|
|
capabilityProbeJSON := strings.TrimSpace(host.CapabilityProbeJSON)
|
|
|
|
switch {
|
|
case hostID == "":
|
|
return 0, fmt.Errorf("host_id is required")
|
|
case baseURL == "":
|
|
return 0, fmt.Errorf("base_url is required")
|
|
case hostVersion == "":
|
|
return 0, fmt.Errorf("host_version is required")
|
|
case capabilityProbeJSON == "":
|
|
capabilityProbeJSON = "{}"
|
|
}
|
|
|
|
result, err := r.db.ExecContext(
|
|
ctx,
|
|
`INSERT INTO hosts (host_id, base_url, host_version, capability_probe_json)
|
|
VALUES (?, ?, ?, ?)`,
|
|
hostID,
|
|
baseURL,
|
|
hostVersion,
|
|
capabilityProbeJSON,
|
|
)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("insert host %q: %w", hostID, err)
|
|
}
|
|
|
|
id, err := result.LastInsertId()
|
|
if err != nil {
|
|
return 0, fmt.Errorf("read inserted host id for %q: %w", hostID, err)
|
|
}
|
|
|
|
return id, nil
|
|
}
|