Skip to content

Commit

Permalink
Merge pull request #21 from kurtosis-tech/lporoli/fix-no-product-resp…
Browse files Browse the repository at this point in the history
…onse

fix: handle product catalog empty responses
  • Loading branch information
leoporoli authored Sep 5, 2024
2 parents fe1a679 + c71ee38 commit 8d378be
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 43 deletions.
41 changes: 26 additions & 15 deletions src/frontend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) {
Price *productcatalogservice_rest_types.Money
}

products := *productsList
products := []productcatalogservice_rest_types.Product{}
if productsList != nil {
products = *productsList
}

ps := make([]productView, len(products))
for i, p := range products {
Expand Down Expand Up @@ -128,7 +131,7 @@ func (fe *frontendServer) productHandler(w http.ResponseWriter, r *http.Request)
renderHTTPError(r, w, errors.Wrapf(err, "could not retrieve product #%s", id), http.StatusInternalServerError)
return
}
p := productResponse.JSON200
productFromCatalog := productResponse.JSON200

currencies, err := fe.currencyService.GetSupportedCurrencies(r.Context())
if err != nil {
Expand All @@ -144,32 +147,40 @@ func (fe *frontendServer) productHandler(w http.ResponseWriter, r *http.Request)

cart := cartResponse.JSON200

price, err := fe.currencyService.Convert(r.Context(), *p.PriceUsd.CurrencyCode, *p.PriceUsd.Units, *p.PriceUsd.Nanos, currentCurrency(r))
price, err := fe.currencyService.Convert(r.Context(), *productFromCatalog.PriceUsd.CurrencyCode, *productFromCatalog.PriceUsd.Units, *productFromCatalog.PriceUsd.Nanos, currentCurrency(r))
if err != nil {
renderHTTPError(r, w, errors.Wrapf(err, "could not convert currency for product #%s", *p.Id), http.StatusInternalServerError)
renderHTTPError(r, w, errors.Wrapf(err, "could not convert currency for product #%s", *productFromCatalog.Id), http.StatusInternalServerError)
return
}

product := struct {
var productInView struct {
Item productcatalogservice_rest_types.Product
Price *productcatalogservice_rest_types.Money
}{*p, price}
}
var isProduct bool

if productFromCatalog != nil {
productInView = struct {
Item productcatalogservice_rest_types.Product
Price *productcatalogservice_rest_types.Money
}{*productFromCatalog, price}
isProduct = true
}

if err := templates.ExecuteTemplate(w, "product", map[string]interface{}{
"session_id": sessionID(r),
"request_id": r.Context().Value(ctxKeyRequestID{}),
//"ad": fe.chooseAd(r.Context(), p.Categories, log),
"user_currency": currentCurrency(r),
"show_currency": true,
"currencies": currencies,
"product": product,
//"recommendations": recommendations,
"session_id": sessionID(r),
"request_id": r.Context().Value(ctxKeyRequestID{}),
"user_currency": currentCurrency(r),
"show_currency": true,
"currencies": currencies,
"product": productInView,
"cart_size": cartSize(*cart.Items),
"platform_css": plat.css,
"platform_name": plat.provider,
"is_cymbal_brand": isCymbalBrand,
"is_present_feature": false,
//"deploymentDetails": deploymentDetailsMap,
"is_product": isProduct,
"id": id,
}); err != nil {
log.Println(err)
}
Expand Down
60 changes: 33 additions & 27 deletions src/frontend/templates/product.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,46 @@
<main role="main">
<div class="h-product container">
<div class="row">
{{if $.is_product}}
<div class="col-md-6">
<img class="product-image" alt="" src="{{$.product.Item.Picture}}" />
</div>
<img class="product-image" alt="" src="{{$.product.Item.Picture}}" />
</div>
<div class="product-info col-md-5">
<div class="product-wrapper">
<div class="product-wrapper">

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

<form method="POST" action="/cart">
<input type="hidden" name="product_id" value="{{$.product.Item.Id}}" />
<div class="product-quantity-dropdown">
<select name="quantity" id="quantity">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>10</option>
</select>
<img src="/static/icons/Hipster_DownArrow.svg" alt="">
</div>
{{if $.is_present_feature}}
<div class="product-present-checkbox">
<img src="/static/images/present.png" alt="present icon">
<label for="present">Is a present?:</label>
<input type="checkbox" id="present" name="present">
<form method="POST" action="/cart">
<input type="hidden" name="product_id" value="{{$.product.Item.Id}}" />
<div class="product-quantity-dropdown">
<select name="quantity" id="quantity">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>10</option>
</select>
<img src="/static/icons/Hipster_DownArrow.svg" alt="">
</div>
{{end}}
<button type="submit" class="cymbal-button-primary">Add To Cart</button>
</form>
{{if $.is_present_feature}}
<div class="product-present-checkbox">
<img src="/static/images/present.png" alt="present icon">
<label for="present">Is a present?:</label>
<input type="checkbox" id="present" name="present">
</div>
{{end}}
<button type="submit" class="cymbal-button-primary">Add To Cart</button>
</form>
</div>
</div>
{{else}}
<div class="col-md-12">
<p >No product found for id {{$.id}}</p>
</div>
{{end}}
</div>
</div>
<div>
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ func KardinalTracingContextWrapper(next http.Handler) http.Handler {
traceID := r.Header.Get(consts.KardinalTraceIdHeaderKey)
logrus.Infof("[KARDINAL-DEBUG] Trace ID: %s", traceID)

next.ServeHTTP(w, r)
if next != nil && r != nil {
next.ServeHTTP(w, r)
}

})
}

0 comments on commit 8d378be

Please sign in to comment.