more features
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 29s
Build and Publish / BuildAndDeploy (push) Successful in 2m23s

This commit is contained in:
matst80
2024-11-14 19:33:04 +01:00
parent d617fd9657
commit 3f6f78c839
5 changed files with 156 additions and 62 deletions

View File

@@ -26,13 +26,26 @@ func (id *CartId) UnmarshalJSON(data []byte) error {
return nil return nil
} }
type StockStatus int
const (
OutOfStock StockStatus = 0
LowStock StockStatus = 1
InStock StockStatus = 2
)
type CartItem struct { type CartItem struct {
Id int `json:"id"` Id int `json:"id"`
Sku string `json:"sku"` ParentId int `json:"parentId,omitempty"`
Name string `json:"name"` Sku string `json:"sku"`
Price int64 `json:"price"` Name string `json:"name"`
Quantity int `json:"qty"` Price int64 `json:"price"`
Image string `json:"image"` OrgPrice int64 `json:"orgPrice"`
Stock StockStatus `json:"stock"`
Quantity int `json:"qty"`
Tax int `json:"tax"`
Disclaimer string `json:"disclaimer,omitempty"`
Image string `json:"image,omitempty"`
} }
type CartDelivery struct { type CartDelivery struct {
@@ -79,34 +92,47 @@ func (c *CartGrain) GetCurrentState() (*FrameWithPayload, error) {
return &ret, nil return &ret, nil
} }
func getInt(data interface{}) (int, error) {
switch v := data.(type) {
case float64:
return int(v), nil
case int:
return v, nil
default:
return 0, fmt.Errorf("invalid type")
}
}
func getItemData(sku string, qty int) (*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
} }
price := 0 orgPrice, _ := getInt(item.Fields[6])
priceField, ok := item.Fields[4]
if ok { price, priceErr := getInt(item.Fields[5])
priceFloat, ok := priceField.(float64)
if !ok { if priceErr != nil {
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 nil, fmt.Errorf("invalid price")
} }
stock := InStock
if item.StockLevel == "0" || item.StockLevel == "" {
stock = OutOfStock
} else if item.StockLevel == "5+" {
stock = LowStock
}
return &messages.AddItem{ return &messages.AddItem{
Quantity: int32(qty), Quantity: int32(qty),
Price: int64(price), Price: int64(price),
Sku: sku, OrgPrice: int64(orgPrice),
Name: item.Title, Sku: sku,
Image: item.Img, Name: item.Title,
Image: item.Img,
Stock: int32(stock),
Tax: 2500,
Disclaimer: item.Disclaimer,
}, nil }, nil
} }
@@ -219,13 +245,21 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
} else { } else {
c.mu.Lock() c.mu.Lock()
c.lastItemId++ c.lastItemId++
tax := 2500
if msg.Tax > 0 {
tax = int(msg.Tax)
}
c.Items = append(c.Items, &CartItem{ c.Items = append(c.Items, &CartItem{
Id: c.lastItemId, Id: c.lastItemId,
Quantity: int(msg.Quantity), Quantity: int(msg.Quantity),
Sku: msg.Sku, Sku: msg.Sku,
Name: msg.Name, Name: msg.Name,
Price: msg.Price, Price: msg.Price,
Image: msg.Image, Image: msg.Image,
Stock: StockStatus(msg.Stock),
Disclaimer: msg.Disclaimer,
OrgPrice: msg.OrgPrice,
Tax: tax,
}) })
c.TotalPrice += msg.Price * int64(msg.Quantity) c.TotalPrice += msg.Price * int64(msg.Quantity)
c.mu.Unlock() c.mu.Unlock()

View File

@@ -67,6 +67,25 @@ func TestAddToCartShortCut(t *testing.T) {
} }
} }
func TestAddRequestToGrain(t *testing.T) {
grain, err := spawn(ToCartId("kalle"))
if err != nil {
t.Errorf("Error spawning: %v\n", err)
}
msg := GetMessage(AddRequestType, &messages.AddRequest{
Quantity: 2,
Sku: "763281",
})
result, err := grain.HandleMessage(msg, false)
if err != nil {
t.Errorf("Error handling message: %v\n", err)
}
if result.StatusCode != 200 {
t.Errorf("Call failed\n")
}
t.Log(result)
}
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 {

View File

@@ -6,9 +6,10 @@ import (
) )
func TestDiscardedHost(t *testing.T) { func TestDiscardedHost(t *testing.T) {
dh := NewDiscardedHostHandler(func(host string) { dh := NewDiscardedHostHandler(8080)
dh.SetReconnectHandler(func(host string) {
t.Log(host) t.Log(host)
}, 8080) })
dh.AppendHost("localhost") dh.AppendHost("localhost")
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
if dh.hosts[0].Tries == 0 { if dh.hosts[0].Tries == 0 {

View File

@@ -78,11 +78,15 @@ type AddItem struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Quantity int32 `protobuf:"varint,2,opt,name=Quantity,proto3" json:"Quantity,omitempty"` Quantity int32 `protobuf:"varint,2,opt,name=Quantity,proto3" json:"Quantity,omitempty"`
Price int64 `protobuf:"varint,3,opt,name=Price,proto3" json:"Price,omitempty"` Price int64 `protobuf:"varint,3,opt,name=Price,proto3" json:"Price,omitempty"`
Sku string `protobuf:"bytes,4,opt,name=Sku,proto3" json:"Sku,omitempty"` OrgPrice int64 `protobuf:"varint,9,opt,name=OrgPrice,proto3" json:"OrgPrice,omitempty"`
Name string `protobuf:"bytes,5,opt,name=Name,proto3" json:"Name,omitempty"` Sku string `protobuf:"bytes,4,opt,name=Sku,proto3" json:"Sku,omitempty"`
Image string `protobuf:"bytes,6,opt,name=Image,proto3" json:"Image,omitempty"` Name string `protobuf:"bytes,5,opt,name=Name,proto3" json:"Name,omitempty"`
Image string `protobuf:"bytes,6,opt,name=Image,proto3" json:"Image,omitempty"`
Stock int32 `protobuf:"varint,7,opt,name=Stock,proto3" json:"Stock,omitempty"`
Tax int32 `protobuf:"varint,8,opt,name=Tax,proto3" json:"Tax,omitempty"`
Disclaimer string `protobuf:"bytes,10,opt,name=Disclaimer,proto3" json:"Disclaimer,omitempty"`
} }
func (x *AddItem) Reset() { func (x *AddItem) Reset() {
@@ -129,6 +133,13 @@ func (x *AddItem) GetPrice() int64 {
return 0 return 0
} }
func (x *AddItem) GetOrgPrice() int64 {
if x != nil {
return x.OrgPrice
}
return 0
}
func (x *AddItem) GetSku() string { func (x *AddItem) GetSku() string {
if x != nil { if x != nil {
return x.Sku return x.Sku
@@ -150,6 +161,27 @@ func (x *AddItem) GetImage() string {
return "" return ""
} }
func (x *AddItem) GetStock() int32 {
if x != nil {
return x.Stock
}
return 0
}
func (x *AddItem) GetTax() int32 {
if x != nil {
return x.Tax
}
return 0
}
func (x *AddItem) GetDisclaimer() string {
if x != nil {
return x.Disclaimer
}
return ""
}
type RemoveItem struct { type RemoveItem struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
@@ -354,28 +386,34 @@ var file_messages_proto_rawDesc = []byte{
0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e,
0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e,
0x74, 0x69, 0x74, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x02, 0x20, 0x01, 0x28, 0x74, 0x69, 0x74, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x02, 0x20, 0x01, 0x28,
0x09, 0x52, 0x03, 0x53, 0x6b, 0x75, 0x22, 0x77, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x49, 0x74, 0x65, 0x09, 0x52, 0x03, 0x53, 0x6b, 0x75, 0x22, 0xdb, 0x01, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x49, 0x74,
0x6d, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02, 0x20, 0x65, 0x6d, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02,
0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x14, 0x0a, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x14,
0x05, 0x50, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x50, 0x72, 0x0a, 0x05, 0x50, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x50,
0x69, 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x72, 0x69, 0x63, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x4f, 0x72, 0x67, 0x50, 0x72, 0x69, 0x63, 0x65,
0x52, 0x03, 0x53, 0x6b, 0x75, 0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x18, 0x09, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x4f, 0x72, 0x67, 0x50, 0x72, 0x69, 0x63, 0x65,
0x01, 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x6d, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x53,
0x67, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x22, 0x6b, 0x75, 0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09,
0x1c, 0x0a, 0x0a, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x12, 0x0e, 0x0a, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x18,
0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x49, 0x64, 0x22, 0x3c, 0x0a, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05,
0x0e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x53, 0x74, 0x6f, 0x63, 0x6b, 0x18, 0x07, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x53, 0x74, 0x6f,
0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x49, 0x64, 0x12, 0x63, 0x6b, 0x12, 0x10, 0x0a, 0x03, 0x54, 0x61, 0x78, 0x18, 0x08, 0x20, 0x01, 0x28, 0x05, 0x52,
0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x54, 0x61, 0x78, 0x12, 0x1e, 0x0a, 0x0a, 0x44, 0x69, 0x73, 0x63, 0x6c, 0x61, 0x69, 0x6d,
0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x22, 0x3f, 0x0a, 0x0b, 0x53, 0x65, 0x72, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x44, 0x69, 0x73, 0x63, 0x6c, 0x61,
0x65, 0x74, 0x44, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72, 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x50, 0x72, 0x69, 0x6d, 0x65, 0x72, 0x22, 0x1c, 0x0a, 0x0a, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x49, 0x74,
0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x50, 0x72, 0x65, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02,
0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x18, 0x49, 0x64, 0x22, 0x3c, 0x0a, 0x0e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x51, 0x75, 0x61, 0x6e,
0x02, 0x20, 0x03, 0x28, 0x03, 0x52, 0x05, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x22, 0x20, 0x0a, 0x0e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03,
0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x44, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72, 0x79, 0x12, 0x0e, 0x52, 0x02, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79,
0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x49, 0x64, 0x42, 0x0c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79,
0x5a, 0x0a, 0x2e, 0x3b, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x22, 0x3f, 0x0a, 0x0b, 0x53, 0x65, 0x74, 0x44, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72, 0x79, 0x12,
0x6f, 0x74, 0x6f, 0x33, 0x1a, 0x0a, 0x08, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x08, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x49,
0x74, 0x65, 0x6d, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x03, 0x52, 0x05, 0x49, 0x74, 0x65, 0x6d,
0x73, 0x22, 0x20, 0x0a, 0x0e, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x44, 0x65, 0x6c, 0x69, 0x76,
0x65, 0x72, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52,
0x02, 0x49, 0x64, 0x42, 0x0c, 0x5a, 0x0a, 0x2e, 0x3b, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@@ -10,9 +10,13 @@ message AddRequest {
message AddItem { message AddItem {
int32 Quantity = 2; int32 Quantity = 2;
int64 Price = 3; int64 Price = 3;
int64 OrgPrice = 9;
string Sku = 4; string Sku = 4;
string Name = 5; string Name = 5;
string Image = 6; string Image = 6;
int32 Stock = 7;
int32 Tax = 8;
string Disclaimer = 10;
} }
message RemoveItem { message RemoveItem {
@@ -32,5 +36,3 @@ message SetDelivery {
message RemoveDelivery { message RemoveDelivery {
int64 Id = 1; int64 Id = 1;
} }