update
This commit is contained in:
@@ -1,21 +1,56 @@
|
||||
package cart
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.k6n.net/go-cart-actor/pkg/actor"
|
||||
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.RegisterMutations(
|
||||
actor.NewMutation(AddItem, func() *messages.AddItem {
|
||||
actor.NewMutation(context.AddItem, func() *messages.AddItem {
|
||||
return &messages.AddItem{}
|
||||
}),
|
||||
actor.NewMutation(ChangeQuantity, func() *messages.ChangeQuantity {
|
||||
actor.NewMutation(context.ChangeQuantity, func() *messages.ChangeQuantity {
|
||||
return &messages.ChangeQuantity{}
|
||||
}),
|
||||
actor.NewMutation(RemoveItem, func() *messages.RemoveItem {
|
||||
actor.NewMutation(context.RemoveItem, func() *messages.RemoveItem {
|
||||
return &messages.RemoveItem{}
|
||||
}),
|
||||
actor.NewMutation(InitializeCheckout, func() *messages.InitializeCheckout {
|
||||
@@ -45,7 +80,7 @@ func NewCartMultationRegistry() actor.MutationRegistry {
|
||||
actor.NewMutation(UpsertSubscriptionDetails, func() *messages.UpsertSubscriptionDetails {
|
||||
return &messages.UpsertSubscriptionDetails{}
|
||||
}),
|
||||
actor.NewMutation(InventoryReserved, func() *messages.InventoryReserved {
|
||||
actor.NewMutation(context.InventoryReserved, func() *messages.InventoryReserved {
|
||||
return &messages.InventoryReserved{}
|
||||
}),
|
||||
actor.NewMutation(PreConditionFailed, func() *messages.PreConditionFailed {
|
||||
|
||||
Reference in New Issue
Block a user