ship: v0.1 Foundation milestone complete (#1)
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/model"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("not found")
|
||||
|
||||
type NodeRepo struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewNodeRepo(db *sql.DB) *NodeRepo {
|
||||
return &NodeRepo{db: db}
|
||||
}
|
||||
|
||||
func (r *NodeRepo) Insert(ctx context.Context, n *model.Node) error {
|
||||
if n.JoinedAt.IsZero() {
|
||||
n.JoinedAt = time.Now().UTC()
|
||||
}
|
||||
if n.LastSeen.IsZero() {
|
||||
n.LastSeen = n.JoinedAt
|
||||
}
|
||||
if n.State == "" {
|
||||
n.State = model.NodeStateReady
|
||||
}
|
||||
metaJSON, err := json.Marshal(n.Metadata)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal metadata: %w", err)
|
||||
}
|
||||
_, err = r.db.ExecContext(ctx,
|
||||
`INSERT INTO nodes (id, name, address, state, joined_at, last_seen, metadata) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
n.ID, n.Name, n.Address, string(n.State), n.JoinedAt, n.LastSeen, string(metaJSON))
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert node: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *NodeRepo) Get(ctx context.Context, id string) (*model.Node, error) {
|
||||
row := r.db.QueryRowContext(ctx,
|
||||
`SELECT id, name, address, state, joined_at, last_seen, metadata FROM nodes WHERE id = ?`, id)
|
||||
return scanNode(row)
|
||||
}
|
||||
|
||||
func (r *NodeRepo) List(ctx context.Context) ([]*model.Node, error) {
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
`SELECT id, name, address, state, joined_at, last_seen, metadata FROM nodes ORDER BY joined_at ASC`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list nodes: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var nodes []*model.Node
|
||||
for rows.Next() {
|
||||
n, err := scanNode(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes = append(nodes, n)
|
||||
}
|
||||
return nodes, rows.Err()
|
||||
}
|
||||
|
||||
func (r *NodeRepo) UpdateState(ctx context.Context, id string, state model.NodeState) error {
|
||||
res, err := r.db.ExecContext(ctx,
|
||||
`UPDATE nodes SET state = ?, last_seen = ? WHERE id = ?`,
|
||||
string(state), time.Now().UTC(), id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update node state: %w", err)
|
||||
}
|
||||
rows, _ := res.RowsAffected()
|
||||
if rows == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *NodeRepo) Delete(ctx context.Context, id string) error {
|
||||
res, err := r.db.ExecContext(ctx, `DELETE FROM nodes WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete node: %w", err)
|
||||
}
|
||||
rows, _ := res.RowsAffected()
|
||||
if rows == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type scanner interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
|
||||
func scanNode(s scanner) (*model.Node, error) {
|
||||
var (
|
||||
n model.Node
|
||||
state string
|
||||
metaJSON sql.NullString
|
||||
)
|
||||
err := s.Scan(&n.ID, &n.Name, &n.Address, &state, &n.JoinedAt, &n.LastSeen, &metaJSON)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan node: %w", err)
|
||||
}
|
||||
n.State = model.NodeState(state)
|
||||
if metaJSON.Valid && metaJSON.String != "" {
|
||||
if err := json.Unmarshal([]byte(metaJSON.String), &n.Metadata); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal metadata: %w", err)
|
||||
}
|
||||
}
|
||||
return &n, nil
|
||||
}
|
||||
Reference in New Issue
Block a user