diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java index 74c8740..8f73883 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java @@ -1,40 +1,36 @@ package com.kampus.kbazaar.cart; -import com.kampus.kbazaar.product.Product; -import com.kampus.kbazaar.product.ProductService; -import com.kampus.kbazaar.shopper.ShopperService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1") @RequiredArgsConstructor public class CartController { - private final ShopperService shopperService; - private final CartService cartService; - private final ProductService productService; @GetMapping("/carts") public ResponseEntity getCart() { // NOSONAR return ResponseEntity.ok().build(); } - @PostMapping("/carts/{username}/products") - public ResponseEntity addProduct( - @PathVariable("username") String username, - @RequestBody AddProductRequest addProductRequest) { - - Product product = productService.getProductBySku(addProductRequest.getProductSku()); - if (product.getQuantity() < addProductRequest.getQuantity()) { - return ResponseEntity.badRequest().build(); - } - - // return ResponseEntity.ok(cartService - // .findCartByUsername(username)); - // .addProduct(product) - // .toAddProductResponse()); - return ResponseEntity.ok().build(); - } +// @PostMapping("/carts/{username}/products") +// public ResponseEntity addProduct( +// @PathVariable("username") String username, +// @RequestBody AddProductRequest addProductRequest) { +// +// Product product = productService.getProductBySku(addProductRequest.getProductSku()); +// if (product.getQuantity() < addProductRequest.getQuantity()) { +// return ResponseEntity.badRequest().build(); +// } +// +// // return ResponseEntity.ok(cartService +// // .findCartByUsername(username)); +// // .addProduct(product) +// // .toAddProductResponse()); +// return ResponseEntity.ok().build(); +// } } diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java deleted file mode 100644 index 15d58c9..0000000 --- a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.kampus.kbazaar.cart; - -import org.springframework.stereotype.Service; - -@Service -public class CartService {} diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java b/kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java index d778e3d..2d0c0eb 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/product/Product.java @@ -6,17 +6,20 @@ import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.PositiveOrZero; import jakarta.validation.constraints.Size; -import java.math.BigDecimal; -import java.util.Set; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import java.math.BigDecimal; +import java.util.Set; + @Entity @Table( name = "product", uniqueConstraints = {@UniqueConstraint(columnNames = "sku")}) @Data +@Builder @NoArgsConstructor @AllArgsConstructor public class Product { @@ -40,10 +43,12 @@ public class Product { @Column(name = "sku", nullable = false, unique = true, length = 255) private String sku; - @NotNull @Column(name = "price", nullable = false, precision = 10, scale = 2) + @NotNull + @Column(name = "price", nullable = false, precision = 10, scale = 2) private BigDecimal price; - @NotNull @PositiveOrZero + @NotNull + @PositiveOrZero @Column(name = "quantity", nullable = false) private Integer quantity; diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java index e70bfda..3eb5774 100644 --- a/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java +++ b/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java @@ -1,13 +1,12 @@ package com.kampus.kbazaar.cart; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - import com.kampus.kbazaar.security.JwtAuthFilter; +import com.kampus.kbazaar.shopper.ShopperService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -18,18 +17,26 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @ExtendWith(SpringExtension.class) @WebMvcTest( controllers = CartController.class, excludeFilters = - @ComponentScan.Filter( - type = FilterType.ASSIGNABLE_TYPE, - classes = JwtAuthFilter.class)) + @ComponentScan.Filter( + type = FilterType.ASSIGNABLE_TYPE, + classes = JwtAuthFilter.class)) public class CartControllerTest { - @Autowired private MockMvc mockMvc; + @Autowired + private MockMvc mockMvc; + + @Mock + private ShopperService shopperService; - @InjectMocks private CartController cartController; + @InjectMocks + private CartController cartController; @BeforeEach public void setup() { diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.java index 99199e9..4360ccf 100644 --- a/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.java +++ b/kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.java @@ -1,14 +1,6 @@ package com.kampus.kbazaar.product; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; - import com.kampus.kbazaar.exceptions.NotFoundException; -import java.math.BigDecimal; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -16,11 +8,22 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + class ProductServiceTest { - @Mock private ProductRepository productRepository; + @Mock + private ProductRepository productRepository; - @InjectMocks private ProductService productService; + @InjectMocks + private ProductService productService; @BeforeEach void setUp() { @@ -31,15 +34,21 @@ void setUp() { @DisplayName("should be able to get all products") void shouldBeAbleToGetAllProducts() { // Mock data - Product product1 = - new Product( - 1L, - "Google Pixel 5", - "MOBILE-GOOGLE-PIXEL-5", - new BigDecimal(12990.75), - 100); - Product product2 = - new Product(2L, "Coca-Cola", "BEV-COCA-COLA", new BigDecimal(20.75), 150); + Product product1 = Product.builder() + .id(1L) + .name("Google Pixel 5") + .sku("MOBILE-GOOGLE-PIXEL-5") + .price(new BigDecimal(12990.75)) + .quantity(100) + .build(); + + Product product2 = Product.builder() + .id(1L) + .name("Coca-Cola") + .sku("BEV-COCA-COLA") + .price(new BigDecimal(20.75)) + .quantity(150) + .build(); List productList = Arrays.asList(product1, product2); // Mock repository method @@ -71,8 +80,13 @@ void shouldReturnEmptyListWhenNoProductFoundGetAllProducts() { @DisplayName("should be able to get product by SKU") void shouldBeAbleToGetProductBySku() { // Mock data - Product product = - new Product(1L, "Pens", "STATIONERY-PEN-BIC-BALLPOINT", new BigDecimal(14.99), 100); + Product product = Product.builder() + .id(1L) + .name("Pens") + .sku("STATIONERY-PEN-BIC-BALLPOINT") + .price(new BigDecimal(14.99)) + .quantity(100) + .build(); // Mock repository method when(productRepository.findBySku("STATIONERY-PEN-BIC-BALLPOINT"))