checkoutservice: money proto migration

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2018-06-27 14:27:17 -07:00 committed by Simon Zeltser
parent b5b6d255ba
commit 33fae5d1d1
4 changed files with 52 additions and 36 deletions

View file

@ -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"]

View file

@ -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 {

View file

@ -3,7 +3,7 @@ package money
import (
"errors"
pb "../genproto"
pb "checkoutservice/genproto"
)
const (

View file

@ -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} }