fix: auto-generate ID+timestamps in storage layer on save

This commit is contained in:
swanadiva
2026-06-23 18:18:10 +07:00
parent 512cf5ce1f
commit 07899c154b
+27
View File
@@ -7,6 +7,9 @@ import (
"os"
"path/filepath"
"sync"
"time"
"github.com/google/uuid"
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
)
@@ -100,6 +103,14 @@ func (s *JSONStorage) SaveHost(ctx context.Context, host *models.Host) error {
s.mu.Lock()
defer s.mu.Unlock()
if host.ID == "" {
host.ID = uuid.New().String()
}
if host.CreatedAt.IsZero() {
host.CreatedAt = time.Now()
}
host.UpdatedAt = time.Now()
hosts, err := s.listHostsInternal()
if err != nil {
return err
@@ -202,6 +213,14 @@ func (s *JSONStorage) SaveKeyPair(ctx context.Context, keyPair *models.KeyPair)
s.mu.Lock()
defer s.mu.Unlock()
if keyPair.ID == "" {
keyPair.ID = uuid.New().String()
}
if keyPair.CreatedAt.IsZero() {
keyPair.CreatedAt = time.Now()
}
keyPair.UpdatedAt = time.Now()
keys, err := s.listKeyPairsInternal()
if err != nil {
return err
@@ -304,6 +323,14 @@ func (s *JSONStorage) SaveSnippet(ctx context.Context, snippet *models.Snippet)
s.mu.Lock()
defer s.mu.Unlock()
if snippet.ID == "" {
snippet.ID = uuid.New().String()
}
if snippet.CreatedAt.IsZero() {
snippet.CreatedAt = time.Now()
}
snippet.UpdatedAt = time.Now()
snippets, err := s.listSnippetsInternal()
if err != nil {
return err