From 33fae5d1d150d918e28897e28b8eacb405167a76 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Wed, 27 Jun 2018 14:27:17 -0700 Subject: [PATCH] checkoutservice: money proto migration Signed-off-by: Ahmet Alp Balkan --- src/checkoutservice/Dockerfile | 11 ++-- src/checkoutservice/main.go | 73 +++++++++++++++---------- src/checkoutservice/money/money.go | 2 +- src/checkoutservice/money/money_test.go | 2 +- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/checkoutservice/Dockerfile b/src/checkoutservice/Dockerfile index d4d2f1c..c69a206 100644 --- a/src/checkoutservice/Dockerfile +++ b/src/checkoutservice/Dockerfile @@ -1,13 +1,12 @@ FROM golang:1.10-alpine as builder RUN apk add --no-cache ca-certificates git -WORKDIR /src/microservices-demo/catalogservice +WORKDIR /go/src/checkoutservice COPY . . RUN go get -d ./... -RUN go build -o /catalogservice . +RUN go build -o /checkoutservice . FROM alpine as release -RUN apk add --no-cache \ - ca-certificates -COPY --from=builder /catalogservice /catalogservice +RUN apk add --no-cache ca-certificates +COPY --from=builder /checkoutservice /checkoutservice EXPOSE 5050 -ENTRYPOINT ["/catalogservice"] +ENTRYPOINT ["/checkoutservice"] diff --git a/src/checkoutservice/main.go b/src/checkoutservice/main.go index 268fc99..cea99d8 100644 --- a/src/checkoutservice/main.go +++ b/src/checkoutservice/main.go @@ -12,8 +12,8 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - pb "./genproto" - money "./money" + pb "checkoutservice/genproto" + money "checkoutservice/money" ) const ( @@ -68,13 +68,13 @@ func (cs *checkoutService) CreateOrder(ctx context.Context, req *pb.CreateOrderR log.Printf("[CreateOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency) resp := new(pb.CreateOrderResponse) - shippingQuoteUSD, err := cs.quoteShipping(ctx, req.Address, nil) // TODO(ahmetb): query CartService for items + prep, err := cs.prepareOrderItemsAndShippingQuoteFromCart(ctx, req.UserId, req.UserCurrency, req.Address) if err != nil { - return nil, status.Errorf(codes.Internal, "shipping quote failure: %+v", err) + return nil, status.Errorf(codes.Internal, err.Error()) } - resp.ShippingCost = shippingQuoteUSD - // TODO(ahmetb) convert to req.UserCurrency - // TODO(ahmetb) calculate resp.OrderItem with req.UserCurrency + + resp.Items = prep.orderItems + resp.ShippingCost = prep.shippingCostLocalized return resp, nil } @@ -86,30 +86,16 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq return nil, status.Errorf(codes.Internal, "failed to generate order uuid") } - cartItems, err := cs.getUserCart(ctx, req.UserId) + prep, err := cs.prepareOrderItemsAndShippingQuoteFromCart(ctx, req.UserId, req.UserCurrency, req.Address) if err != nil { - return nil, status.Errorf(codes.Internal, "cart failure: %+v", err) - } - - orderItems, err := cs.prepOrderItems(ctx, cartItems, req.UserCurrency) - if err != nil { - return nil, status.Errorf(codes.Internal, "failed to prepare order: %+v", err) - } - - shippingUsd, err := cs.quoteShipping(ctx, req.Address, cartItems) // TODO(ahmetb): query CartService for items - if err != nil { - return nil, status.Errorf(codes.Internal, "shipping quote failure: %+v", err) - } - shippingPrice, err := cs.convertCurrency(ctx, shippingUsd, req.UserCurrency) - if err != nil { - return nil, status.Errorf(codes.Internal, "failed to convert shipping cost to currency: %+v", err) + return nil, status.Errorf(codes.Internal, err.Error()) } total := pb.Money{CurrencyCode: req.UserCurrency, Units: 0, Nanos: 0} - total = money.Must(money.Sum(total, *shippingPrice)) - for _, it := range orderItems { + total = money.Must(money.Sum(total, *prep.shippingCostLocalized)) + for _, it := range prep.orderItems { total = money.Must(money.Sum(total, *it.Cost)) } @@ -119,7 +105,7 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq } log.Printf("payment went through (transaction_id: %s)", txID) - shippingTrackingID, err := cs.shipOrder(ctx, req.Address, cartItems) + shippingTrackingID, err := cs.shipOrder(ctx, req.Address, prep.cartItems) if err != nil { return nil, status.Errorf(codes.Unavailable, "shipping error: %+v", err) } @@ -127,9 +113,9 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq orderResult := &pb.OrderResult{ OrderId: orderID.String(), ShippingTrackingId: shippingTrackingID, - ShippingCost: shippingPrice, + ShippingCost: prep.shippingCostLocalized, ShippingAddress: req.Address, - Items: orderItems, + Items: prep.orderItems, } if err := cs.sendOrderConfirmation(ctx, req.Email, orderResult); err != nil { @@ -141,6 +127,37 @@ func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderReq return resp, nil } +type orderPrep struct { + orderItems []*pb.OrderItem + cartItems []*pb.CartItem + shippingCostLocalized *pb.Money +} + +func (cs *checkoutService) prepareOrderItemsAndShippingQuoteFromCart(ctx context.Context, userID, userCurrency string, address *pb.Address) (orderPrep, error) { + var out orderPrep + cartItems, err := cs.getUserCart(ctx, userID) + if err != nil { + return out, fmt.Errorf("cart failure: %+v", err) + } + orderItems, err := cs.prepOrderItems(ctx, cartItems, userCurrency) + if err != nil { + return out, fmt.Errorf("failed to prepare order: %+v", err) + } + shippingUSD, err := cs.quoteShipping(ctx, address, cartItems) + if err != nil { + return out, fmt.Errorf("shipping quote failure: %+v", err) + } + shippingPrice, err := cs.convertCurrency(ctx, shippingUSD, userCurrency) + if err != nil { + return out, fmt.Errorf("failed to convert shipping cost to currency: %+v", err) + } + + out.shippingCostLocalized = shippingPrice + out.cartItems = cartItems + out.orderItems = orderItems + return out, nil +} + func (cs *checkoutService) quoteShipping(ctx context.Context, address *pb.Address, items []*pb.CartItem) (*pb.Money, error) { conn, err := grpc.DialContext(ctx, cs.shippingSvcAddr, grpc.WithInsecure()) if err != nil { diff --git a/src/checkoutservice/money/money.go b/src/checkoutservice/money/money.go index 410daaf..97d8555 100644 --- a/src/checkoutservice/money/money.go +++ b/src/checkoutservice/money/money.go @@ -3,7 +3,7 @@ package money import ( "errors" - pb "../genproto" + pb "checkoutservice/genproto" ) const ( diff --git a/src/checkoutservice/money/money_test.go b/src/checkoutservice/money/money_test.go index d672129..deef207 100644 --- a/src/checkoutservice/money/money_test.go +++ b/src/checkoutservice/money/money_test.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - pb "../genproto" + pb "checkoutservice/genproto" ) func mmc(u int64, n int32, c string) pb.Money { return pb.Money{Units: u, Nanos: n, CurrencyCode: c} }