Compare commits
2 Commits
bc6c3ee692
...
c5bc17c44d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5bc17c44d | ||
|
|
d01b6675b4 |
@@ -25,11 +25,12 @@ func (id *CartId) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
|
||||
type CartItem struct {
|
||||
Id int `json:"id"`
|
||||
Sku string `json:"sku"`
|
||||
Name string `json:"name"`
|
||||
Price int64 `json:"price"`
|
||||
Image string `json:"image"`
|
||||
Id int `json:"id"`
|
||||
Sku string `json:"sku"`
|
||||
Name string `json:"name"`
|
||||
Price int64 `json:"price"`
|
||||
Quantity int `json:"qty"`
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
type CartDelivery struct {
|
||||
@@ -42,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 []string `json:"deliveries,omitempty"`
|
||||
}
|
||||
|
||||
type Grain interface {
|
||||
@@ -64,7 +65,7 @@ func (c *CartGrain) GetLastChange() int64 {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -87,7 +88,7 @@ func getItemData(sku string) (*messages.AddItem, error) {
|
||||
}
|
||||
|
||||
return &messages.AddItem{
|
||||
Quantity: 1,
|
||||
Quantity: int32(qty),
|
||||
Price: int64(price),
|
||||
Sku: sku,
|
||||
Name: item.Title,
|
||||
@@ -95,8 +96,8 @@ func getItemData(sku string) (*messages.AddItem, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *CartGrain) AddItem(sku string) ([]byte, error) {
|
||||
cartItem, err := getItemData(sku)
|
||||
func (c *CartGrain) AddItem(sku string, qty int) ([]byte, error) {
|
||||
cartItem, err := getItemData(sku, qty)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -130,22 +131,38 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) ([]byte, erro
|
||||
if !ok {
|
||||
err = fmt.Errorf("expected AddRequest")
|
||||
} else {
|
||||
return c.AddItem(msg.Sku)
|
||||
return c.AddItem(msg.Sku, 1) // extent AddRequest to include quantity
|
||||
}
|
||||
case AddItemType:
|
||||
msg, ok := message.Content.(*messages.AddItem)
|
||||
if !ok {
|
||||
err = fmt.Errorf("expected AddItem")
|
||||
} else {
|
||||
c.lastItemId++
|
||||
c.Items = append(c.Items, CartItem{
|
||||
Id: c.lastItemId,
|
||||
Sku: msg.Sku,
|
||||
Name: msg.Name,
|
||||
Price: msg.Price,
|
||||
Image: msg.Image,
|
||||
})
|
||||
c.TotalPrice += msg.Price * int64(msg.Quantity)
|
||||
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.Items = append(c.Items, &CartItem{
|
||||
Id: c.lastItemId,
|
||||
Quantity: int(msg.Quantity),
|
||||
Sku: msg.Sku,
|
||||
Name: msg.Name,
|
||||
Price: msg.Price,
|
||||
Image: msg.Image,
|
||||
})
|
||||
c.TotalPrice += msg.Price * int64(msg.Quantity)
|
||||
}
|
||||
|
||||
}
|
||||
case RemoveItemType:
|
||||
//msg, ok := message.Content.(*messages.RemoveItem)
|
||||
|
||||
@@ -39,5 +39,23 @@ func TestAddToCart(t *testing.T) {
|
||||
if grain.TotalPrice != 200 {
|
||||
t.Errorf("Expected total price 200, got %d\n", grain.TotalPrice)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
result, err = grain.HandleMessage(msg, false)
|
||||
if err != nil {
|
||||
t.Errorf("Error handling message: %v\n", err)
|
||||
}
|
||||
if len(result) == 0 {
|
||||
t.Errorf("Expected result, got nil\n")
|
||||
}
|
||||
if grain.Items[0].Quantity != 4 {
|
||||
t.Errorf("Expected quantity 4, got %d\n", grain.Items[0].Quantity)
|
||||
}
|
||||
if grain.TotalPrice != 400 {
|
||||
t.Errorf("Expected total price 400, got %d\n", grain.TotalPrice)
|
||||
}
|
||||
}
|
||||
|
||||
2
main.go
2
main.go
@@ -40,7 +40,7 @@ func spawn(id CartId) (*CartGrain, error) {
|
||||
lastDeliveryId: 0,
|
||||
Deliveries: []string{},
|
||||
Id: id,
|
||||
Items: []CartItem{},
|
||||
Items: []*CartItem{},
|
||||
storageMessages: []Message{},
|
||||
TotalPrice: 0,
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ func TestQueue(t *testing.T) {
|
||||
return &CartGrain{
|
||||
Id: id,
|
||||
storageMessages: []Message{},
|
||||
Items: []CartItem{},
|
||||
Items: []*CartItem{},
|
||||
TotalPrice: 0,
|
||||
}, nil
|
||||
})
|
||||
@@ -22,11 +22,7 @@ func TestQueue(t *testing.T) {
|
||||
err = pool.AddRemote("localhost")
|
||||
if err != nil {
|
||||
t.Errorf("Error adding remote: %v", err)
|
||||
}
|
||||
r := pool.remotes[0]
|
||||
|
||||
if len(r.PacketQueue.Packets) != 1 {
|
||||
t.Errorf("Expected 1 packet, got %d", len(r.PacketQueue.Packets))
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -387,7 +387,6 @@ func (p *SyncedPool) AddRemote(host string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
//pool := NewRemoteGrainPool(host)
|
||||
remote := RemoteHost{
|
||||
Client: client,
|
||||
MissedPings: 0,
|
||||
|
||||
@@ -12,7 +12,7 @@ func TestConnection(t *testing.T) {
|
||||
return &CartGrain{
|
||||
Id: id,
|
||||
storageMessages: []Message{},
|
||||
Items: []CartItem{},
|
||||
Items: []*CartItem{},
|
||||
TotalPrice: 0,
|
||||
}, nil
|
||||
})
|
||||
@@ -29,8 +29,8 @@ func TestConnection(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Error negotiating: %v", err)
|
||||
}
|
||||
if len(allHosts) != 1 {
|
||||
t.Errorf("Expected 1 host, got %d", len(allHosts))
|
||||
if len(allHosts) != 0 {
|
||||
t.Errorf("Expected 0 host, (host should be known) got %d", len(allHosts))
|
||||
}
|
||||
|
||||
data, err := pool.Get(ToCartId("kalle"))
|
||||
|
||||
Reference in New Issue
Block a user