better
This commit is contained in:
@@ -1,11 +1,5 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
messages "git.tornberg.me/go-cart-actor/pkg/messages"
|
||||
)
|
||||
|
||||
// mutation_add_request.go
|
||||
//
|
||||
// Registers the AddRequest mutation. This mutation is a higher-level intent
|
||||
@@ -30,32 +24,28 @@ import (
|
||||
// - Stock validation before increasing quantity
|
||||
// - Reservation logic or concurrency guards around stock updates
|
||||
// - Coupon / pricing rules applied conditionally during add-by-sku
|
||||
func init() {
|
||||
RegisterMutation[messages.AddRequest](
|
||||
"AddRequest",
|
||||
func(g *CartGrain, m *messages.AddRequest) error {
|
||||
if m == nil {
|
||||
return fmt.Errorf("AddRequest: nil payload")
|
||||
}
|
||||
if m.Sku == "" {
|
||||
return fmt.Errorf("AddRequest: sku is empty")
|
||||
}
|
||||
if m.Quantity < 1 {
|
||||
return fmt.Errorf("AddRequest: invalid quantity %d", m.Quantity)
|
||||
}
|
||||
|
||||
// Existing line: accumulate quantity only.
|
||||
if existing, found := g.FindItemWithSku(m.Sku); found {
|
||||
existing.Quantity += int(m.Quantity)
|
||||
return nil
|
||||
}
|
||||
// func HandleAddRequest(g *CartGrain, m *messages.AddRequest) error {
|
||||
// if m == nil {
|
||||
// return fmt.Errorf("AddRequest: nil payload")
|
||||
// }
|
||||
// if m.Sku == "" {
|
||||
// return fmt.Errorf("AddRequest: sku is empty")
|
||||
// }
|
||||
// if m.Quantity < 1 {
|
||||
// return fmt.Errorf("AddRequest: invalid quantity %d", m.Quantity)
|
||||
// }
|
||||
|
||||
// New line: delegate to higher-level AddItem flow (product lookup).
|
||||
// We intentionally ignore the returned *CartGrain; registry will
|
||||
// do totals again after this handler returns (harmless).
|
||||
_, err := g.AddItem(m.Sku, int(m.Quantity), m.Country, m.StoreId)
|
||||
return err
|
||||
},
|
||||
WithTotals(),
|
||||
)
|
||||
}
|
||||
// // Existing line: accumulate quantity only.
|
||||
// if existing, found := g.FindItemWithSku(m.Sku); found {
|
||||
// existing.Quantity += int(m.Quantity)
|
||||
// return nil
|
||||
// }
|
||||
// data, err := GetItemAddMessage(m.Sku, int(m.Quantity), m.Country, m.StoreId)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return AddItem(g, data)
|
||||
|
||||
// return err
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user