feat: create save new agent in db

This commit is contained in:
2026-04-19 16:16:03 +03:00
parent 52766cf7e8
commit e289365ce8
14 changed files with 314 additions and 72 deletions
+69
View File
@@ -0,0 +1,69 @@
package store
import (
"encoding/json"
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
domainHub "github.com/lorsanstand/HomeOps-Hub/internal/hub/domain"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store/sqlc/gen"
)
func toDBAgent(agent domainHub.CreateAgentModel) gen.CreateAgentParams {
return gen.CreateAgentParams{
AgentID: agent.AgentID,
AgentName: &agent.AgentName,
Architecture: agent.Architecture,
System: agent.System,
Hostname: agent.Hostname,
Version: agent.Version,
Capabilities: toJsonCapabilities(agent.Capabilities),
}
}
func toUpdateDBAgent(agent domainHub.CreateAgentModel) gen.UpdateAgentByIDParams {
return gen.UpdateAgentByIDParams{
AgentID: agent.AgentID,
AgentName: &agent.AgentName,
Architecture: agent.Architecture,
System: agent.System,
Hostname: agent.Hostname,
Version: agent.Version,
Capabilities: toJsonCapabilities(agent.Capabilities),
}
}
func toJsonCapabilities(caps []domain.Capability) []byte {
data, err := json.Marshal(caps)
if err != nil {
// Note: Error is silently handled - consider logging in production
return []byte{}
}
return data
}
func toAgentModel(dbAgent gen.Agent) domainHub.AgentModel {
var dbAgentName string
if dbAgent.AgentName != nil {
dbAgentName = *dbAgent.AgentName
}
return domainHub.AgentModel{
ID: int(dbAgent.ID),
AgentID: dbAgent.AgentID,
AgentName: dbAgentName,
Architecture: dbAgent.Architecture,
System: dbAgent.System,
Hostname: dbAgent.Hostname,
Capabilities: toDomainCapabilities(dbAgent.Capabilities),
}
}
func toDomainCapabilities(caps []byte) []domain.Capability {
var capabilities []domain.Capability
err := json.Unmarshal(caps, &capabilities)
if err != nil {
// Note: Error is silently handled - consider logging in production
return []domain.Capability{}
}
return capabilities
}
+75
View File
@@ -36,3 +36,78 @@ func (q *Queries) CreateAgent(ctx context.Context, arg CreateAgentParams) error
)
return err
}
const getAgentByAgentID = `-- name: GetAgentByAgentID :one
SELECT id, agent_id, agent_name, architecture, system, hostname, version, capabilities, registered_at from agents
WHERE agent_id=$1
`
func (q *Queries) GetAgentByAgentID(ctx context.Context, agentID string) (Agent, error) {
row := q.db.QueryRow(ctx, getAgentByAgentID, agentID)
var i Agent
err := row.Scan(
&i.ID,
&i.AgentID,
&i.AgentName,
&i.Architecture,
&i.System,
&i.Hostname,
&i.Version,
&i.Capabilities,
&i.RegisteredAt,
)
return i, err
}
const getAgentByID = `-- name: GetAgentByID :one
SELECT id, agent_id, agent_name, architecture, system, hostname, version, capabilities, registered_at from agents
WHERE id=$1
`
func (q *Queries) GetAgentByID(ctx context.Context, id int32) (Agent, error) {
row := q.db.QueryRow(ctx, getAgentByID, id)
var i Agent
err := row.Scan(
&i.ID,
&i.AgentID,
&i.AgentName,
&i.Architecture,
&i.System,
&i.Hostname,
&i.Version,
&i.Capabilities,
&i.RegisteredAt,
)
return i, err
}
const updateAgentByID = `-- name: UpdateAgentByID :exec
UPDATE agents
SET agent_id=$1, agent_name=$2, architecture=$3, system=$4, hostname=$5, version=$6, capabilities=$7
WHERE id=$8
`
type UpdateAgentByIDParams struct {
AgentID string
AgentName *string
Architecture string
System string
Hostname string
Version string
Capabilities []byte
ID int32
}
func (q *Queries) UpdateAgentByID(ctx context.Context, arg UpdateAgentByIDParams) error {
_, err := q.db.Exec(ctx, updateAgentByID,
arg.AgentID,
arg.AgentName,
arg.Architecture,
arg.System,
arg.Hostname,
arg.Version,
arg.Capabilities,
arg.ID,
)
return err
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
)
type Agent struct {
ID int64
ID int32
AgentID string
AgentName *string
Architecture string
+14 -1
View File
@@ -1,3 +1,16 @@
-- name: CreateAgent :exec
INSERT INTO agents (agent_id, agent_name, architecture, system, hostname, version, capabilities)
VALUES ($1, $2, $3, $4, $5, $6, $7);
VALUES ($1, $2, $3, $4, $5, $6, $7);
-- name: GetAgentByID :one
SELECT * from agents
WHERE id=$1;
-- name: GetAgentByAgentID :one
SELECT * from agents
WHERE agent_id=$1;
-- name: UpdateAgentByID :exec
UPDATE agents
SET agent_id=$1, agent_name=$2, architecture=$3, system=$4, hostname=$5, version=$6, capabilities=$7
WHERE id=$8;
+16 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
domainHub "github.com/lorsanstand/HomeOps-Hub/internal/hub/domain"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store/sqlc/gen"
)
@@ -16,6 +17,20 @@ func NewHubStore(db *pgxpool.Pool) *HubStore {
return &HubStore{queries}
}
func (h *HubStore) NewAgent(ctx context.Context, agent Agent) error {
func (h *HubStore) NewAgent(ctx context.Context, agent domainHub.CreateAgentModel) error {
return h.queries.CreateAgent(ctx, toDBAgent(agent))
}
func (h *HubStore) GetAgentByAgentID(ctx context.Context, AgentID string) (domainHub.AgentModel, error) {
data, err := h.queries.GetAgentByAgentID(ctx, AgentID)
if err != nil {
return domainHub.AgentModel{}, err
}
return toAgentModel(data), nil
}
func (h *HubStore) UpdateAgentByID(ctx context.Context, ID int, updateAgent domainHub.CreateAgentModel) error {
data := toUpdateDBAgent(updateAgent)
data.ID = int32(ID)
return h.queries.UpdateAgentByID(ctx, data)
}
-37
View File
@@ -1,37 +0,0 @@
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
}