-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
11 changed files
with
170 additions
and
98 deletions.
There are no files selected for viewing
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
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
106 changes: 76 additions & 30 deletions
106
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,57 +1,103 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.product.ProductRepository; | ||
import com.kampus.kbazaar.promotion.PromotionRepository; | ||
import com.kampus.kbazaar.promotion.PromotionResponse; | ||
import com.kampus.kbazaar.promotion.PromotionService; | ||
import com.kampus.kbazaar.shopper.ShopperRepository; | ||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CartService { | ||
private final PromotionRepository promotionRepository; | ||
private final PromotionService promotionService; | ||
private final ShopperRepository ShopperRepository; | ||
private final ProductRepository productRepository; | ||
private final CartRepository cartRepository; | ||
|
||
public CartService( | ||
PromotionRepository promotionRepository, | ||
PromotionService promotionService, | ||
ShopperRepository ShopperRepository) { | ||
ShopperRepository ShopperRepository, | ||
ProductRepository productRepository, | ||
CartRepository cartRepository) { | ||
this.promotionRepository = promotionRepository; | ||
this.promotionService = promotionService; | ||
this.ShopperRepository = ShopperRepository; | ||
this.productRepository = productRepository; | ||
this.cartRepository = cartRepository; | ||
} | ||
|
||
// Story 5 : Add promotion to all products | ||
public CartResponse addPromotionAll(String username, PromotionAllRequest promotion) { | ||
|
||
if (ShopperRepository.findByUsername(username).isEmpty()) { | ||
throw new RuntimeException("Shopper not found"); | ||
} | ||
|
||
if (promotion.getCode().equals("FIXEDAMOUNT10")) { | ||
PromotionResponse newPromotion = | ||
promotionService.getPromotionByCode(promotion.getCode()); | ||
// if (isDateBetween(newPromotion.startDate(), | ||
// newPromotion.endDate(), | ||
// LocalDateTime.now())) | ||
// { | ||
return new CartResponse(username, null, 0, 0); | ||
} else { | ||
throw new RuntimeException("Promotion not found"); | ||
} | ||
} | ||
// Story 4 : Add specific product to my cart | ||
public CartResponse addItemToCart(String productSku, int quantity, String username) { | ||
|
||
// Optional<Product> product = productRepository.findBySku(productSku); | ||
|
||
// public boolean isDateBetween( | ||
// LocalDateTime startDate, LocalDateTime endDate, LocalDateTime dateToCheck) { | ||
// return !dateToCheck.isBefore(startDate) && !dateToCheck.isAfter(endDate); | ||
// } | ||
// if (product.isEmpty()) { | ||
// throw new NotFoundException("Product not found"); | ||
// } | ||
|
||
public void createCartPromotions30UpTo200() { | ||
// List<Cart> carts = cartRepository.findByUserId(1L); | ||
// if (cartRepository.findByUserId(1L).isEmpty()) | ||
// throw new NotFoundException("Cart not found"); | ||
// 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")); | ||
|
||
BigDecimal TotalPrice = new BigDecimal(0); | ||
return new CartResponse(username, List.of(mockItem), 0, 0); | ||
} | ||
|
||
// if (cart.isEmpty()) { | ||
// // Create a new cart | ||
// Cart newCart = new Cart(1, 1, username, product.get().getId()); | ||
// cartRepository.save(newCart); | ||
// } | ||
// else { | ||
// // Add the product to the cart | ||
// Cart existingCart = cart.get(); | ||
// Product[] products = existingCart.getProducts(); | ||
// Product[] newProducts = new Product[products.length + 1]; | ||
// for (int i = 0; i < products.length; i++) { | ||
// newProducts[i] = products[i]; | ||
// } | ||
// newProducts[products.length] = product.get(); | ||
// existingCart.setProducts(newProducts); | ||
// cartRepository.save(existingCart); | ||
// } | ||
// | ||
// return new Cart(username, new Product[]{product.get()}); | ||
} | ||
// } | ||
|
||
// Story 5 : Add promotion to all products | ||
// public CartResponse addPromotionAll(String username, PromotionAllRequest promotion) { | ||
// | ||
// if (ShopperRepository.findByUsername(username).isEmpty()) { | ||
// throw new RuntimeException("Shopper not found"); | ||
// } | ||
// | ||
// if (promotion.getCode().equals("FIXEDAMOUNT10")) { | ||
// PromotionResponse newPromotion = | ||
// promotionService.getPromotionByCode(promotion.getCode()); | ||
// return new CartResponse(username, null, 0, 0); | ||
// } else { | ||
// throw new RuntimeException("Promotion not found"); | ||
// } | ||
// } | ||
// | ||
// | ||
// | ||
// public void createCartPromotions30UpTo200() { | ||
// // List<Cart> carts = cartRepository.findByUserId(1L); | ||
// // if (cartRepository.findByUserId(1L).isEmpty()) | ||
// // throw new NotFoundException("Cart not found"); | ||
// | ||
// BigDecimal TotalPrice = new BigDecimal(0); | ||
// } |
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,10 +1,18 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Setter | ||
public class Item { | ||
private String sku; | ||
private String name; | ||
private Integer quantity; | ||
private Integer price; | ||
private Integer discount; | ||
private Integer finalPrice; | ||
private BigDecimal price; | ||
private BigDecimal discount; | ||
private BigDecimal finalPrice; | ||
} |
19 changes: 19 additions & 0 deletions
19
kbazaar/src/main/java/com/kampus/kbazaar/cart/ItemRequest.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,19 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
public class ItemRequest { | ||
private String sku; | ||
private Integer quantity; | ||
|
||
public ItemRequest(String sku, Integer quantity) { | ||
this.sku = sku; | ||
this.quantity = quantity; | ||
} | ||
|
||
public String getSku() { | ||
return sku; | ||
} | ||
|
||
public Integer getQuantity() { | ||
return quantity; | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
kbazaar/src/main/java/com/kampus/kbazaar/product/ProductRepository.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,12 +1,11 @@ | ||
package com.kampus.kbazaar.product; | ||
|
||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ProductRepository extends JpaRepository<Product, Long> { | ||
|
||
Optional<Product> findBySku(String sku); | ||
Product findBySku(String sku); | ||
// @Query("SELECT p FROM Product p WHERE p.name = ?1") | ||
} |
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
Oops, something went wrong.