312 lines
6.7 KiB
Go
312 lines
6.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
messages "git.tornberg.me/go-cart-actor/proto"
|
|
)
|
|
|
|
type CartId [16]byte
|
|
|
|
func (id CartId) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(id.String())
|
|
}
|
|
|
|
func (id *CartId) UnmarshalJSON(data []byte) error {
|
|
var str string
|
|
err := json.Unmarshal(data, &str)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
copy(id[:], []byte(str))
|
|
return nil
|
|
}
|
|
|
|
type CartItem struct {
|
|
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 {
|
|
Provider string `json:"provider"`
|
|
Price int64 `json:"price"`
|
|
Items []int `json:"items"`
|
|
}
|
|
|
|
type CartGrain struct {
|
|
mu sync.RWMutex
|
|
lastItemId int
|
|
lastDeliveryId int
|
|
storageMessages []Message
|
|
Id CartId `json:"id"`
|
|
Items []*CartItem `json:"items"`
|
|
TotalPrice int64 `json:"totalPrice"`
|
|
Deliveries []CartDelivery `json:"deliveries,omitempty"`
|
|
}
|
|
|
|
type Grain interface {
|
|
GetId() CartId
|
|
HandleMessage(message *Message, isReplay bool) (*CallResult, error)
|
|
GetCurrentState() (*CallResult, error)
|
|
}
|
|
|
|
func (c *CartGrain) GetId() CartId {
|
|
return c.Id
|
|
}
|
|
|
|
func (c *CartGrain) GetLastChange() int64 {
|
|
if len(c.storageMessages) == 0 {
|
|
return 0
|
|
}
|
|
return *c.storageMessages[len(c.storageMessages)-1].TimeStamp
|
|
}
|
|
|
|
func (c *CartGrain) GetCurrentState() (*CallResult, error) {
|
|
result, err := json.Marshal(c)
|
|
return &CallResult{
|
|
StatusCode: 200,
|
|
Data: result,
|
|
}, err
|
|
}
|
|
|
|
func getItemData(sku string, qty int) (*messages.AddItem, error) {
|
|
item, err := FetchItem(sku)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
price := 0
|
|
priceField, ok := item.Fields[4]
|
|
if ok {
|
|
priceFloat, ok := priceField.(float64)
|
|
if !ok {
|
|
price, ok = priceField.(int)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid price type")
|
|
}
|
|
} else {
|
|
price = int(priceFloat)
|
|
}
|
|
}
|
|
if price == 0 {
|
|
return nil, fmt.Errorf("invalid price")
|
|
}
|
|
|
|
return &messages.AddItem{
|
|
Quantity: int32(qty),
|
|
Price: int64(price),
|
|
Sku: sku,
|
|
Name: item.Title,
|
|
Image: item.Img,
|
|
}, nil
|
|
}
|
|
|
|
func (c *CartGrain) AddItem(sku string, qty int) (*CallResult, error) {
|
|
cartItem, err := getItemData(sku, qty)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.HandleMessage(&Message{
|
|
Type: 2,
|
|
Content: cartItem,
|
|
}, false)
|
|
}
|
|
|
|
func (c *CartGrain) GetStorageMessage(since int64) []StorableMessage {
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
ret := make([]StorableMessage, 0)
|
|
|
|
for _, message := range c.storageMessages {
|
|
if *message.TimeStamp > since {
|
|
ret = append(ret, message)
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func (c *CartGrain) GetState() ([]byte, error) {
|
|
return json.Marshal(c)
|
|
}
|
|
|
|
func (c *CartGrain) ItemsWithDelivery() []int {
|
|
ret := make([]int, 0, len(c.Items))
|
|
for _, item := range c.Items {
|
|
for _, delivery := range c.Deliveries {
|
|
for _, id := range delivery.Items {
|
|
if item.Id == id {
|
|
ret = append(ret, id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func (c *CartGrain) ItemsWithoutDelivery() []int {
|
|
ret := make([]int, 0, len(c.Items))
|
|
hasDelivery := c.ItemsWithDelivery()
|
|
for _, item := range c.Items {
|
|
found := false
|
|
for _, id := range hasDelivery {
|
|
if item.Id == id {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
ret = append(ret, item.Id)
|
|
}
|
|
}
|
|
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) (*CallResult, error) {
|
|
if message.TimeStamp == nil {
|
|
now := time.Now().Unix()
|
|
message.TimeStamp = &now
|
|
}
|
|
grainMutations.Inc()
|
|
var err error
|
|
switch message.Type {
|
|
case AddRequestType:
|
|
msg, ok := message.Content.(*messages.AddRequest)
|
|
if !ok {
|
|
err = fmt.Errorf("expected AddRequest")
|
|
} else {
|
|
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:
|
|
msg, ok := message.Content.(*messages.AddItem)
|
|
if !ok {
|
|
err = fmt.Errorf("expected AddItem")
|
|
} else {
|
|
|
|
if msg.Quantity < 1 {
|
|
return nil, fmt.Errorf("invalid quantity")
|
|
}
|
|
existingItem, found := c.FindItemWithSku(msg.Sku)
|
|
if found {
|
|
existingItem.Quantity += int(msg.Quantity)
|
|
c.TotalPrice += existingItem.Price * int64(msg.Quantity)
|
|
} else {
|
|
c.mu.Lock()
|
|
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)
|
|
c.mu.Unlock()
|
|
}
|
|
}
|
|
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)
|
|
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 SetDeliveryType:
|
|
msg, ok := message.Content.(*messages.SetDelivery)
|
|
if !ok {
|
|
err = fmt.Errorf("expected SetDelivery")
|
|
} else {
|
|
c.lastDeliveryId++
|
|
items := make([]int, 0)
|
|
withDelivery := c.ItemsWithDelivery()
|
|
if len(msg.Items) == 0 {
|
|
items = append(items, c.ItemsWithoutDelivery()...)
|
|
} else {
|
|
for _, id := range msg.Items {
|
|
for _, item := range c.Items {
|
|
if item.Id == int(id) {
|
|
if slices.Contains(withDelivery, item.Id) {
|
|
return nil, fmt.Errorf("item already has delivery")
|
|
}
|
|
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)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !isReplay {
|
|
c.mu.Lock()
|
|
c.storageMessages = append(c.storageMessages, *message)
|
|
c.mu.Unlock()
|
|
}
|
|
result, err := json.Marshal(c)
|
|
return &CallResult{
|
|
StatusCode: 200,
|
|
Data: result,
|
|
}, err
|
|
}
|