stuffs
This commit is contained in:
141
cart-grain.go
Normal file
141
cart-grain.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/matst80/slask-finder/pkg/index"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
TimeStamp *int64
|
||||
Type string
|
||||
Content interface{}
|
||||
}
|
||||
|
||||
type CartItem struct {
|
||||
Sku string `json:"sku"`
|
||||
Name string `json:"name"`
|
||||
Price int `json:"price"`
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
type CartGrain struct {
|
||||
storageMessages []Message
|
||||
Id string `json:"id"`
|
||||
Items []CartItem `json:"items"`
|
||||
TotalPrice int `json:"totalPrice"`
|
||||
}
|
||||
|
||||
type Grain interface {
|
||||
GetId() string
|
||||
GetLastChange() int64
|
||||
HandleMessage(message *Message, isReplay bool, reply *CartGrain) error
|
||||
GetStorageMessage(since int64) []Message
|
||||
}
|
||||
|
||||
func (c *CartGrain) GetId() string {
|
||||
return c.Id
|
||||
}
|
||||
|
||||
func (c *CartGrain) GetLastChange() int64 {
|
||||
if len(c.storageMessages) == 0 {
|
||||
return 0
|
||||
}
|
||||
return *c.storageMessages[len(c.storageMessages)-1].TimeStamp
|
||||
}
|
||||
|
||||
func getItemData(sku string) (*CartItem, error) {
|
||||
res, err := http.Get("https://slask-finder.tornberg.me/api/get/" + sku)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
var item index.DataItem
|
||||
err = json.NewDecoder(res.Body).Decode(&item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
price := item.GetPrice()
|
||||
if price == 0 {
|
||||
priceField, ok := item.GetFields()[4]
|
||||
if ok {
|
||||
|
||||
pricef, ok := priceField.(float64)
|
||||
if !ok {
|
||||
price, ok = priceField.(int)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid price type")
|
||||
}
|
||||
} else {
|
||||
price = int(pricef)
|
||||
}
|
||||
}
|
||||
}
|
||||
return &CartItem{
|
||||
Sku: item.Sku,
|
||||
Name: item.Title,
|
||||
Price: price,
|
||||
Image: item.Img,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *CartGrain) AddItem(sku string, reply *CartGrain) error {
|
||||
cartItem, err := getItemData(sku)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.HandleMessage(&Message{
|
||||
Type: "append",
|
||||
Content: *cartItem,
|
||||
}, false, reply)
|
||||
}
|
||||
|
||||
func (c *CartGrain) GetStorageMessage(since int64) []Message {
|
||||
if since == 0 {
|
||||
return c.storageMessages
|
||||
}
|
||||
ret := make([]Message, 0)
|
||||
for _, message := range c.storageMessages {
|
||||
if *message.TimeStamp > since {
|
||||
ret = append(ret, message)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c *CartGrain) HandleMessage(message *Message, isReplay bool, reply *CartGrain) error {
|
||||
log.Printf("Handling message %s", message.Type)
|
||||
if message.TimeStamp == nil {
|
||||
now := time.Now().Unix()
|
||||
message.TimeStamp = &now
|
||||
}
|
||||
var err error
|
||||
switch message.Type {
|
||||
case "add":
|
||||
sku, ok := message.Content.(string)
|
||||
if !ok {
|
||||
err = fmt.Errorf("invalid content type")
|
||||
} else {
|
||||
return c.AddItem(sku, reply)
|
||||
}
|
||||
case "append":
|
||||
item, ok := message.Content.(CartItem)
|
||||
if !ok {
|
||||
err = fmt.Errorf("invalid content type")
|
||||
} else {
|
||||
c.Items = append(c.Items, item)
|
||||
c.TotalPrice += item.Price
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("unknown message type")
|
||||
}
|
||||
if !isReplay {
|
||||
c.storageMessages = append(c.storageMessages, *message)
|
||||
}
|
||||
*reply = *c
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user