support multiple mutations
All checks were successful
Build and Publish / Metadata (push) Successful in 9s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m13s
Build and Publish / BuildAndDeployArm64 (push) Successful in 3m54s

This commit is contained in:
2025-10-13 19:51:27 +02:00
parent 91e398dcc3
commit 8e60cc2239
5 changed files with 33 additions and 26 deletions

View File

@@ -29,8 +29,8 @@ func NewPoolServer(pool actor.GrainPool[*CartGrain], pod_name string, klarnaClie
}
}
func (s *PoolServer) ApplyLocal(id CartId, mutation proto.Message) (*CartGrain, error) {
return s.Apply(uint64(id), mutation)
func (s *PoolServer) ApplyLocal(id CartId, mutation ...proto.Message) (*CartGrain, error) {
return s.Apply(uint64(id), mutation...)
}
func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request, id CartId) error {
@@ -178,28 +178,30 @@ func (s *PoolServer) HandleSetCartItems(w http.ResponseWriter, r *http.Request,
if err != nil {
return err
}
reply, err := s.ApplyLocal(id, &messages.ClearCartRequest{})
if err != nil {
return err
}
msgs := make([]proto.Message, 0, len(setCartItems.Items)+1)
msgs = append(msgs, &messages.ClearCartRequest{})
wg := sync.WaitGroup{}
for _, item := range setCartItems.Items {
wg.Add(1)
go func(sku string, quantity int) {
defer wg.Done()
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()
msgs = append(msgs, msg)
}(item.Sku, item.Quantity)
}
wg.Wait()
reply, err := s.ApplyLocal(id, msgs...)
if err != nil {
return err
}
return s.WriteResult(w, reply)
}