missing updates #5

Merged
mats merged 77 commits from refactor/http-proxy into main 2025-10-14 23:12:06 +02:00
Showing only changes of commit 27c866ce58 - Show all commits

View File

@@ -164,12 +164,34 @@ func (s *PoolServer) HandleQuantityChange(w http.ResponseWriter, r *http.Request
return s.WriteResult(w, reply) return s.WriteResult(w, reply)
} }
type Item struct {
Sku string `json:"sku"`
Quantity int `json:"quantity"`
StoreId *string `json:"storeId,omitempty"`
}
type SetCartItems struct { type SetCartItems struct {
Country string `json:"country"` Country string `json:"country"`
Items []struct { Items []Item `json:"items"`
Sku string `json:"sku"` }
Quantity int `json:"quantity"`
} `json:"items"` func getMultipleAddMessages(items []Item, country string) []proto.Message {
wg := sync.WaitGroup{}
msgs := make([]proto.Message, 0, len(items))
for _, itm := range items {
wg.Add(1)
go func(itm Item) {
defer wg.Done()
msg, err := GetItemAddMessage(itm.Sku, itm.Quantity, country, itm.StoreId)
if err != nil {
log.Printf("error adding item %s: %v", itm.Sku, err)
return
}
msgs = append(msgs, msg)
}(itm)
}
wg.Wait()
return msgs
} }
func (s *PoolServer) HandleSetCartItems(w http.ResponseWriter, r *http.Request, id CartId) error { func (s *PoolServer) HandleSetCartItems(w http.ResponseWriter, r *http.Request, id CartId) error {
@@ -181,22 +203,7 @@ func (s *PoolServer) HandleSetCartItems(w http.ResponseWriter, r *http.Request,
msgs := make([]proto.Message, 0, len(setCartItems.Items)+1) msgs := make([]proto.Message, 0, len(setCartItems.Items)+1)
msgs = append(msgs, &messages.ClearCartRequest{}) msgs = append(msgs, &messages.ClearCartRequest{})
wg := sync.WaitGroup{} msgs = append(msgs, getMultipleAddMessages(setCartItems.Items, setCartItems.Country)...)
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
}
msgs = append(msgs, msg)
}(item.Sku, item.Quantity)
}
wg.Wait()
reply, err := s.ApplyLocal(id, msgs...) reply, err := s.ApplyLocal(id, msgs...)
if err != nil { if err != nil {
@@ -205,6 +212,20 @@ func (s *PoolServer) HandleSetCartItems(w http.ResponseWriter, r *http.Request,
return s.WriteResult(w, reply) return s.WriteResult(w, reply)
} }
func (s *PoolServer) HandleAddMultiple(w http.ResponseWriter, r *http.Request, id CartId) error {
setCartItems := SetCartItems{}
err := json.NewDecoder(r.Body).Decode(&setCartItems)
if err != nil {
return err
}
reply, err := s.ApplyLocal(id, getMultipleAddMessages(setCartItems.Items, setCartItems.Country)...)
if err != nil {
return err
}
return s.WriteResult(w, reply)
}
func (s *PoolServer) HandleAddRequest(w http.ResponseWriter, r *http.Request, id CartId) error { func (s *PoolServer) HandleAddRequest(w http.ResponseWriter, r *http.Request, id CartId) error {
addRequest := messages.AddRequest{} addRequest := messages.AddRequest{}
err := json.NewDecoder(r.Body).Decode(&addRequest) err := json.NewDecoder(r.Body).Decode(&addRequest)
@@ -426,6 +447,7 @@ func (s *PoolServer) Serve() *http.ServeMux {
mux.HandleFunc("GET /", CookieCartIdHandler(s.ProxyHandler(s.HandleGet))) mux.HandleFunc("GET /", CookieCartIdHandler(s.ProxyHandler(s.HandleGet)))
mux.HandleFunc("GET /add/{sku}", CookieCartIdHandler(s.ProxyHandler(s.HandleAddSku))) mux.HandleFunc("GET /add/{sku}", CookieCartIdHandler(s.ProxyHandler(s.HandleAddSku)))
mux.HandleFunc("POST /add", CookieCartIdHandler(s.ProxyHandler(s.HandleAddMultiple)))
mux.HandleFunc("POST /", CookieCartIdHandler(s.ProxyHandler(s.HandleAddRequest))) mux.HandleFunc("POST /", CookieCartIdHandler(s.ProxyHandler(s.HandleAddRequest)))
mux.HandleFunc("POST /set", CookieCartIdHandler(s.ProxyHandler(s.HandleSetCartItems))) mux.HandleFunc("POST /set", CookieCartIdHandler(s.ProxyHandler(s.HandleSetCartItems)))
mux.HandleFunc("DELETE /{itemId}", CookieCartIdHandler(s.ProxyHandler(s.HandleDeleteItem))) mux.HandleFunc("DELETE /{itemId}", CookieCartIdHandler(s.ProxyHandler(s.HandleDeleteItem)))