-
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
1 parent
b69aa71
commit bc07125
Showing
12 changed files
with
244 additions
and
18 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
10 changes: 10 additions & 0 deletions
10
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,10 +1,20 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.product.Product; | ||
import com.kampus.kbazaar.shopper.Shopper; | ||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CartRepository extends JpaRepository<Cart, Long> { | ||
Cart findByShopper(Shopper shopper); | ||
|
||
Optional<Cart> findByShopperId(Long shopperId); | ||
|
||
@Query("SELECT c.cartProducts FROM Cart c WHERE c.id = :cartId") | ||
ArrayList<Product> findProductsByCartId(@Param("cartId") Long cartId); | ||
} |
10 changes: 10 additions & 0 deletions
10
kbazaar/src/main/java/com/kampus/kbazaar/cart/CartRequestDto.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,10 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class CartRequestDto { | ||
@NotBlank(message = "is required.") | ||
private String code; | ||
} |
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
109 changes: 109 additions & 0 deletions
109
kbazaar/src/main/java/com/kampus/kbazaar/cart/OrderResponse.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,109 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.product.Product; | ||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
|
||
public class OrderResponse { | ||
private String username; | ||
private ArrayList<Product> items; | ||
private BigDecimal totalPrice; | ||
private BigDecimal totalDiscount; | ||
|
||
public OrderResponse() { | ||
this.items = new ArrayList<>(); | ||
} | ||
|
||
// Getters and setters | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public ArrayList<Product> getItems() { | ||
return items; | ||
} | ||
|
||
public void setItems(ArrayList<Product> items) { | ||
this.items = items; | ||
} | ||
|
||
public BigDecimal getTotalPrice() { | ||
return totalPrice; | ||
} | ||
|
||
public void setTotalPrice(BigDecimal totalPrice) { | ||
this.totalPrice = totalPrice; | ||
} | ||
|
||
public BigDecimal getTotalDiscount() { | ||
return totalDiscount; | ||
} | ||
|
||
public void setTotalDiscount(BigDecimal totalDiscount) { | ||
this.totalDiscount = totalDiscount; | ||
} | ||
|
||
public static class Item { | ||
private String sku; | ||
private String name; | ||
private int quantity; | ||
private BigDecimal price; | ||
private BigDecimal discount; | ||
private BigDecimal finalPrice; | ||
|
||
// Getters and setters | ||
|
||
public String getSku() { | ||
return sku; | ||
} | ||
|
||
public void setSku(String sku) { | ||
this.sku = sku; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public void setQuantity(int quantity) { | ||
this.quantity = quantity; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public BigDecimal getDiscount() { | ||
return discount; | ||
} | ||
|
||
public void setDiscount(BigDecimal discount) { | ||
this.discount = discount; | ||
} | ||
|
||
public BigDecimal getFinalPrice() { | ||
return finalPrice; | ||
} | ||
|
||
public void setFinalPrice(BigDecimal finalPrice) { | ||
this.finalPrice = finalPrice; | ||
} | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
kbazaar/src/test/java/com/kampus/kbazaar/cart/CartServiceTest.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,68 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.doReturn; | ||
|
||
import com.kampus.kbazaar.product.Product; | ||
import com.kampus.kbazaar.promotion.Promotion; | ||
import com.kampus.kbazaar.shopper.ShopperService; | ||
import java.math.BigDecimal; | ||
import java.time.LocalDateTime; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.*; | ||
|
||
public class CartServiceTest { | ||
|
||
@Mock private CartRepository cartRepository; | ||
|
||
@Spy @InjectMocks private CartService underTest; | ||
|
||
@Mock private ShopperService shopperService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
@DisplayName("should be able to apply discount") | ||
void shouldBeAbleToApplyDiscount() { | ||
String username = "TechNinja"; | ||
LocalDateTime ldt = LocalDateTime.now(); | ||
|
||
Promotion promotion = new Promotion(); | ||
promotion.setPromotionId(1L); | ||
promotion.setCode("FIXEDAMOUNT10"); | ||
promotion.setName("Fixed Amount $10 Off Entire Cart"); | ||
promotion.setDescription("Get $10 off on your entire cart purchase."); | ||
promotion.setStartDate(ldt); | ||
promotion.setEndDate(ldt); | ||
promotion.setDiscountType("FIXED_AMOUNT"); | ||
promotion.setDiscountAmount(BigDecimal.valueOf(10.00)); | ||
promotion.setMaxDiscountAmount(BigDecimal.valueOf(10.00)); | ||
promotion.setApplicableTo("ENTIRE_CART"); | ||
promotion.setProductSkus(""); | ||
promotion.setMinQuantity(null); | ||
promotion.setFreeQuantity(null); | ||
Product product1 = | ||
new Product( | ||
1L, | ||
"Apple iPhone 12 Pro", | ||
"MOBILE-APPLE-IPHONE-12-PRO", | ||
BigDecimal.valueOf(10010), | ||
1); | ||
Cart cart = new Cart(); | ||
CartProduct cartProduct = new CartProduct(1L, product1, null, cart, 1); | ||
cart.addProduct(cartProduct); | ||
|
||
BigDecimal expectFinalTotalCost = BigDecimal.valueOf(10000.0); | ||
|
||
doReturn(cart).when(underTest).findCartByUsername(Mockito.any()); | ||
CartResponse cartResponse = underTest.applyDiscount(username, promotion); | ||
|
||
assertEquals(expectFinalTotalCost, cartResponse.getFinalTotalCost()); | ||
assertEquals(promotion.getCode(), cartResponse.getPromotionCodes().get(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