2026-07-16 01:47:11 +00:00
|
|
|
package main
|
2026-07-22 07:23:40 +00:00
|
|
|
|
2026-07-16 01:47:11 +00:00
|
|
|
import (
|
2026-07-22 07:23:40 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-07-16 01:47:11 +00:00
|
|
|
"floorvisualizer/internal/model"
|
|
|
|
|
"floorvisualizer/internal/repository"
|
|
|
|
|
)
|
2026-07-22 07:23:40 +00:00
|
|
|
|
2026-07-16 01:47:11 +00:00
|
|
|
func main() {
|
2026-07-22 07:23:40 +00:00
|
|
|
dsn := repository.DefaultDSN
|
|
|
|
|
if v := os.Getenv("DATABASE_URL"); v != "" {
|
|
|
|
|
dsn = v
|
|
|
|
|
}
|
|
|
|
|
dataDir := "data/products"
|
|
|
|
|
if v := os.Getenv("PRODUCT_DATA_DIR"); v != "" {
|
|
|
|
|
dataDir = v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db, err := repository.Connect(dsn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-07-16 01:47:11 +00:00
|
|
|
defer db.Close()
|
|
|
|
|
db.Exec("DELETE FROM products")
|
2026-07-22 07:23:40 +00:00
|
|
|
files, err := os.ReadDir(dataDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-07-16 01:47:11 +00:00
|
|
|
total := 0
|
|
|
|
|
for _, f := range files {
|
2026-07-22 07:23:40 +00:00
|
|
|
if f.IsDir() || !strings.EqualFold(filepath.Ext(f.Name()), ".json") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
data, err := os.ReadFile(filepath.Join(dataDir, f.Name()))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-07-16 01:47:11 +00:00
|
|
|
var prods []model.Product
|
2026-07-22 07:23:40 +00:00
|
|
|
if err := json.Unmarshal(data, &prods); err != nil {
|
|
|
|
|
panic(fmt.Errorf("parse %s: %w", f.Name(), err))
|
|
|
|
|
}
|
|
|
|
|
if err := repository.InsertProducts(db, prods); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-07-16 01:47:11 +00:00
|
|
|
total += len(prods)
|
|
|
|
|
fmt.Printf(" %s: %d\n", f.Name(), len(prods))
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("\nDone! %d products\n", total)
|
|
|
|
|
}
|