update
This commit is contained in:
@@ -94,7 +94,7 @@ func main() {
|
|||||||
|
|
||||||
_ = os.MkdirAll(dataDir, 0755)
|
_ = os.MkdirAll(dataDir, 0755)
|
||||||
|
|
||||||
reg := cart.NewCartMultationRegistry()
|
reg := cart.NewCartMultationRegistry(nil)
|
||||||
diskStorage := actor.NewDiskStorage[cart.CartGrain](dataDir, reg)
|
diskStorage := actor.NewDiskStorage[cart.CartGrain](dataDir, reg)
|
||||||
|
|
||||||
fs := NewFileServer(dataDir, diskStorage)
|
fs := NewFileServer(dataDir, diskStorage)
|
||||||
|
|||||||
@@ -1,21 +1,56 @@
|
|||||||
package cart
|
package cart
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"git.k6n.net/go-cart-actor/pkg/actor"
|
"git.k6n.net/go-cart-actor/pkg/actor"
|
||||||
messages "git.k6n.net/go-cart-actor/pkg/messages"
|
messages "git.k6n.net/go-cart-actor/pkg/messages"
|
||||||
|
"github.com/matst80/go-redis-inventory/pkg/inventory"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCartMultationRegistry() actor.MutationRegistry {
|
type CartMutationContext struct {
|
||||||
|
reservationService inventory.CartReservationService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCartMutationContext(reservationService inventory.CartReservationService) CartMutationContext {
|
||||||
|
return CartMutationContext{
|
||||||
|
reservationService: reservationService,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CartMutationContext) ReserveItem(ctx context.Context, cartId CartId, sku string, locationId string, quantity int) error {
|
||||||
|
if quantity <= 0 || c.reservationService == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.reservationService.ReserveForCart(ctx, inventory.CartReserveRequest{
|
||||||
|
CartID: inventory.CartID(cartId.String()),
|
||||||
|
InventoryReference: &inventory.InventoryReference{
|
||||||
|
SKU: inventory.SKU(sku),
|
||||||
|
LocationID: inventory.LocationID(locationId),
|
||||||
|
},
|
||||||
|
|
||||||
|
Quantity: uint32(quantity),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CartMutationContext) ReleaseItem(ctx context.Context, cartId CartId, sku string, locationId string) error {
|
||||||
|
if c.reservationService == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.reservationService.ReleaseForCart(ctx, inventory.SKU(sku), inventory.LocationID(locationId), inventory.CartID(cartId.String()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCartMultationRegistry(context *CartMutationContext) actor.MutationRegistry {
|
||||||
|
|
||||||
reg := actor.NewMutationRegistry()
|
reg := actor.NewMutationRegistry()
|
||||||
reg.RegisterMutations(
|
reg.RegisterMutations(
|
||||||
actor.NewMutation(AddItem, func() *messages.AddItem {
|
actor.NewMutation(context.AddItem, func() *messages.AddItem {
|
||||||
return &messages.AddItem{}
|
return &messages.AddItem{}
|
||||||
}),
|
}),
|
||||||
actor.NewMutation(ChangeQuantity, func() *messages.ChangeQuantity {
|
actor.NewMutation(context.ChangeQuantity, func() *messages.ChangeQuantity {
|
||||||
return &messages.ChangeQuantity{}
|
return &messages.ChangeQuantity{}
|
||||||
}),
|
}),
|
||||||
actor.NewMutation(RemoveItem, func() *messages.RemoveItem {
|
actor.NewMutation(context.RemoveItem, func() *messages.RemoveItem {
|
||||||
return &messages.RemoveItem{}
|
return &messages.RemoveItem{}
|
||||||
}),
|
}),
|
||||||
actor.NewMutation(InitializeCheckout, func() *messages.InitializeCheckout {
|
actor.NewMutation(InitializeCheckout, func() *messages.InitializeCheckout {
|
||||||
@@ -45,7 +80,7 @@ func NewCartMultationRegistry() actor.MutationRegistry {
|
|||||||
actor.NewMutation(UpsertSubscriptionDetails, func() *messages.UpsertSubscriptionDetails {
|
actor.NewMutation(UpsertSubscriptionDetails, func() *messages.UpsertSubscriptionDetails {
|
||||||
return &messages.UpsertSubscriptionDetails{}
|
return &messages.UpsertSubscriptionDetails{}
|
||||||
}),
|
}),
|
||||||
actor.NewMutation(InventoryReserved, func() *messages.InventoryReserved {
|
actor.NewMutation(context.InventoryReserved, func() *messages.InventoryReserved {
|
||||||
return &messages.InventoryReserved{}
|
return &messages.InventoryReserved{}
|
||||||
}),
|
}),
|
||||||
actor.NewMutation(PreConditionFailed, func() *messages.PreConditionFailed {
|
actor.NewMutation(PreConditionFailed, func() *messages.PreConditionFailed {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import (
|
|||||||
// NOTE: Any future field additions in messages.AddItem that affect pricing / tax
|
// NOTE: Any future field additions in messages.AddItem that affect pricing / tax
|
||||||
// must keep this handler in sync.
|
// must keep this handler in sync.
|
||||||
|
|
||||||
func AddItem(g *CartGrain, m *messages.AddItem) error {
|
func (c *CartMutationContext) AddItem(g *CartGrain, m *messages.AddItem) error {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return fmt.Errorf("AddItem: nil payload")
|
return fmt.Errorf("AddItem: nil payload")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import (
|
|||||||
// (If strict locking is required around every mutation, wrap logic in
|
// (If strict locking is required around every mutation, wrap logic in
|
||||||
// an explicit g.mu.Lock()/Unlock(), but current model mirrors prior code.)
|
// an explicit g.mu.Lock()/Unlock(), but current model mirrors prior code.)
|
||||||
|
|
||||||
func ChangeQuantity(g *CartGrain, m *messages.ChangeQuantity) error {
|
func (c *CartMutationContext) ChangeQuantity(g *CartGrain, m *messages.ChangeQuantity) error {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return fmt.Errorf("ChangeQuantity: nil payload")
|
return fmt.Errorf("ChangeQuantity: nil payload")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package cart
|
|||||||
|
|
||||||
import "git.k6n.net/go-cart-actor/pkg/messages"
|
import "git.k6n.net/go-cart-actor/pkg/messages"
|
||||||
|
|
||||||
func InventoryReserved(g *CartGrain, m *messages.InventoryReserved) error {
|
func (c *CartMutationContext) InventoryReserved(g *CartGrain, m *messages.InventoryReserved) error {
|
||||||
g.InventoryReserved = true
|
g.InventoryReserved = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import (
|
|||||||
// - If multiple lines somehow shared the same Id (should not happen), only
|
// - If multiple lines somehow shared the same Id (should not happen), only
|
||||||
// the first match would be removed—data integrity relies on unique line Ids.
|
// the first match would be removed—data integrity relies on unique line Ids.
|
||||||
|
|
||||||
func RemoveItem(g *CartGrain, m *messages.RemoveItem) error {
|
func (c *CartMutationContext) RemoveItem(g *CartGrain, m *messages.RemoveItem) error {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return fmt.Errorf("RemoveItem: nil payload")
|
return fmt.Errorf("RemoveItem: nil payload")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gogo/protobuf/proto"
|
"github.com/gogo/protobuf/proto"
|
||||||
|
"github.com/matst80/go-redis-inventory/pkg/inventory"
|
||||||
"google.golang.org/protobuf/types/known/anypb"
|
"google.golang.org/protobuf/types/known/anypb"
|
||||||
|
|
||||||
"git.k6n.net/go-cart-actor/pkg/actor"
|
"git.k6n.net/go-cart-actor/pkg/actor"
|
||||||
@@ -25,8 +26,28 @@ func newTestGrain() *CartGrain {
|
|||||||
return NewCartGrain(123, time.Now())
|
return NewCartGrain(123, time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MockReservationService struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockReservationService) ReserveForCart(ctx context.Context, req inventory.CartReserveRequest) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockReservationService) ReleaseForCart(ctx context.Context, sku inventory.SKU, locationID inventory.LocationID, cartID inventory.CartID) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockReservationService) GetAvailableInventory(ctx context.Context, sku inventory.SKU, locationID inventory.LocationID) (uint32, error) {
|
||||||
|
return 1000, nil
|
||||||
|
}
|
||||||
|
|
||||||
func newRegistry() actor.MutationRegistry {
|
func newRegistry() actor.MutationRegistry {
|
||||||
return NewCartMultationRegistry()
|
cartCtx := &CartMutationContext{
|
||||||
|
reservationService: &MockReservationService{
|
||||||
|
reservations: []messages.Reservation{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return NewCartMultationRegistry(cartCtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func msgAddItem(sku string, price int64, qty int32, storePtr *string) *messages.AddItem {
|
func msgAddItem(sku string, price int64, qty int32, storePtr *string) *messages.AddItem {
|
||||||
|
|||||||
Reference in New Issue
Block a user