more cleanup
This commit is contained in:
@@ -574,7 +574,7 @@ func (s *PoolServer) RemoveLineItemMarkingHandler(w http.ResponseWriter, r *http
|
|||||||
return s.WriteResult(w, reply)
|
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 {
|
if r.Method != http.MethodPost {
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
return nil
|
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}/user", CartIdHandler(s.ProxyHandler(s.SetUserIdHandler)))
|
||||||
handleFunc("PUT /cart/byid/{id}/item/{itemId}/marking", CartIdHandler(s.ProxyHandler(s.LineItemMarkingHandler)))
|
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("DELETE /cart/byid/{id}/item/{itemId}/marking", CartIdHandler(s.ProxyHandler(s.RemoveLineItemMarkingHandler)))
|
||||||
|
|
||||||
handleFunc("POST /internal/cart/{id}/mutation", CartIdHandler(s.InternalApplyMutationHandler))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.k6n.net/go-cart-actor/pkg/cart"
|
"git.k6n.net/go-cart-actor/pkg/cart"
|
||||||
"github.com/gogo/protobuf/proto"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CartClient struct {
|
type CartClient struct {
|
||||||
@@ -22,24 +22,44 @@ func NewCartClient(baseUrl string) *CartClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CartClient) ApplyMutation(cartId cart.CartId, mutation proto.Message) error {
|
// func (c *CartClient) ApplyMutation(cartId cart.CartId, mutation proto.Message) error {
|
||||||
url := fmt.Sprintf("%s/internal/cart/%s/mutation", c.baseUrl, cartId.String())
|
// url := fmt.Sprintf("%s/internal/cart/%s/mutation", c.baseUrl, cartId.String())
|
||||||
data, err := proto.Marshal(mutation)
|
// 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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/protobuf")
|
|
||||||
resp, err := c.httpClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var id checkout.CheckoutId
|
var id checkout.CheckoutId
|
||||||
|
|||||||
Reference in New Issue
Block a user