Skip to content

Commit b918fa3

Browse files
committed
Add minor logic improvements
1 parent 03fd3ae commit b918fa3

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

frontend/service.bal

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,15 @@ service / on new http:Listener(9098) {
226226
units: 0
227227
};
228228
CartItemView[] cartItems = [];
229-
foreach stubs:CartItem item in cart.items {
230-
stubs:Product product = check getProduct(item.product_id);
231-
229+
foreach stubs:CartItem {product_id, quantity} in cart.items {
230+
stubs:Product product = check getProduct(product_id);
232231
stubs:Money convertedPrice = check convertCurrency(product.price_usd, currencyCookie.value);
233-
234-
stubs:Money price = money:multiplySlow(convertedPrice, item.quantity);
232+
stubs:Money price = money:multiplySlow(convertedPrice, quantity);
235233
string renderedPrice = money:renderMoney(price);
236234
cartItems.push({
237235
product,
238236
price: renderedPrice,
239-
quantity: item.quantity
237+
quantity: quantity
240238
});
241239
totalPrice = money:sum(totalPrice, price);
242240
}
@@ -261,7 +259,7 @@ service / on new http:Listener(9098) {
261259

262260
# POST method to update the cart with a product.
263261
#
264-
# + request - `AddToCartRequest` containing the product id of the product to add
262+
# + request - `AddToCartRequest` containing the product id of the product to be added
265263
# + cookieHeader - header containing the cookie
266264
# + return - `http:Created` if successful or `http:Unauthorized` or `http:BadRequest` or `error` if an error occurs
267265
resource function post cart(@http:Payload AddToCartRequest request,
@@ -313,7 +311,7 @@ service / on new http:Listener(9098) {
313311

314312
# Post method to checkout the user's cart.
315313
#
316-
# + request - `CheckoutRequest` containing user's details
314+
# + request - `CheckoutRequest` containing user details
317315
# + cookieHeader - header containing the cookie
318316
# + return - `CheckoutResponse` if successful or an `http:Unauthorized` or `error` if an error occurs
319317
resource function post cart/checkout(@http:Payload CheckoutRequest request,
@@ -349,8 +347,8 @@ service / on new http:Listener(9098) {
349347

350348
stubs:Product[] recommendations = check getRecommendations(userId);
351349
stubs:Money totalCost = orderResult.shipping_cost;
352-
foreach stubs:OrderItem item in orderResult.items {
353-
stubs:Money multiplyRes = money:multiplySlow(item.cost, item.item.quantity);
350+
foreach stubs:OrderItem {item, cost} in orderResult.items {
351+
stubs:Money multiplyRes = money:multiplySlow(cost, item.quantity);
354352
totalCost = money:sum(totalCost, multiplyRes);
355353
}
356354

@@ -369,7 +367,7 @@ service / on new http:Listener(9098) {
369367
}
370368

371369
function getProductIdFromCart(stubs:Cart cart) returns string[] {
372-
return from stubs:CartItem item in cart.items
373-
select item.product_id;
370+
return from stubs:CartItem {product_id} in cart.items
371+
select product_id;
374372
}
375373
}

frontend/utils.bal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ isolated function parseCookieHeader(string cookieStringValue) returns http:Cooki
5151

5252
isolated function getCartSize(stubs:Cart cart) returns int {
5353
int cartSize = 0;
54-
foreach stubs:CartItem item in cart.items {
55-
cartSize += item.quantity;
54+
foreach stubs:CartItem {quantity} in cart.items {
55+
cartSize += quantity;
5656
}
5757
return cartSize;
5858
}

0 commit comments

Comments
 (0)