61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Host struct {
|
|
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) 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
|
|
}
|