Skip to content

Commit

Permalink
add Story4
Browse files Browse the repository at this point in the history
  • Loading branch information
patntp committed Mar 31, 2024
1 parent 79e3065 commit a4623b8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
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 kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
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;
}
}
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);
}
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);
}
}

0 comments on commit a4623b8

Please sign in to comment.