quantity
This commit is contained in:
@@ -29,6 +29,7 @@ type CartItem struct {
|
|||||||
Sku string `json:"sku"`
|
Sku string `json:"sku"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Price int64 `json:"price"`
|
Price int64 `json:"price"`
|
||||||
|
Quantity int `json:"qty"`
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ func (c *CartGrain) GetLastChange() int64 {
|
|||||||
return *c.storageMessages[len(c.storageMessages)-1].TimeStamp
|
return *c.storageMessages[len(c.storageMessages)-1].TimeStamp
|
||||||
}
|
}
|
||||||
|
|
||||||
func getItemData(sku string) (*messages.AddItem, error) {
|
func getItemData(sku string, qty int) (*messages.AddItem, error) {
|
||||||
item, err := FetchItem(sku)
|
item, err := FetchItem(sku)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -87,7 +88,7 @@ func getItemData(sku string) (*messages.AddItem, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &messages.AddItem{
|
return &messages.AddItem{
|
||||||
Quantity: 1,
|
Quantity: int32(qty),
|
||||||
Price: int64(price),
|
Price: int64(price),
|
||||||
Sku: sku,
|
Sku: sku,
|
||||||
Name: item.Title,
|
Name: item.Title,
|
||||||
@@ -95,8 +96,8 @@ func getItemData(sku string) (*messages.AddItem, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CartGrain) AddItem(sku string) ([]byte, error) {
|
func (c *CartGrain) AddItem(sku string, qty int) ([]byte, error) {
|
||||||
cartItem, err := getItemData(sku)
|
cartItem, err := getItemData(sku, qty)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -130,21 +131,36 @@ 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)
|
return c.AddItem(msg.Sku, 1) // 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 {
|
||||||
|
return nil, fmt.Errorf("invalid quantity")
|
||||||
|
}
|
||||||
|
for _, item := range c.Items {
|
||||||
|
if item.Sku == msg.Sku {
|
||||||
|
found = true
|
||||||
|
item.Quantity += int(msg.Quantity)
|
||||||
|
c.TotalPrice += item.Price * int64(msg.Quantity)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
c.lastItemId++
|
c.lastItemId++
|
||||||
c.Items = append(c.Items, CartItem{
|
c.Items = append(c.Items, CartItem{
|
||||||
Id: c.lastItemId,
|
Id: c.lastItemId,
|
||||||
|
|
||||||
Sku: msg.Sku,
|
Sku: msg.Sku,
|
||||||
Name: msg.Name,
|
Name: msg.Name,
|
||||||
Price: msg.Price,
|
Price: msg.Price,
|
||||||
Image: msg.Image,
|
Image: msg.Image,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
c.TotalPrice += msg.Price * int64(msg.Quantity)
|
c.TotalPrice += msg.Price * int64(msg.Quantity)
|
||||||
}
|
}
|
||||||
case RemoveItemType:
|
case RemoveItemType:
|
||||||
|
|||||||
Reference in New Issue
Block a user