Skip to content

Commit

Permalink
add discount values
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean committed Aug 14, 2024
1 parent 8345f21 commit 2741f5f
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 150 deletions.
4 changes: 2 additions & 2 deletions release/obd-kardinal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ spec:
spec:
containers:
- name: server
image: kurtosistech/frontend:main
image: kurtosistech/frontend:demo-on-sale
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
Expand Down Expand Up @@ -286,7 +286,7 @@ spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: kurtosistech/productcatalogservice:main
image: kurtosistech/productcatalogservice:demo-on-sale
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8070
Expand Down
70 changes: 59 additions & 11 deletions src/frontend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) {
cart := cartResponse.JSON200

type productView struct {
Item productcatalogservice_rest_types.Product
Price *productcatalogservice_rest_types.Money
Item productcatalogservice_rest_types.Product
Price *productcatalogservice_rest_types.Money
Discount *productcatalogservice_rest_types.Money
FinalPrice *productcatalogservice_rest_types.Money
}

products := *productsList
Expand All @@ -91,7 +93,14 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) {
renderHTTPError(r, w, errors.Wrapf(err, "could not convert currency for product #%s", *p.Id), http.StatusInternalServerError)
return
}
newPV := productView{p, price}

if p.Discount == nil {
renderHTTPError(r, w, errors.Errorf("could not calculate discount for product #%s", *p.Id), http.StatusInternalServerError)
return
}
discount := money.Must(money.MultiplyFloat(price, float64(*p.Discount)))
finalPrice := money.Must(money.Sum(price, money.Negate(discount)))
newPV := productView{p, price, discount, finalPrice}
ps[i] = newPV
}

Expand Down Expand Up @@ -150,10 +159,19 @@ func (fe *frontendServer) productHandler(w http.ResponseWriter, r *http.Request)
return
}

if p.Discount == nil {
renderHTTPError(r, w, errors.Errorf("Could not calculate discount for product #%s", *p.Id), http.StatusInternalServerError)
return
}
discount := money.Must(money.MultiplyFloat(price, float64(*p.Discount)))
finalPrice := money.Must(money.Sum(price, money.Negate(discount)))

product := struct {
Item productcatalogservice_rest_types.Product
Price *productcatalogservice_rest_types.Money
}{*p, price}
Item productcatalogservice_rest_types.Product
Price *productcatalogservice_rest_types.Money
Discount *productcatalogservice_rest_types.Money
FinalPrice *productcatalogservice_rest_types.Money
}{*p, price, discount, finalPrice}

if err := templates.ExecuteTemplate(w, "product", map[string]interface{}{
"session_id": sessionID(r),
Expand Down Expand Up @@ -247,7 +265,10 @@ func (fe *frontendServer) viewCartHandler(w http.ResponseWriter, r *http.Request
Quantity int32
IsAPresent bool
Price *productcatalogservice_rest_types.Money
FinalPrice *productcatalogservice_rest_types.Money
Discount *productcatalogservice_rest_types.Money
}

items := make([]cartItemView, len(*cart.Items))
currentCurrencyObj := currentCurrency(r)
zeroNanos := int32(0)
Expand All @@ -257,6 +278,11 @@ func (fe *frontendServer) viewCartHandler(w http.ResponseWriter, r *http.Request
Nanos: &zeroNanos,
Units: &zeroUnits,
}
totalDiscount := &productcatalogservice_rest_types.Money{
CurrencyCode: &currentCurrencyObj,
Nanos: &zeroNanos,
Units: &zeroUnits,
}

cartItems := *cart.Items

Expand All @@ -272,21 +298,41 @@ func (fe *frontendServer) viewCartHandler(w http.ResponseWriter, r *http.Request
renderHTTPError(r, w, errors.Wrapf(err, "could not convert currency for product #%s", *item.ProductId), http.StatusInternalServerError)
return
}

logrus.Debugf("Price is %+v", price)

multPrice := money.MultiplySlow(price, uint32(*item.Quantity))

if p.Discount == nil {
renderHTTPError(r, w, errors.Errorf("Could not calculate discount for product #%s", *item.ProductId), http.StatusInternalServerError)
return
}

multDiscount, err := money.MultiplyFloat(price, float64(*p.Discount))
if err != nil {
renderHTTPError(r, w, errors.Wrapf(err, "could not calculate discount for product #%s", *item.ProductId), http.StatusInternalServerError)
return
}
finalPrice := money.Must(money.Sum(multPrice, money.Negate(multDiscount)))

prod := *p
quan := *item.Quantity

items[i] = cartItemView{
Item: prod,
Quantity: quan,
Price: multPrice,
Item: prod,
Quantity: quan,
Price: multPrice,
FinalPrice: finalPrice,
Discount: multDiscount,
}

totalPrice = money.Must(money.Sum(totalPrice, multPrice))
totalDiscount = money.Must(money.Sum(totalDiscount, multDiscount))
}

totalCost, err := money.Sum(totalPrice, money.Negate(totalDiscount))
if err != nil {
renderHTTPError(r, w, errors.Wrapf(err, "could not calculate discount for cart"), http.StatusInternalServerError)
return
}

year := time.Now().Year()
Expand All @@ -299,7 +345,9 @@ func (fe *frontendServer) viewCartHandler(w http.ResponseWriter, r *http.Request
"cart_size": cartSize(*cart.Items),
//"shipping_cost": shippingCost,
"show_currency": true,
"total_cost": totalPrice,
"total_cost": totalCost,
"total_value": totalPrice,
"total_discount": totalDiscount,
"items": items,
"expiration_years": []int{year, year + 1, year + 2, year + 3, year + 4},
"platform_css": plat.css,
Expand Down
37 changes: 34 additions & 3 deletions src/frontend/money/money.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package money

import (
"errors"

productcatalogservice_rest_types "github.com/kurtosis-tech/new-obd/src/productcatalogservice/api/http_rest/types"
)

Expand All @@ -32,7 +33,6 @@ var (

// IsValid checks if specified value has a valid units/nanos signs and ranges.
func IsValid(m *productcatalogservice_rest_types.Money) bool {

nanos := *m.Nanos
return signMatches(m) && validNanos(nanos)
}
Expand Down Expand Up @@ -84,7 +84,8 @@ func Negate(m *productcatalogservice_rest_types.Money) *productcatalogservice_re
return &productcatalogservice_rest_types.Money{
Units: &negativeUnits,
Nanos: &negativeNanos,
CurrencyCode: m.CurrencyCode}
CurrencyCode: m.CurrencyCode,
}
}

// Must panics if the given error is not nil. This can be used with other
Expand Down Expand Up @@ -126,7 +127,8 @@ func Sum(l, r *productcatalogservice_rest_types.Money) (*productcatalogservice_r
return &productcatalogservice_rest_types.Money{
Units: &units,
Nanos: &nanos,
CurrencyCode: l.CurrencyCode}, nil
CurrencyCode: l.CurrencyCode,
}, nil
}

// MultiplySlow is a slow multiplication operation done through adding the value
Expand All @@ -139,3 +141,32 @@ func MultiplySlow(m *productcatalogservice_rest_types.Money, n uint32) *productc
}
return out
}

// MultiplyFloat multiplies the Money value by a floating-point factor.
// It returns an error if the value is invalid or the result is invalid.
func MultiplyFloat(m *productcatalogservice_rest_types.Money, factor float64) (*productcatalogservice_rest_types.Money, error) {
if !IsValid(m) {
return &productcatalogservice_rest_types.Money{}, ErrInvalidValue
}

// Convert Money to total nanoseconds
totalNanos := int64(*m.Units)*nanosMod + int64(*m.Nanos)

// Perform the multiplication
resultNanos := int64(float64(totalNanos) * factor)

// Convert back to units and nanos
units := resultNanos / nanosMod
nanos := int32(resultNanos % nanosMod)

// Ensure nanos is within valid range
if nanos < nanosMin || nanos > nanosMax {
return &productcatalogservice_rest_types.Money{}, ErrInvalidValue
}

return &productcatalogservice_rest_types.Money{
Units: &units,
Nanos: &nanos,
CurrencyCode: m.CurrencyCode,
}, nil
}
20 changes: 19 additions & 1 deletion src/frontend/static/styles/cart.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

.cart-summary-item-row,
.cart-summary-shipping-row,
.cart-summary-total-value-row,
.cart-summary-total-discount-row,
.cart-summary-total-row {
padding-bottom: 24px;
padding-top: 24px;
Expand Down Expand Up @@ -90,6 +92,11 @@
}
}

.cart-item-original-price {
font-size: 14px;
text-decoration: line-through;
}

/* Item cost (price). */
.cart-summary-item-row .row:last-child strong {
font-weight: 500;
Expand All @@ -99,6 +106,17 @@
font-size: 28px;
}

.cart-summary-total-value-row {
font-size: 28px;
text-decoration: line-through;
}

.cart-summary-total-discount-row {
font-size: 28px;
color: red;
}


/* Cart Checkout Form */

.cart-checkout-form h3 {
Expand All @@ -112,4 +130,4 @@
/* "Place Order" button */
.cart-checkout-form .cymbal-button-primary {
margin-top: 36px;
}
}
10 changes: 10 additions & 0 deletions src/frontend/static/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ main {
margin-bottom: 8px;
}

.hot-product-card-original-price {
font-size: 14px;
text-decoration: line-through;
}

.hot-product-card-price {
font-size: 14px;
}
Expand Down Expand Up @@ -419,6 +424,11 @@ similar to that of the hot-products-row.
font-size: 28px;
}

.h-product .product-original-price {
font-size: 28px;
text-decoration: line-through;
}

.h-product .product-info .product-wrapper {
margin-left: 15px;
}
Expand Down
15 changes: 14 additions & 1 deletion src/frontend/templates/cart.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ <h4>{{ .Item.Name }}</h4>
</div>
{{end}}
<div class="col pr-md-0 text-right">
<strong>
<div class="cart-item-original-price col pr-md-0 text-right">
{{ renderMoney .Price }}
</div>
<strong>
{{ renderMoney .FinalPrice }}
</strong>
</div>
</div>
Expand All @@ -95,6 +98,16 @@ <h4>{{ .Item.Name }}</h4>
<div class="col pr-md-0 text-right">{{ renderMoney .shipping_cost }}</div>
</div>

<div class="row cart-summary-total-value-row">
<div class="col pl-md-0">Total Value</div>
<div class="col pr-md-0 text-right">{{ renderMoney .total_value }}</div>
</div>

<div class="row cart-summary-total-discount-row">
<div class="col pl-md-0">Total Discount</div>
<div class="col pr-md-0 text-right">{{ renderMoney .total_discount }}</div>
</div>

<div class="row cart-summary-total-row">
<div class="col pl-md-0">Total</div>
<div class="col pr-md-0 text-right">{{ renderMoney .total_cost }}</div>
Expand Down
5 changes: 3 additions & 2 deletions src/frontend/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ <h3>Hot Products</h3>
</a>
<div>
<div class="hot-product-card-name">{{ .Item.Name }}</div>
<div class="hot-product-card-price">{{ renderMoney .Price }}</div>
<div class="hot-product-card-original-price">{{ renderMoney .Price }}</div>
<div class="hot-product-card-price">{{ renderMoney .FinalPrice }}</div>
</div>
</div>
{{ end }}
Expand All @@ -75,4 +76,4 @@ <h3>Hot Products</h3>
{{ template "footer" . }}
</div>

{{ end }}
{{ end }}
5 changes: 3 additions & 2 deletions src/frontend/templates/product.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
<div class="product-wrapper">

<h2>{{ $.product.Item.Name }}</h2>
<p class="product-price">{{ renderMoney $.product.Price }}</p>
<p class="product-original-price">{{ renderMoney $.product.Price }}</p>
<p class="product-price">{{ renderMoney $.product.FinalPrice }}</p>
<p>{{ $.product.Item.Description }}</p>

<form method="POST" action="/cart">
Expand Down Expand Up @@ -71,4 +72,4 @@ <h2>{{ $.product.Item.Name }}</h2>
</div>
</main>
{{ template "footer" . }}
{{ end }}
{{ end }}
28 changes: 14 additions & 14 deletions src/productcatalogservice/api/http_rest/server/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2741f5f

Please sign in to comment.