From 5f6a7c47c933b0a780691ad8cffcaa6dba6acf79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mats=20T=C3=B6rnberg?= Date: Tue, 2 Dec 2025 21:13:28 +0100 Subject: [PATCH] more cleanup --- cmd/cart/pool-server.go | 4 +-- cmd/checkout/cart-client.go | 50 ++++++++++++++++++++++++++----------- cmd/checkout/pool-server.go | 20 --------------- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/cmd/cart/pool-server.go b/cmd/cart/pool-server.go index dd8a056..36639b7 100644 --- a/cmd/cart/pool-server.go +++ b/cmd/cart/pool-server.go @@ -574,7 +574,7 @@ func (s *PoolServer) RemoveLineItemMarkingHandler(w http.ResponseWriter, r *http return s.WriteResult(w, reply) } -func (s *PoolServer) InternalApplyMutationHandler(cartId cart.CartId, w http.ResponseWriter, r *http.Request) error { +func (s *PoolServer) InternalApplyMutationHandler(w http.ResponseWriter, r *http.Request, cartId cart.CartId) error { if r.Method != http.MethodPost { w.WriteHeader(http.StatusMethodNotAllowed) return nil @@ -668,6 +668,4 @@ func (s *PoolServer) Serve(mux *http.ServeMux) { handleFunc("PUT /cart/byid/{id}/user", CartIdHandler(s.ProxyHandler(s.SetUserIdHandler))) handleFunc("PUT /cart/byid/{id}/item/{itemId}/marking", CartIdHandler(s.ProxyHandler(s.LineItemMarkingHandler))) handleFunc("DELETE /cart/byid/{id}/item/{itemId}/marking", CartIdHandler(s.ProxyHandler(s.RemoveLineItemMarkingHandler))) - - handleFunc("POST /internal/cart/{id}/mutation", CartIdHandler(s.InternalApplyMutationHandler)) } diff --git a/cmd/checkout/cart-client.go b/cmd/checkout/cart-client.go index 9dbcecc..55f060a 100644 --- a/cmd/checkout/cart-client.go +++ b/cmd/checkout/cart-client.go @@ -1,13 +1,13 @@ package main import ( - "bytes" + "context" + "encoding/json" "fmt" "net/http" "time" "git.k6n.net/go-cart-actor/pkg/cart" - "github.com/gogo/protobuf/proto" ) type CartClient struct { @@ -22,24 +22,44 @@ func NewCartClient(baseUrl string) *CartClient { } } -func (c *CartClient) ApplyMutation(cartId cart.CartId, mutation proto.Message) error { - url := fmt.Sprintf("%s/internal/cart/%s/mutation", c.baseUrl, cartId.String()) - data, err := proto.Marshal(mutation) +// func (c *CartClient) ApplyMutation(cartId cart.CartId, mutation proto.Message) error { +// url := fmt.Sprintf("%s/internal/cart/%s/mutation", c.baseUrl, cartId.String()) +// data, err := proto.Marshal(mutation) +// if err != nil { +// return err +// } +// req, err := http.NewRequest("POST", url, bytes.NewReader(data)) +// if err != nil { +// return err +// } +// req.Header.Set("Content-Type", "application/protobuf") +// resp, err := c.httpClient.Do(req) +// if err != nil { +// return err +// } +// defer resp.Body.Close() +// if resp.StatusCode != http.StatusOK { +// return fmt.Errorf("cart mutation failed: %s", resp.Status) +// } +// return nil +// } + +func (s *CartClient) getCartGrain(ctx context.Context, cartId cart.CartId) (*cart.CartGrain, error) { + // Call cart service to get grain + url := fmt.Sprintf("%s/cart/byid/%d", s.baseUrl, cartId.String()) + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { - return err + return nil, err } - req, err := http.NewRequest("POST", url, bytes.NewReader(data)) + resp, err := s.httpClient.Do(req) if err != nil { - return err - } - req.Header.Set("Content-Type", "application/protobuf") - resp, err := c.httpClient.Do(req) - if err != nil { - return err + return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return fmt.Errorf("cart mutation failed: %s", resp.Status) + return nil, fmt.Errorf("failed to get cart: %s", resp.Status) } - return nil + var grain cart.CartGrain + err = json.NewDecoder(resp.Body).Decode(&grain) + return &grain, err } diff --git a/cmd/checkout/pool-server.go b/cmd/checkout/pool-server.go index 1be4a38..f93d159 100644 --- a/cmd/checkout/pool-server.go +++ b/cmd/checkout/pool-server.go @@ -156,26 +156,6 @@ func (s *CheckoutPoolServer) CheckoutHandler(fn func(order *CheckoutOrder, w htt })) } -func (s *CheckoutPoolServer) getCartGrain(ctx context.Context, cartId cart.CartId, version int) (*cart.CartGrain, error) { - // Call cart service to get grain - url := fmt.Sprintf("%s/internal/cart/%s/%d", s.cartClient.baseUrl, cartId.String(), version) - req, err := http.NewRequestWithContext(ctx, "GET", url, nil) - if err != nil { - return nil, err - } - resp, err := s.cartClient.httpClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("failed to get cart: %s", resp.Status) - } - var grain cart.CartGrain - err = json.NewDecoder(resp.Body).Decode(&grain) - return &grain, err -} - func CheckoutIdHandler(fn func(checkoutId checkout.CheckoutId, w http.ResponseWriter, r *http.Request) error) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { var id checkout.CheckoutId