forked from KBTG-Kampus-ClassNest-SE-Java/workshopb2-group-1
-
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
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CartRepository extends JpaRepository<Cart, Long> { | ||
} |
50 changes: 50 additions & 0 deletions
50
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.cartItem.CartItem; | ||
import com.kampus.kbazaar.cartItem.CartItemService; | ||
import com.kampus.kbazaar.exceptions.NotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
public class CartService { | ||
private CartRepository cartRepository; | ||
private CartItemService cartItemService; | ||
|
||
public CartService(CartRepository cartRepository, CartItemService cartItemService) { | ||
this.cartRepository = cartRepository; | ||
this.cartItemService = cartItemService; | ||
} | ||
|
||
public List<CartResponse> getAll(){ | ||
List<Cart> carts = cartRepository.findAll(); | ||
if(carts.isEmpty()){ | ||
throw new NotFoundException("Cart not found"); | ||
} | ||
|
||
// CartResponse cartResponse = new CartResponse(); | ||
List<CartResponse> cartResponseList = new ArrayList<CartResponse>(); | ||
for (Cart cart: carts){ | ||
List<CartItem> cartItems = cartItemService.findByUsername(cart.getUsername()); | ||
//Calculate for each username | ||
// BigDecimal discount = 0; | ||
// for (CartItem cartItem: cartItems){ | ||
// | ||
// } | ||
CartResponse cartResponse = new CartResponse( | ||
cart.getUsername(), | ||
cartItems, | ||
cart.getDiscount(), | ||
cart.getTotalDiscount(), | ||
cart.getSubtotal(), | ||
cart.getGrandTotal() | ||
); | ||
cartResponseList.add(cartResponse); | ||
} | ||
|
||
return cartResponseList; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
kbazaar/src/main/java/com/kampus/kbazaar/cartItem/CartItemRepository.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,13 @@ | ||
package com.kampus.kbazaar.cartItem; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface CartItemRepository extends JpaRepository<CartItem, Long> { | ||
|
||
List<CartItem> findAllByUsername(String username); | ||
} |
22 changes: 22 additions & 0 deletions
22
kbazaar/src/main/java/com/kampus/kbazaar/cartItem/CartItemService.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,22 @@ | ||
package com.kampus.kbazaar.cartItem; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class CartItemService { | ||
private CartItemRepository cartItemRepository; | ||
|
||
public CartItemService(CartItemRepository cartItemRepository) { | ||
this.cartItemRepository = cartItemRepository; | ||
} | ||
|
||
public List<CartItem> getAll(){ | ||
return cartItemRepository.findAll(); | ||
} | ||
|
||
public List<CartItem> findByUsername(String username) { | ||
return cartItemRepository.findAllByUsername(username); | ||
} | ||
} |