feat: add register agent in db

This commit is contained in:
lorsan
2026-04-16 13:38:35 +03:00
parent 65d8f883dc
commit 52766cf7e8
16 changed files with 271 additions and 29 deletions
+38
View File
@@ -0,0 +1,38 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: agent.sql
package gen
import (
"context"
)
const createAgent = `-- name: CreateAgent :exec
INSERT INTO agents (agent_id, agent_name, architecture, system, hostname, version, capabilities)
VALUES ($1, $2, $3, $4, $5, $6, $7)
`
type CreateAgentParams struct {
AgentID string
AgentName *string
Architecture string
System string
Hostname string
Version string
Capabilities []byte
}
func (q *Queries) CreateAgent(ctx context.Context, arg CreateAgentParams) error {
_, err := q.db.Exec(ctx, createAgent,
arg.AgentID,
arg.AgentName,
arg.Architecture,
arg.System,
arg.Hostname,
arg.Version,
arg.Capabilities,
)
return err
}
+32
View File
@@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package gen
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}
+21
View File
@@ -0,0 +1,21 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package gen
import (
"github.com/jackc/pgx/v5/pgtype"
)
type Agent struct {
ID int64
AgentID string
AgentName *string
Architecture string
System string
Hostname string
Version string
Capabilities []byte
RegisteredAt pgtype.Timestamp
}
@@ -0,0 +1,3 @@
-- name: CreateAgent :exec
INSERT INTO agents (agent_id, agent_name, architecture, system, hostname, version, capabilities)
VALUES ($1, $2, $3, $4, $5, $6, $7);
+21
View File
@@ -0,0 +1,21 @@
package store
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store/sqlc/gen"
)
type HubStore struct {
queries *gen.Queries
}
func NewHubStore(db *pgxpool.Pool) *HubStore {
queries := gen.New(db)
return &HubStore{queries}
}
func (h *HubStore) NewAgent(ctx context.Context, agent Agent) error {
return h.queries.CreateAgent(ctx, toDBAgent(agent))
}
+37
View File
@@ -0,0 +1,37 @@
package store
import (
"encoding/json"
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store/sqlc/gen"
)
type Agent struct {
AgentID string
AgentName string
Architecture string
System string
Hostname string
Version string
Capabilities []domain.Capability
}
func toDBAgent(agent Agent) gen.CreateAgentParams {
return gen.CreateAgentParams{
AgentID: agent.AgentID,
AgentName: &agent.AgentName,
Architecture: agent.Architecture,
System: agent.System,
Version: agent.Version,
Capabilities: toJsonCapabilities(agent.Capabilities),
}
}
func toJsonCapabilities(caps []domain.Capability) []byte {
data, err := json.Marshal(caps)
if err != nil {
return []byte{}
}
return data
}