shortcut quantity change
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 2m1s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 2m1s
This commit is contained in:
@@ -160,6 +160,17 @@ func (c *CartGrain) ItemsWithoutDelivery() []int {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CartGrain) FindItemWithSku(sku string) (*CartItem, bool) {
|
||||||
|
c.mu.RLock()
|
||||||
|
defer c.mu.RUnlock()
|
||||||
|
for _, item := range c.Items {
|
||||||
|
if item.Sku == sku {
|
||||||
|
return item, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
func (c *CartGrain) HandleMessage(message *Message, isReplay bool) ([]byte, error) {
|
func (c *CartGrain) HandleMessage(message *Message, isReplay bool) ([]byte, error) {
|
||||||
if message.TimeStamp == nil {
|
if message.TimeStamp == nil {
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
@@ -173,26 +184,28 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) ([]byte, erro
|
|||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected AddRequest")
|
err = fmt.Errorf("expected AddRequest")
|
||||||
} else {
|
} else {
|
||||||
return c.AddItem(msg.Sku, 1) // extent AddRequest to include quantity
|
existingItem, found := c.FindItemWithSku(msg.Sku)
|
||||||
|
if found {
|
||||||
|
existingItem.Quantity += int(msg.Quantity)
|
||||||
|
c.TotalPrice += existingItem.Price * int64(msg.Quantity)
|
||||||
|
} else {
|
||||||
|
return c.AddItem(msg.Sku, int(msg.Quantity)) // extent AddRequest to include quantity
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case AddItemType:
|
case AddItemType:
|
||||||
msg, ok := message.Content.(*messages.AddItem)
|
msg, ok := message.Content.(*messages.AddItem)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected AddItem")
|
err = fmt.Errorf("expected AddItem")
|
||||||
} else {
|
} else {
|
||||||
found := false
|
|
||||||
if msg.Quantity < 1 {
|
if msg.Quantity < 1 {
|
||||||
return nil, fmt.Errorf("invalid quantity")
|
return nil, fmt.Errorf("invalid quantity")
|
||||||
}
|
}
|
||||||
for _, item := range c.Items {
|
existingItem, found := c.FindItemWithSku(msg.Sku)
|
||||||
if item.Sku == msg.Sku {
|
if found {
|
||||||
found = true
|
existingItem.Quantity += int(msg.Quantity)
|
||||||
item.Quantity += int(msg.Quantity)
|
c.TotalPrice += existingItem.Price * int64(msg.Quantity)
|
||||||
c.TotalPrice += item.Price * int64(msg.Quantity)
|
} else {
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
c.lastItemId++
|
c.lastItemId++
|
||||||
c.Items = append(c.Items, &CartItem{
|
c.Items = append(c.Items, &CartItem{
|
||||||
|
|||||||
@@ -16,6 +16,57 @@ func GetMessage(t uint16, data interface{}) *Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddToCartShortCut(t *testing.T) {
|
||||||
|
grain, err := spawn(ToCartId("kalle"))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error spawning: %v\n", err)
|
||||||
|
}
|
||||||
|
if len(grain.Items) != 0 {
|
||||||
|
t.Errorf("Expected 0 items, got %d\n", len(grain.Items))
|
||||||
|
}
|
||||||
|
msg := GetMessage(AddItemType, &messages.AddItem{
|
||||||
|
Quantity: 2,
|
||||||
|
Price: 100,
|
||||||
|
Sku: "123",
|
||||||
|
Name: "Test item",
|
||||||
|
Image: "test.jpg",
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err = grain.HandleMessage(msg, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error handling message: %v\n", err)
|
||||||
|
}
|
||||||
|
if len(grain.Items) != 1 {
|
||||||
|
t.Errorf("Expected 1 item, got %d\n", len(grain.Items))
|
||||||
|
}
|
||||||
|
if grain.Items[0].Quantity != 2 {
|
||||||
|
t.Errorf("Expected quantity 2, got %d\n", grain.Items[0].Quantity)
|
||||||
|
}
|
||||||
|
if len(grain.storageMessages) != 1 {
|
||||||
|
t.Errorf("Expected 1 storage message, got %d\n", len(grain.storageMessages))
|
||||||
|
}
|
||||||
|
shortCutMessage := GetMessage(AddRequestType, &messages.AddRequest{
|
||||||
|
Quantity: 2,
|
||||||
|
Sku: "123",
|
||||||
|
})
|
||||||
|
_, err = grain.HandleMessage(shortCutMessage, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error handling message: %v\n", err)
|
||||||
|
}
|
||||||
|
if len(grain.Items) != 1 {
|
||||||
|
t.Errorf("Expected 1 item, got %d\n", len(grain.Items))
|
||||||
|
}
|
||||||
|
if len(grain.storageMessages) != 2 {
|
||||||
|
t.Errorf("Expected 2 storage message, got %d\n", len(grain.storageMessages))
|
||||||
|
}
|
||||||
|
if grain.storageMessages[0].Type != AddItemType {
|
||||||
|
t.Errorf("Expected AddItemType, got %d\n", grain.storageMessages[0].Type)
|
||||||
|
}
|
||||||
|
if grain.storageMessages[1].Type != AddRequestType {
|
||||||
|
t.Errorf("Expected AddRequestType, got %d\n", grain.storageMessages[1].Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddToCart(t *testing.T) {
|
func TestAddToCart(t *testing.T) {
|
||||||
grain, err := spawn(ToCartId("kalle"))
|
grain, err := spawn(ToCartId("kalle"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user