Skip to content

Commit

Permalink
add cart service and cart repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Bank authored and Bank committed Mar 23, 2024
1 parent dceb02a commit 0056c84
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
6 changes: 6 additions & 0 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ public class Cart {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "shopper_id", referencedColumnName = "id")
private Shopper shopper;

public Cart addProduct(Product product) {
this.products.add(product);
product.getCarts().add(this);
return this;
}
}
42 changes: 23 additions & 19 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package com.kampus.kbazaar.cart;

import com.kampus.kbazaar.shopper.ShopperService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class CartController {

private final ShopperService shopperService;

@GetMapping("/carts")
public ResponseEntity getCart() { // NOSONAR
return ResponseEntity.ok().build();
}

// @PostMapping("/carts/{username}/products")
// public ResponseEntity<AddProductResponse> 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<String> addProduct(
@PathVariable("username") String username,
@RequestBody AddProductRequest addProductRequest) {

// Cart cart = cartService.findCartByUsername(username);
// 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("hey");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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> {}
14 changes: 14 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,14 @@
package com.kampus.kbazaar.cart;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CartService {
private final CartRepository cartRepository;

public Cart findCartByUsername(String username) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
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;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.MediaType;
Expand All @@ -31,7 +31,7 @@ public class CartControllerTest {

@Autowired private MockMvc mockMvc;

@Mock private ShopperService shopperService;
@MockBean private ShopperService shopperService;

@InjectMocks private CartController cartController;

Expand Down

0 comments on commit 0056c84

Please sign in to comment.