mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-09-18 15:08:21 +03:00
refactor: change structure project
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
domainHub "github.com/lorsanstand/HomeOps-Hub/hub/internal/domain"
|
||||
gen2 "github.com/lorsanstand/HomeOps-Hub/hub/internal/store/sqlc/gen"
|
||||
"github.com/lorsanstand/HomeOps-Hub/shared/domain"
|
||||
)
|
||||
|
||||
func toDBAgent(agent domainHub.CreateAgentModel) gen2.CreateAgentParams {
|
||||
return gen2.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) gen2.UpdateAgentByIDParams {
|
||||
return gen2.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 gen2.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
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: cmd.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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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 int32
|
||||
AgentID string
|
||||
AgentName *string
|
||||
Architecture string
|
||||
System string
|
||||
Hostname string
|
||||
Version string
|
||||
Capabilities []byte
|
||||
RegisteredAt pgtype.Timestamp
|
||||
}
|
||||
@@ -0,0 +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);
|
||||
|
||||
-- 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;
|
||||
@@ -0,0 +1,36 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
domainHub "github.com/lorsanstand/HomeOps-Hub/hub/internal/domain"
|
||||
"github.com/lorsanstand/HomeOps-Hub/hub/internal/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 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)
|
||||
}
|
||||
Reference in New Issue
Block a user