Added Plates and Items Models and connected them with the Frontend
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ensureDir(dir string) error {
|
||||
return os.MkdirAll(dir, 0o755)
|
||||
}
|
||||
|
||||
func readJSON[T any](path string) (T, error) {
|
||||
var v T
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
if err := json.Unmarshal(data, &v); err != nil {
|
||||
return v, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func writeJSON(path string, v any) error {
|
||||
data, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0o644)
|
||||
}
|
||||
|
||||
func removeFile(path, notFoundMsg string) error {
|
||||
if err := os.Remove(path); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return fmt.Errorf("%s", notFoundMsg)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func listFromDir[T any](
|
||||
dir string,
|
||||
match func(name string) bool,
|
||||
readLabel string,
|
||||
createdAt func(T) time.Time,
|
||||
) ([]T, error) {
|
||||
if err := ensureDir(dir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %s: %w", readLabel, err)
|
||||
}
|
||||
|
||||
var out []T
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !match(e.Name()) {
|
||||
continue
|
||||
}
|
||||
v, err := readJSON[T](filepath.Join(dir, e.Name()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
return createdAt(out[i]).After(createdAt(out[j]))
|
||||
})
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func findByID[T any](list []T, id string, idOf func(T) string) (T, error) {
|
||||
for _, v := range list {
|
||||
if idOf(v) == id {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
var zero T
|
||||
return zero, fmt.Errorf("not found: %s", id)
|
||||
}
|
||||
|
||||
func stampNew(createdAt *time.Time) time.Time {
|
||||
if createdAt.IsZero() {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return *createdAt
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"printer.backend/internal/model"
|
||||
"printer.backend/internal/svgtemplate"
|
||||
)
|
||||
|
||||
// ItemStore manages items (SVG + metadata) in the template directory.
|
||||
type ItemStore struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
// NewItemStore creates a store that scans templateDir for *.meta.json files.
|
||||
func NewItemStore(templateDir string) *ItemStore {
|
||||
if templateDir == "" {
|
||||
templateDir = svgtemplate.OutputDir
|
||||
}
|
||||
return &ItemStore{dir: templateDir}
|
||||
}
|
||||
|
||||
// List returns all items sorted by creation time (newest first).
|
||||
func (s *ItemStore) List() ([]model.Item, error) {
|
||||
return listFromDir(s.dir,
|
||||
func(name string) bool { return strings.HasSuffix(name, ".meta.json") },
|
||||
"template dir",
|
||||
func(it model.Item) time.Time { return it.CreatedAt },
|
||||
)
|
||||
}
|
||||
|
||||
// Get returns an item by ID.
|
||||
func (s *ItemStore) Get(id string) (model.Item, error) {
|
||||
list, err := s.List()
|
||||
if err != nil {
|
||||
return model.Item{}, err
|
||||
}
|
||||
item, err := findByID(list, id, func(it model.Item) string { return it.ID })
|
||||
if err != nil {
|
||||
return model.Item{}, fmt.Errorf("item %w", err)
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
// Create generates SVG + metadata for a new item.
|
||||
func (s *ItemStore) Create(name string, spec model.ItemSpec) (model.Item, error) {
|
||||
if spec.SizeMM <= 0 {
|
||||
return model.Item{}, fmt.Errorf("size_mm must be positive")
|
||||
}
|
||||
|
||||
basename := svgBasename(name)
|
||||
data := svgtemplate.Build(spec.SizeMM, spec.BleedMM, spec.MarginMM, spec.PaddingMM)
|
||||
if err := svgtemplate.WriteFile(basename, data); err != nil {
|
||||
return model.Item{}, fmt.Errorf("write svg: %w", err)
|
||||
}
|
||||
displayName := name
|
||||
if displayName == "" {
|
||||
displayName = strings.TrimSuffix(basename, filepath.Ext(basename))
|
||||
}
|
||||
return svgtemplate.WriteMeta(basename, spec, displayName)
|
||||
}
|
||||
|
||||
// Delete removes an item's SVG and metadata by ID.
|
||||
func (s *ItemStore) Delete(id string) error {
|
||||
item, err := s.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, path := range []string{
|
||||
filepath.Join(s.dir, item.SVGTemplate),
|
||||
filepath.Join(s.dir, model.MetaFilename(item.SVGTemplate)),
|
||||
} {
|
||||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SVGPath returns the path to an item's SVG file.
|
||||
func (s *ItemStore) SVGPath(id string) (string, error) {
|
||||
item, err := s.Get(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
path := filepath.Join(s.dir, item.SVGTemplate)
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return "", fmt.Errorf("svg file missing: %w", err)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func svgBasename(name string) string {
|
||||
base := strings.TrimSpace(name)
|
||||
if base == "" {
|
||||
return uuid.NewString() + ".svg"
|
||||
}
|
||||
base = strings.ReplaceAll(base, " ", "_")
|
||||
if !strings.HasSuffix(strings.ToLower(base), ".svg") {
|
||||
base += ".svg"
|
||||
}
|
||||
return filepath.Base(base)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"printer.backend/internal/model"
|
||||
"printer.backend/internal/paths"
|
||||
)
|
||||
|
||||
// PlateStore persists plates as JSON files.
|
||||
type PlateStore struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
// NewPlateStore creates a store under dir (default data/plates).
|
||||
func NewPlateStore(dir string) *PlateStore {
|
||||
if dir == "" {
|
||||
dir = paths.PlatesDir
|
||||
}
|
||||
return &PlateStore{dir: dir}
|
||||
}
|
||||
|
||||
// List returns all plates sorted by creation time (newest first).
|
||||
func (s *PlateStore) List() ([]model.Plate, error) {
|
||||
return listFromDir(s.dir,
|
||||
func(name string) bool { return filepath.Ext(name) == ".json" },
|
||||
"plates dir",
|
||||
func(p model.Plate) time.Time { return p.CreatedAt },
|
||||
)
|
||||
}
|
||||
|
||||
// Save writes a new plate and assigns an ID when empty.
|
||||
func (s *PlateStore) Save(p model.Plate) (model.Plate, error) {
|
||||
if err := ensureDir(s.dir); err != nil {
|
||||
return model.Plate{}, err
|
||||
}
|
||||
if p.ID == "" {
|
||||
p.ID = uuid.NewString()
|
||||
}
|
||||
p.CreatedAt = stampNew(&p.CreatedAt)
|
||||
|
||||
path := filepath.Join(s.dir, p.ID+".json")
|
||||
if err := writeJSON(path, p); err != nil {
|
||||
return model.Plate{}, fmt.Errorf("write plate: %w", err)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Delete removes a plate by ID.
|
||||
func (s *PlateStore) Delete(id string) error {
|
||||
path := filepath.Join(s.dir, id+".json")
|
||||
return removeFile(path, fmt.Sprintf("plate not found: %s", id))
|
||||
}
|
||||
Reference in New Issue
Block a user