Skip to content

Commit

Permalink
Merge branch 'main' of github.com:KBTG-Kampus-ClassNest-SE-Java/works…
Browse files Browse the repository at this point in the history
…hopb2-group-1

# Conflicts:
#	kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java
  • Loading branch information
Patpum Hakaew authored and Patpum Hakaew committed Mar 31, 2024
2 parents 6c0e12d + 93c1bf0 commit eabdc1d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
2 changes: 1 addition & 1 deletion infra/gitops/dev/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spec:
spec:
containers:
- name: kbazaar-api
image: ghcr.io/kbtg-kampus-classnest-se-java/workshopb2-group-1:ec371c2dd1e5d8e83d00f4f4c7f38183dec4e646
image: ghcr.io/kbtg-kampus-classnest-se-java/workshopb2-group-1:55708518210d5b9ee3973c135b0602a2fb4ca2ca
imagePullPolicy: Always
ports:
- containerPort: 8080
Expand Down
25 changes: 4 additions & 21 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.kampus.kbazaar.cart;

import com.kampus.kbazaar.cartitem.CartItemRepository;
import com.kampus.kbazaar.cartitem.CartItemService;
import com.kampus.kbazaar.promotion.PromotionApplyCartRequest;
import com.kampus.kbazaar.shopper.Shopper;
import com.kampus.kbazaar.shopper.ShopperRepository;
import java.util.List;
Expand All @@ -11,39 +9,24 @@
@Service
public class CartService {

private final CartRepository cartRepository;
private final CartItemRepository cartItemRepository;
private final CartItemService cartItemService;

private final ShopperRepository shopperRepository;

public CartResponse applyCartPromotion(
String username, PromotionApplyCartRequest promotionApplyCartRequest) {
return new CartResponse();
}

public CartService(
CartRepository cartRepository,
CartItemRepository cartItemRepository,
ShopperRepository shopperRepository,
CartItemService cartItemService) {
this.cartRepository = cartRepository;
this.cartItemRepository = cartItemRepository;
public CartService(ShopperRepository shopperRepository, CartItemService cartItemService) {
this.shopperRepository = shopperRepository;
this.cartItemService = cartItemService;
}

// get all carts
public List<CartResponse> getAllCart() {

// getb all user
// get all user
List<String> usernameList =
shopperRepository.findAll().stream().map(Shopper::getUsername).toList();

// for loop getcartby usernam
List<CartResponse> carts =
usernameList.stream().map(cartItemService::getCartByUsername).toList();
// for loop get cart by username

return carts;
return usernameList.stream().map(cartItemService::getCartByUsername).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ public class PromotionService {

private final CartItemService cartItemService;

private final CartRepository cartRepository;

public PromotionService(
PromotionRepository promotionRepository,
CartItemRepository cartItemRepository,
CartRepository cartRepository,
CartItemService cartItemService) {
this.promotionRepository = promotionRepository;
this.cartItemRepository = cartItemRepository;
this.cartRepository = cartRepository;
this.cartItemService = cartItemService;
}

Expand Down Expand Up @@ -57,8 +54,6 @@ public CartResponse applyPromotion(String username, Promotion promotion) {
}
}

CartResponse response = cartItemService.getCartByUsername(username);

return response;
return cartItemService.getCartByUsername(username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.kampus.kbazaar.promotion.PromotionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

import com.kampus.kbazaar.cart.Cart;
import com.kampus.kbazaar.cart.CartRepository;
Expand Down Expand Up @@ -57,4 +56,22 @@ void shouldAddCartItem() {

verify(cartItemRepository).save(any());
}

@Test
void shouldGetCartByUsernameWhenDataIsNotPresent() {
Cart cart = new Cart();
cart.setId(1L);
cart.setUsername("mockusername");
cart.setDiscount(BigDecimal.ZERO);
cart.setTotalDiscount(BigDecimal.ZERO);
cart.setPromotionCodes("promo-code");
cart.setSubtotal(BigDecimal.ZERO);
cart.setGrandTotal(BigDecimal.ZERO);

when(cartRepository.getCartByUsername(anyString())).thenReturn(Optional.empty());
when(cartRepository.save(any())).thenReturn(cart);

cartItemService.getCartByUsername("username");
verify(cartRepository, times(1)).save(any());
}
}

0 comments on commit eabdc1d

Please sign in to comment.