Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/spring_team4/spring_team4/cart/Cart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package spring_team4.spring_team4.cart;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import spring_team4.spring_team4.product.Product;
import spring_team4.spring_team4.user.User;

@Entity
@Getter
@Setter
public class Cart {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cartId;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@ManyToOne
@JoinColumn(name = "product_id")
private Product product;
}
42 changes: 42 additions & 0 deletions src/main/java/spring_team4/spring_team4/cart/CartController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package spring_team4.spring_team4.cart;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

@Autowired
private CartService cartService;

@PostMapping
public ResponseEntity<String> addToCart(@RequestBody Cart cart) {
boolean isAdded = cartService.addProductToCart(cart);
if (isAdded) {
return ResponseEntity.status(HttpStatus.CREATED).body("Product added to cart.");
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Failed to add product to cart.");
}

@GetMapping
public ResponseEntity<List<Cart>> getCartItems(
@RequestParam(required = false) Long productId,
@RequestParam(required = false) Long categoryId,
@RequestParam(required = false) Long userId) {
List<Cart> cartItems = cartService.getCartItems(productId, categoryId, userId);
return ResponseEntity.ok(cartItems);
}

@DeleteMapping
public ResponseEntity<String> removeFromCart(@RequestBody List<Long> cartIds) {
boolean isRemoved = cartService.removeProductsFromCart(cartIds);
if (isRemoved) {
return ResponseEntity.ok("Products removed from cart.");
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Failed to remove products from cart.");
}
}
11 changes: 11 additions & 0 deletions src/main/java/spring_team4/spring_team4/cart/CartRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package spring_team4.spring_team4.cart;

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

import java.util.List;

public interface CartRepository extends JpaRepository<Cart, Long> {
List<Cart> findByProductProductId(Long productId);
List<Cart> findByProductCategoryId(Long categoryId);
List<Cart> findByUserUserId(Long userId);
}
37 changes: 37 additions & 0 deletions src/main/java/spring_team4/spring_team4/cart/CartService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package spring_team4.spring_team4.cart;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartService {

@Autowired
private CartRepository cartRepository;

public boolean addProductToCart(Cart cart) {
// 장바구니에 동일한 상품이 이미 있으면 추가하지 않거나, 수량 증가 로직 등을 넣을 수 있음
cartRepository.save(cart);
return true;
}

public List<Cart> getCartItems(Long productId, Long categoryId, Long userId) {
if (productId != null) {
return cartRepository.findByProductProductId(productId);
}
if (categoryId != null) {
return cartRepository.findByProductCategoryId(categoryId);
}
if (userId != null) {
return cartRepository.findByUserUserId(userId);
}
return cartRepository.findAll();
}

public boolean removeProductsFromCart(List<Long> cartIds) {
cartRepository.deleteAllByIdInBatch(cartIds);
return true;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package spring_team4.spring_team4.cart.dto;

public class CartRequest {
private Long productId;
private String userId;

// 생성자, getter, setter
}
11 changes: 11 additions & 0 deletions src/main/java/spring_team4/spring_team4/cart/dto/CartResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package spring_team4.spring_team4.cart.dto;

public class CartResponse {
private Long cartItemId;
private String userId;
private Long productId;
private String productName;
private String categoryName;

// 생성자, getter, setter
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package spring_team4.spring_team4.product;


import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/*order 추가 예정*/
import spring_team4.spring_team4.category.Category;
import spring_team4.spring_team4.cart.cartStandard;
import spring_team4.spring_team4.order.Order;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

Expand All @@ -19,3 +21,4 @@ public interface ProductRepository extends JpaRepository<Product, Long> {

// List<Product> findByProductNameContaining(String keyword);*/
}