more features
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m52s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m52s
This commit is contained in:
@@ -43,10 +43,10 @@ type CartGrain struct {
|
||||
lastItemId int
|
||||
lastDeliveryId int
|
||||
storageMessages []Message
|
||||
Id CartId `json:"id"`
|
||||
Items []*CartItem `json:"items"`
|
||||
TotalPrice int64 `json:"totalPrice"`
|
||||
Deliveries []string `json:"deliveries,omitempty"`
|
||||
Id CartId `json:"id"`
|
||||
Items []*CartItem `json:"items"`
|
||||
TotalPrice int64 `json:"totalPrice"`
|
||||
Deliveries []CartDelivery `json:"deliveries,omitempty"`
|
||||
}
|
||||
|
||||
type Grain interface {
|
||||
@@ -162,13 +162,60 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) ([]byte, erro
|
||||
})
|
||||
c.TotalPrice += msg.Price * int64(msg.Quantity)
|
||||
}
|
||||
}
|
||||
case ChangeQuantityType:
|
||||
msg, ok := message.Content.(*messages.ChangeQuantity)
|
||||
if !ok {
|
||||
err = fmt.Errorf("expected RemoveItem")
|
||||
} else {
|
||||
for i, item := range c.Items {
|
||||
if item.Id == int(msg.Id) {
|
||||
if item.Quantity <= int(msg.Quantity) {
|
||||
c.Items = append(c.Items[:i], c.Items[i+1:]...)
|
||||
|
||||
} else {
|
||||
item.Quantity -= int(msg.Quantity)
|
||||
}
|
||||
c.TotalPrice -= item.Price * int64(msg.Quantity)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
case RemoveItemType:
|
||||
//msg, ok := message.Content.(*messages.RemoveItem)
|
||||
msg, ok := message.Content.(*messages.RemoveItem)
|
||||
if !ok {
|
||||
err = fmt.Errorf("expected RemoveItem")
|
||||
} else {
|
||||
for i, item := range c.Items {
|
||||
if item.Id == int(msg.Id) {
|
||||
c.TotalPrice -= item.Price * int64(item.Quantity)
|
||||
c.Items = append(c.Items[:i], c.Items[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
case AddDeliveryType:
|
||||
c.lastDeliveryId++
|
||||
//msg, ok := message.Content.(*messages.AddDelivery)
|
||||
msg, ok := message.Content.(*messages.SetDelivery)
|
||||
if !ok {
|
||||
err = fmt.Errorf("expected SetDelivery")
|
||||
} else {
|
||||
c.lastDeliveryId++
|
||||
items := make([]int, 0)
|
||||
for _, id := range msg.Items {
|
||||
for _, item := range c.Items {
|
||||
if item.Id == int(id) {
|
||||
items = append(items, int(item.Id))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
c.Deliveries = append(c.Deliveries, CartDelivery{
|
||||
Provider: msg.Provider,
|
||||
Price: 49,
|
||||
Items: items,
|
||||
})
|
||||
}
|
||||
case RemoveDeliveryType:
|
||||
default:
|
||||
err = fmt.Errorf("unknown message type %d", message.Type)
|
||||
|
||||
Reference in New Issue
Block a user