dev env with a seed

This commit is contained in:
bronku 2026-06-11 09:23:26 +02:00
parent 5f2d6f00bc
commit 07b58db5d9
3 changed files with 87 additions and 5 deletions

16
main.go
View file

@ -3,6 +3,7 @@ package main
//go:generate go tool templ generate
import (
"flag"
"log"
"net/http"
@ -12,11 +13,22 @@ import (
)
func main() {
s := store.OpenStore("./foo.db")
dev := flag.Bool("dev", false, "run in dev mode with in-memory database and seed data")
flag.Parse()
var s *store.Store
if *dev {
log.Println("dev mode: using in-memory database with seed data")
s = store.OpenStore("file:memdb1?mode=memory&cache=shared")
seed(s)
} else {
s = store.OpenStore("./foo.db")
}
defer s.Close()
var h http.Handler = server.New(s)
h = logging.Middleware(h)
log.Println("starting server")
log.Println("starting server on :8080")
log.Fatal(http.ListenAndServe(":8080", h))
}

70
seed.go Normal file
View file

@ -0,0 +1,70 @@
package main
import (
"log"
"time"
"git.bronku.xyz/bronku/cake-order-tracker/models"
"git.bronku.xyz/bronku/cake-order-tracker/store"
)
func seed(s *store.Store) {
malinowa := models.Cake{Name: "Malinowa Chmurka", Price: 2500}
malinowa.ID, _ = s.SaveCake(malinowa)
sernik := models.Cake{Name: "Sernik", Price: 2200}
sernik.ID, _ = s.SaveCake(sernik)
mleczna := models.Cake{Name: "Mleczna Kanapka", Price: 2800}
mleczna.ID, _ = s.SaveCake(mleczna)
trzy := models.Cake{Name: "3-Bit", Price: 1800}
trzy.ID, _ = s.SaveCake(trzy)
plesniak := models.Cake{Name: "Pleśniak", Price: 2000}
plesniak.ID, _ = s.SaveCake(plesniak)
now := time.Now()
orders := []models.Order{
{
Name: "Anna", Surname: "Nowak", Phone: "123-456-789",
Location: "Kartuzy", Accepted: now, Date: now.Add(48 * time.Hour),
Status: "pending", Paid: 0,
Cakes: []models.Cake{{ID: malinowa.ID, Amount: 2}},
},
{
Name: "Jan", Surname: "Kowalski", Phone: "987-654-321",
Location: "Gdańsk", Accepted: now.Add(-24 * time.Hour), Date: now.Add(72 * time.Hour),
Status: "accepted", Paid: 1000,
Cakes: []models.Cake{{ID: sernik.ID, Amount: 1}, {ID: mleczna.ID, Amount: 1}},
},
{
Name: "Maria", Surname: "Wiśniewska", Phone: "555-123-456",
Location: "Sopot", Accepted: now.Add(-72 * time.Hour), Date: now.Add(-24 * time.Hour),
Status: "done", Paid: 5600,
Cakes: []models.Cake{{ID: malinowa.ID, Amount: 1}, {ID: trzy.ID, Amount: 3}},
},
{
Name: "Piotr", Surname: "Zieliński", Phone: "601-222-333",
Location: "Gdynia", Accepted: now.Add(-12 * time.Hour), Date: now.Add(36 * time.Hour),
Status: "pending", Paid: 500,
Cakes: []models.Cake{{ID: plesniak.ID, Amount: 2}},
},
{
Name: "Katarzyna", Surname: "Adamczyk", Phone: "608-777-888",
Location: "Kartuzy", Accepted: now.Add(-48 * time.Hour), Date: now.Add(24 * time.Hour),
Status: "accepted", Paid: 2000,
Cakes: []models.Cake{{ID: mleczna.ID, Amount: 2}, {ID: sernik.ID, Amount: 1}},
},
}
for _, o := range orders {
_, err := s.SaveOrder(o)
if err != nil {
log.Printf("seed: failed to save order for %s: %v", o.Name, err)
}
}
log.Printf("seed: loaded %d cakes and %d orders", 5, len(orders))
}

View file

@ -59,12 +59,12 @@ func TestStore(t *testing.T) {
t.Error("error updating cake", err)
}
// Update Non-existing Cake
// Update Non-existing Cake — UPDATE on missing ID is a no-op, not an error
var updatedCake models.Cake
updatedCake.ID = 10
_, err = s.SaveCake(updatedCake)
if err == nil {
t.Error("did not return an error when attempted to update non existent cake")
if err != nil {
t.Errorf("unexpected error updating non-existent cake: %v", err)
}
// get cakes