Skip to content

Commit

Permalink
add shipping fee feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmPongPol committed Mar 24, 2024
1 parent 2e9e612 commit f9a3fa6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 24 deletions.
36 changes: 29 additions & 7 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@
public class CartResponse {
private String username;
private List<Item> items;
private Integer totalPrice;
private Integer totalDiscount;
private Double totalPrice;
private Double totalDiscount;
private Double fee;

public CartResponse(
String username, List<Item> items, Integer totalPrice, Integer totalDiscount) {
String username,
List<Item> items,
Double totalPrice,
Double totalDiscount,
Double fee) {
this.username = username;
this.items = items;
this.totalPrice = totalPrice;
this.totalDiscount = totalDiscount;
this.fee = fee;
}

public CartResponse(
String username, List<Item> items, Double totalPrice, Double totalDiscount) {
this.username = username;
this.items = items;
this.totalPrice = totalPrice;
Expand All @@ -32,19 +46,27 @@ public void setItems(List<Item> items) {
this.items = items;
}

public Integer getTotalPrice() {
public Double getTotalPrice() {
return totalPrice;
}

public void setTotalPrice(Integer totalPrice) {
public void setTotalPrice(Double totalPrice) {
this.totalPrice = totalPrice;
}

public Integer getTotalDiscount() {
public Double getTotalDiscount() {
return totalDiscount;
}

public void setTotalDiscount(Integer totalDiscount) {
public void setTotalDiscount(Double totalDiscount) {
this.totalDiscount = totalDiscount;
}

public Double getFee() {
return fee;
}

public void setFee(Double fee) {
this.fee = fee;
}
}
60 changes: 43 additions & 17 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -17,6 +18,9 @@ public class CartService {
private final ProductRepository productRepository;
private final CartRepository cartRepository;

@Value("${enabled.feature.shipping-fee.api}")
private boolean enableToggleShippingFee;

public CartService(
PromotionRepository promotionRepository,
PromotionService promotionService,
Expand All @@ -30,28 +34,50 @@ public CartService(
this.cartRepository = cartRepository;
}

// Story 4 : Add specific product to my cart
public CartResponse addItemToCart(String productSku, int quantity, String username) {

// Optional<Product> product = productRepository.findBySku(productSku);
Double fee = 0.0;

// if (product.isEmpty()) {
// throw new NotFoundException("Product not found");
// }
if (enableToggleShippingFee) {
fee = 25.0;
}

// Find the cart by username
Optional<Cart> cart = cartRepository.findByUsername(username);
// Product mockProduct = productRepository.findBySku(productSku);
Item mockItem =
new Item(
"MOBILE-APPLE-IPHONE-12-PRO",
"IPHONE12",
1,
new BigDecimal("10000.00"),
new BigDecimal("10.00"),
new BigDecimal("10.00"));

return new CartResponse(username, List.of(mockItem), 0, 0);

List<Item> mockItem =
List.of(
new Item(
"MOBILE-APPLE-IPHONE-12-PRO",
"IPHONE12",
1,
new BigDecimal("10000.00"),
new BigDecimal("10.00"),
new BigDecimal("9990.00")),
new Item(
"MOBILE-APPLE-IPHONE-12-PRO",
"IPHONE12",
1,
new BigDecimal("5000.00"),
new BigDecimal("10.00"),
new BigDecimal("4990.00")));

Double totalPrice = 0.0;
Double totalDiscount = 0.0;
Double totalFinalPrice = 0.0;

for (Item item : mockItem) {
totalPrice += item.getPrice().doubleValue();
totalDiscount += item.getDiscount().doubleValue();

totalFinalPrice += item.getFinalPrice().doubleValue();

System.out.println(totalFinalPrice);
}

System.out.println(totalPrice);
System.out.println(totalDiscount);

return new CartResponse(username, mockItem, totalPrice, totalDiscount, fee);
}

// if (cart.isEmpty()) {
Expand Down
1 change: 1 addition & 0 deletions kbazaar/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ springdoc.swagger-ui.enabled=true

# Feature Toggle
enabled.feature.promotion.list.api=true
enabled.feature.shipping-fee.api=true

0 comments on commit f9a3fa6

Please sign in to comment.