cleanup
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.tornberg.me/go-cart-actor/pkg/actor"
|
||||
@@ -15,25 +16,25 @@ import (
|
||||
)
|
||||
|
||||
type PoolServer struct {
|
||||
actor.GrainPool[*CartGrain]
|
||||
pod_name string
|
||||
pool actor.GrainPool[*CartGrain]
|
||||
klarnaClient *KlarnaClient
|
||||
}
|
||||
|
||||
func NewPoolServer(pool actor.GrainPool[*CartGrain], pod_name string, klarnaClient *KlarnaClient) *PoolServer {
|
||||
return &PoolServer{
|
||||
GrainPool: pool,
|
||||
pod_name: pod_name,
|
||||
pool: pool,
|
||||
klarnaClient: klarnaClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PoolServer) ApplyLocal(id CartId, mutation proto.Message) (*CartGrain, error) {
|
||||
return s.pool.Apply(uint64(id), mutation)
|
||||
return s.Apply(uint64(id), mutation)
|
||||
}
|
||||
|
||||
func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request, id CartId) error {
|
||||
grain, err := s.pool.Get(uint64(id))
|
||||
grain, err := s.Get(uint64(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -163,16 +164,42 @@ func (s *PoolServer) HandleQuantityChange(w http.ResponseWriter, r *http.Request
|
||||
return s.WriteResult(w, reply)
|
||||
}
|
||||
|
||||
type SetCartItems struct {
|
||||
Country string `json:"country"`
|
||||
Items []struct {
|
||||
Sku string `json:"sku"`
|
||||
Quantity int `json:"quantity"`
|
||||
} `json:"items"`
|
||||
}
|
||||
|
||||
func (s *PoolServer) HandleSetCartItems(w http.ResponseWriter, r *http.Request, id CartId) error {
|
||||
setCartItems := messages.SetCartRequest{}
|
||||
setCartItems := SetCartItems{}
|
||||
err := json.NewDecoder(r.Body).Decode(&setCartItems)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reply, err := s.ApplyLocal(id, &setCartItems)
|
||||
reply, err := s.ApplyLocal(id, &messages.ClearCartRequest{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wg := sync.WaitGroup{}
|
||||
for _, item := range setCartItems.Items {
|
||||
wg.Add(1)
|
||||
go func(sku string, quantity int) {
|
||||
msg, err := GetItemAddMessage(sku, quantity, setCartItems.Country, nil)
|
||||
if err != nil {
|
||||
log.Printf("error adding item %s: %v", sku, err)
|
||||
return
|
||||
}
|
||||
reply, err = s.ApplyLocal(id, msg)
|
||||
if err != nil {
|
||||
log.Printf("error applying message %v: %v", msg, err)
|
||||
return
|
||||
}
|
||||
wg.Done()
|
||||
}(item.Sku, item.Quantity)
|
||||
}
|
||||
wg.Wait()
|
||||
return s.WriteResult(w, reply)
|
||||
}
|
||||
|
||||
@@ -241,7 +268,7 @@ func (s *PoolServer) CreateOrUpdateCheckout(host string, id CartId) (*CheckoutOr
|
||||
}
|
||||
|
||||
// Get current grain state (may be local or remote)
|
||||
grain, err := s.pool.Get(uint64(id))
|
||||
grain, err := s.Get(uint64(id))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -261,7 +288,7 @@ func (s *PoolServer) CreateOrUpdateCheckout(host string, id CartId) (*CheckoutOr
|
||||
|
||||
func (s *PoolServer) ApplyCheckoutStarted(klarnaOrder *CheckoutOrder, id CartId) (*CartGrain, error) {
|
||||
// Persist initialization state via mutation (best-effort)
|
||||
return s.pool.Apply(uint64(id), &messages.InitializeCheckout{
|
||||
return s.Apply(uint64(id), &messages.InitializeCheckout{
|
||||
OrderId: klarnaOrder.ID,
|
||||
Status: klarnaOrder.Status,
|
||||
PaymentInProgress: true,
|
||||
@@ -373,7 +400,7 @@ func CartIdHandler(fn func(cartId CartId, w http.ResponseWriter, r *http.Request
|
||||
|
||||
func (s *PoolServer) ProxyHandler(fn func(w http.ResponseWriter, r *http.Request, cartId CartId) error) func(cartId CartId, w http.ResponseWriter, r *http.Request) error {
|
||||
return func(cartId CartId, w http.ResponseWriter, r *http.Request) error {
|
||||
if ownerHost, ok := s.pool.OwnerHost(uint64(cartId)); ok {
|
||||
if ownerHost, ok := s.OwnerHost(uint64(cartId)); ok {
|
||||
handled, err := ownerHost.Proxy(uint64(cartId), w, r)
|
||||
if err == nil && handled {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user