Files
terminalShop/pkg/database/seed.go
marcusDenslow 93b257a612 fix: store coffee prices as int cents to prevent rounding errors
prices stored as float64 could lead to rounding errors when converting to cents for stripe (19.99 * 100 = 1998.99... truncating to 1998). canged Coffee.Price to int, storing cents directly. Updated the seed data, fallback products, API handlers, and all TUI display code to be in sync with this change.
2026-03-26 09:09:20 +01:00

89 lines
2.2 KiB
Go

package database
import (
"fmt"
"log"
"gorm.io/gorm"
"terminalShop/pkg/models"
)
// Seed populates the database with initial data
func Seed(db *gorm.DB) error {
log.Println("Seeding database...")
// Check if products already exist
var count int64
db.Model(&models.Coffee{}).Count(&count)
if count > 0 {
log.Printf("Database already has %d products, skipping seed", count)
return nil
}
// Define seed products (from handlers/products.go)
products := []models.Coffee{
{
Name: "Espresso",
RoastType: "Dark Roast",
Ounces: 2,
BeanType: "Arabica",
Price: 350, // $3.50
Color: "#8B4513",
Description: "Bold and intense coffee with a rich crema",
},
{
Name: "Americano",
RoastType: "Medium Roast",
Ounces: 8,
BeanType: "Arabica",
Price: 400, // $4.00
Color: "#A0522D",
Description: "Smooth and balanced espresso diluted with hot water",
},
{
Name: "Cappuccino",
RoastType: "Medium Roast",
Ounces: 6,
BeanType: "Arabica",
Price: 450, // $4.50
Color: "#C4A574",
Description: "Espresso with steamed milk and a thick layer of foam",
},
{
Name: "Latte",
RoastType: "Medium Roast",
Ounces: 12,
BeanType: "Arabica",
Price: 500, // $5.00
Color: "#D2B48C",
Description: "Creamy espresso with plenty of steamed milk and a touch of foam",
},
{
Name: "Mocha",
RoastType: "Dark Roast",
Ounces: 12,
BeanType: "Arabica",
Price: 550, // $5.50
Color: "#8B4513",
Description: "Chocolate and espresso combined with steamed milk. Sweet and indulgent.",
},
{
Name: "Macchiato",
RoastType: "Dark Roast",
Ounces: 3,
BeanType: "Robusta Blend",
Price: 425, // $4.25
Color: "#DAA520",
Description: "Espresso 'marked' with a dollop of foamed milk. Small but mighty, like a tiny caffeinated warrior.",
},
}
// Insert all products in a transaction
if err := db.Create(&products).Error; err != nil {
return fmt.Errorf("failed to seed products: %w", err)
}
log.Printf("Successfully seeded %d products", len(products))
return nil
}