-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bank
authored and
Bank
committed
Mar 24, 2024
1 parent
c7b4729
commit f54f417
Showing
10 changed files
with
243 additions
and
54 deletions.
There are no files selected for viewing
3 changes: 0 additions & 3 deletions
3
kbazaar/src/main/java/com/kampus/kbazaar/cart/AddProductResponse.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 37 additions & 4 deletions
41
kbazaar/src/main/java/com/kampus/kbazaar/cart/CartProduct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,60 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.product.Product; | ||
import com.kampus.kbazaar.promotion.Promotion; | ||
import com.kampus.kbazaar.promotion.PromotionDiscount; | ||
import jakarta.persistence.*; | ||
import java.math.BigDecimal; | ||
import java.util.Optional; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class CartProduct { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Column(name = "product_id") | ||
private Long productId; | ||
@ManyToOne | ||
@JoinColumn(name = "product_id") | ||
private Product product; | ||
|
||
@Column(name = "quantity") | ||
private Integer quantity; | ||
@ManyToOne | ||
@JoinColumn(name = "promotion_id") | ||
private Promotion promotion; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "cart_id") | ||
private Cart cart; | ||
|
||
@Column(name = "quantity") | ||
private Integer quantity; | ||
|
||
public BigDecimal getDiscountAmount() { | ||
return PromotionDiscount.getDiscountAmountFromPromotion( | ||
product.getPrice(), quantity, promotion); | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return product.getPrice().multiply(BigDecimal.valueOf(quantity)); | ||
} | ||
|
||
public BigDecimal getFinalPrice() { | ||
return this.getPrice().subtract(this.getDiscountAmount()); | ||
} | ||
|
||
public static Optional<CartProduct> of(Product product, Integer quantity, Cart cart) { | ||
if (product.getQuantity() < quantity) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of( | ||
CartProduct.builder().product(product).quantity(quantity).cart(cart).build()); | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
kbazaar/src/main/java/com/kampus/kbazaar/cart/CartRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.shopper.Shopper; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CartRepository extends JpaRepository<Cart, Long> {} | ||
public interface CartRepository extends JpaRepository<Cart, Long> { | ||
Cart findByShopper(Shopper shopper); | ||
} |
56 changes: 56 additions & 0 deletions
56
kbazaar/src/main/java/com/kampus/kbazaar/cart/CartResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.promotion.Promotion; | ||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Data | ||
public class CartResponse { | ||
private Long cartId; | ||
private List<Item> items; | ||
private List<String> promotionCodes; | ||
private BigDecimal totalCost; | ||
private BigDecimal entireCartPromotionDiscount; | ||
private BigDecimal finalTotalCost; | ||
|
||
public static CartResponse fromCart(Cart cart) { | ||
List<Item> items = cart.getCartProducts().stream().map(CartResponse::initItem).toList(); | ||
|
||
return CartResponse.builder() | ||
.cartId(cart.getId()) | ||
.items(items) | ||
.promotionCodes(cart.getPromotions().stream().map(Promotion::getCode).toList()) | ||
.totalCost(cart.getTotalCost()) | ||
.entireCartPromotionDiscount(cart.getEntireCartDiscountAmount()) | ||
.finalTotalCost(cart.getFinalTotalCost()) | ||
.build(); | ||
} | ||
|
||
private static Item initItem(CartProduct cartProduct) { | ||
return new Item( | ||
cartProduct.getId(), | ||
cartProduct.getProduct().getName(), | ||
cartProduct.getQuantity(), | ||
cartProduct.getPrice(), | ||
cartProduct.getDiscountAmount(), | ||
cartProduct.getFinalPrice(), | ||
Optional.ofNullable(cartProduct.getPromotion()).map(Promotion::getCode).orElse("")); | ||
} | ||
} | ||
|
||
record Item( | ||
Long id, | ||
String name, | ||
Integer quantity, | ||
BigDecimal price, | ||
BigDecimal discount, | ||
BigDecimal finalPrice, | ||
String promotionCode) {} |
27 changes: 26 additions & 1 deletion
27
kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,39 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.exceptions.InternalServerException; | ||
import com.kampus.kbazaar.exceptions.NotFoundException; | ||
import com.kampus.kbazaar.product.Product; | ||
import com.kampus.kbazaar.product.ProductService; | ||
import com.kampus.kbazaar.shopper.ShopperService; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CartService { | ||
private final ShopperService shopperService; | ||
private final ProductService productService; | ||
private final CartRepository cartRepository; | ||
|
||
public Cart findCartByUsername(String username) { | ||
return null; | ||
return Optional.of(username) | ||
.map(shopperService::getShopperByUsername) | ||
.map(cartRepository::findByShopper) | ||
.orElseThrow(() -> new NotFoundException("Failed to find cart by username")); | ||
} | ||
|
||
public Cart addProductByUsernameAndProductSku( | ||
String username, String productSku, Integer quantity) { | ||
Product product = productService.getProductBySku(productSku); | ||
Cart cart = findCartByUsername(username); | ||
// TODO: add deduct product stock logic here | ||
return CartProduct.of(product, quantity, cart) | ||
.map(cart::addProduct) | ||
.map(cartRepository::save) | ||
.orElseThrow( | ||
() -> | ||
new InternalServerException( | ||
"Product out of stock or failed to add product to cart")); | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
kbazaar/src/main/java/com/kampus/kbazaar/cart/CartTemp.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
kbazaar/src/main/java/com/kampus/kbazaar/promotion/PromotionDiscount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.kampus.kbazaar.promotion; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
public class PromotionDiscount { | ||
public static BigDecimal getDiscountAmountFromPromotion( | ||
BigDecimal price, Integer quantity, Promotion promotion) { | ||
if (promotion == null) { | ||
return BigDecimal.ZERO; | ||
} | ||
return switch (promotion.getDiscountType()) { | ||
case "FIXED_AMOUNT" -> promotion.getDiscountAmount(); | ||
case "PERCENTAGE" -> price.subtract( | ||
price.multiply(promotion.getDiscountAmount()) | ||
.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP)); | ||
case "buy1_get1" -> { | ||
if (quantity >= 2) { | ||
yield price; | ||
} | ||
yield BigDecimal.ZERO; | ||
} | ||
case "buy2_get1" -> { | ||
if (quantity >= 3) { | ||
yield price; | ||
} | ||
yield BigDecimal.ZERO; | ||
} | ||
default -> BigDecimal.ZERO; | ||
}; | ||
} | ||
|
||
public static BigDecimal divideDiscount(BigDecimal price, Integer divider) { | ||
return price.divide(BigDecimal.valueOf(divider), 2, RoundingMode.HALF_UP); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters