Skip to content

Commit

Permalink
feat: cart
Browse files Browse the repository at this point in the history
  • Loading branch information
chaiyapluek committed Mar 23, 2024
1 parent 211cd79 commit a262bd7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
22 changes: 14 additions & 8 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import com.kampus.kbazaar.product.Product;
import lombok.Data;

@Data
public class Cart {
private int userID;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import lombok.Getter;
import lombok.Setter;

private Product[] products;
@Entity(name = "cart")
@Getter
@Setter
public class Cart {
@EmbeddedId private CartId cartId;
private Integer quantity;

public Cart(int userID, Product[] products) {
this.userID = userID;
this.products = products;
public Cart(CartId cartId, Integer quantity) {
this.cartId = cartId;
this.quantity = quantity;
}
}
}
10 changes: 10 additions & 0 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
@RestController
@RequestMapping("/api/v1")
public class CartController {
private CartService cartService;

public CartController(CartService cartService) {
this.cartService = cartService;
}

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

@DeleteMapping("/carts/{username}/items/{product_sku}")
public String deleteCartItem(@PathVariable String username, @PathVariable String product_sku) {
return cartService.deleteCartItem(username, product_sku);
}
}
17 changes: 17 additions & 0 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kampus.kbazaar.cart;

import jakarta.persistence.Embeddable;
import java.io.Serializable;
import lombok.Getter;

@Getter
@Embeddable
public class CartId implements Serializable {
private Long shopper_id;
private Long product_id;

public CartId(Long shopper_id, Long product_id) {
this.shopper_id = shopper_id;
this.product_id = product_id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.kampus.kbazaar.cart;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CartRepository extends JpaRepository<Cart, CartId> {

}
16 changes: 16 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,16 @@
package com.kampus.kbazaar.cart;

import org.springframework.stereotype.Service;

@Service
public class CartService {
private CartRepository cartRepository;

public CartService(CartRepository cartRepository) {
this.cartRepository = cartRepository;
}

public String deleteCartItem(String username, String product_sku) {
return "deleteL: " + username + product_sku;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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 @@ -29,6 +30,8 @@ public class CartControllerTest {

@Autowired private MockMvc mockMvc;

@MockBean private CartService cartService;

@InjectMocks private CartController cartController;

@BeforeEach
Expand Down

0 comments on commit a262bd7

Please sign in to comment.