This commit is contained in:
matst80
2025-10-13 15:39:41 +02:00
parent 6094da99f3
commit 9fc3871e84
26 changed files with 927 additions and 848 deletions

View File

@@ -1,11 +1,5 @@
package main
import (
"fmt"
messages "git.tornberg.me/go-cart-actor/pkg/messages"
)
// mutation_set_cart_items.go
//
// Registers the SetCartRequest mutation. This mutation replaces the entire list
@@ -25,33 +19,28 @@ import (
// - Deliveries might reference item IDs that are now invalid—original logic
// also left deliveries untouched. If that becomes an issue, add a cleanup
// pass to remove deliveries whose item IDs no longer exist.
func init() {
RegisterMutation[messages.SetCartRequest](
"SetCartRequest",
func(g *CartGrain, m *messages.SetCartRequest) error {
if m == nil {
return fmt.Errorf("SetCartRequest: nil payload")
}
// Clear current items (keep deliveries)
g.mu.Lock()
g.Items = make([]*CartItem, 0, len(m.Items))
g.mu.Unlock()
// func HandleSetCartRequest(g *CartGrain, m *messages.SetCartRequest) error {
// if m == nil {
// return fmt.Errorf("SetCartRequest: nil payload")
// }
for _, it := range m.Items {
if it == nil {
continue
}
if it.Sku == "" || it.Quantity < 1 {
return fmt.Errorf("SetCartRequest: invalid item (sku='%s' qty=%d)", it.Sku, it.Quantity)
}
_, err := g.AddItem(it.Sku, int(it.Quantity), it.Country, it.StoreId)
if err != nil {
return fmt.Errorf("SetCartRequest: add sku '%s' failed: %w", it.Sku, err)
}
}
return nil
},
WithTotals(),
)
}
// // Clear current items (keep deliveries)
// g.mu.Lock()
// g.Items = make([]*CartItem, 0, len(m.Items))
// g.mu.Unlock()
// for _, it := range m.Items {
// if it == nil {
// continue
// }
// if it.Sku == "" || it.Quantity < 1 {
// return fmt.Errorf("SetCartRequest: invalid item (sku='%s' qty=%d)", it.Sku, it.Quantity)
// }
// _, err := g.AddItem(it.Sku, int(it.Quantity), it.Country, it.StoreId)
// if err != nil {
// return fmt.Errorf("SetCartRequest: add sku '%s' failed: %w", it.Sku, err)
// }
// }
// return nil
// }