Skip to content

Commit

Permalink
Merge pull request #176 from Team-BC-1/fix/like-product
Browse files Browse the repository at this point in the history
중복된 관심상품 등록요청에 대한 검증처리
  • Loading branch information
vanillacake369 authored Jan 19, 2024
2 parents add0a4b + 4d2b2e8 commit 77d1b39
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("api/products")
@RequestMapping("/api/products")
public class ProductLikeController {

private final ProductLikeService productLikeService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

public record ProductLikesResponseDto(
Long id,
String brand,
String name,
String imageUrl,
String description,
Long price
String productBrand,
String productName,
String productImageUrl,
String productDescription,
Long productPrice
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
Expand All @@ -35,7 +36,8 @@ public class LikeProduct {
@JoinColumn(name = "product_id")
private Product product;

public LikeProduct(User user, Product product) {
@Builder
private LikeProduct(User user, Product product) {
this.user = user;
this.product = product;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/bc1/gream/domain/product/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.AccessLevel;
import lombok.Builder;
Expand All @@ -37,7 +35,7 @@ public class Product extends BaseEntity {

@JsonIgnore
@OneToMany(mappedBy = "product", targetEntity = LikeProduct.class, cascade = CascadeType.ALL, orphanRemoval = true)
private final List<LikeProduct> likeProducts = new ArrayList<>();
private final Set<LikeProduct> likeProducts = new HashSet<>();

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import jakarta.transaction.Transactional;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class ProductLikeService {


Expand All @@ -23,7 +25,11 @@ public class ProductLikeService {
@Transactional
public void likeProduct(User user, Long productId) {
Product product = getProductBy(productId);
user.addLikeProduct(product);
boolean hasNotLikedThisProduct = user.getLikeProducts().stream()
.noneMatch(likeProduct -> likeProduct.getProduct().equals(product));
if (hasNotLikedThisProduct) {
user.addLikeProduct(product);
}
}

@Transactional
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/bc1/gream/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@Getter
@Entity
Expand All @@ -34,15 +33,15 @@ public class User extends BaseEntity {

@JsonIgnore
@OneToMany(mappedBy = "buyer", targetEntity = Order.class)
private final Set purchasedOrders = new HashSet();
private final Set<Order> purchasedOrders = new HashSet<>();

@JsonIgnore
@OneToMany(mappedBy = "seller", targetEntity = Order.class)
private final Set saleOrders = new HashSet();
private final Set<Order> saleOrders = new HashSet<>();

@JsonIgnore
@OneToMany(mappedBy = "user", targetEntity = LikeProduct.class, cascade = CascadeType.ALL, orphanRemoval = true)
private final List<LikeProduct> likeProducts = new ArrayList<>();
private final Set<LikeProduct> likeProducts = new HashSet<>();

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -78,12 +77,14 @@ private User(String loginId, String nickname, String password, UserRole role, Pr
this.point = 100000L;
}

@Transactional
public void addLikeProduct(Product product) {
LikeProduct likeProduct = new LikeProduct(this, product);
LikeProduct likeProduct = LikeProduct.builder().user(this).product(product).build();
product.getLikeProducts().add(likeProduct);
this.likeProducts.add(likeProduct);
}

@Transactional
public void removeLikeProduct(Product product) {
LikeProduct likeProduct = this.likeProducts.stream()
.filter(lp -> lp.getProduct().equals(product))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void tearDown() {
@Test
public void 중간조인테이블_복합키_확인() {
// GIVEN
LikeProduct likeProduct = new LikeProduct(TEST_USER, TEST_PRODUCT);
LikeProduct likeProduct = LikeProduct.builder().user(TEST_USER).product(TEST_PRODUCT).build();

// WHEN
LikeProduct savedLikeProduct = likeProductRepository.save(likeProduct);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package bc1.gream.domain.product.service.command;

import static org.junit.jupiter.api.Assertions.assertTrue;

import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.repository.LikeProductRepository;
import bc1.gream.domain.user.entity.User;
import bc1.gream.test.BaseIntegrationTest;
import bc1.gream.test.ProductTest;
import bc1.gream.test.UserTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Disabled("통합테스트는 로컬에서만 실행합니다. 실행 시, SECRET KEY 에 대한 IntelliJ 환경변수를 설정해주어야 합니다.")
@Transactional
class ProductLikeServiceIntegrationTest extends BaseIntegrationTest implements ProductTest, UserTest {

@Autowired
private ProductLikeService productLikeService;
@Autowired
private LikeProductRepository likeProductRepository;

@BeforeEach
void setUp() {
setUpBaseIntegrationTest();
}

@AfterEach
void tearDown() {
tearDownBaseIntegrationTest();
}

@Test
@DisplayName("중복된 관심상품 요청을 검증합니다.")
public void 중복된_관심상품_요청검증() {
// GIVEN
Product product = savedIcedAmericano;
User user = savedBuyer;

// WHEN
productLikeService.likeProduct(user, product.getId());

// THEN
boolean hasNotLikedThisProduct = user.getLikeProducts().stream()
.noneMatch(likeProduct -> likeProduct.getProduct().equals(product));
assertTrue(hasNotLikedThisProduct);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
@Rollback(value = true)
@Transactional
Expand Down Expand Up @@ -55,7 +57,7 @@ class ProductLikeServiceTest implements ProductTest, UserTest {
// GIVEN
given(productRepository.findById(TEST_PRODUCT_ID)).willReturn(Optional.of(TEST_PRODUCT));
User user = User.builder().build();
LikeProduct likeProduct = new LikeProduct(user, TEST_PRODUCT);
LikeProduct likeProduct = LikeProduct.builder().user(user).product(TEST_PRODUCT).build();
user.getLikeProducts().add(likeProduct);

// WHEN
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/bc1/gream/test/BaseDataRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import bc1.gream.domain.order.entity.Order;
import bc1.gream.domain.order.repository.OrderRepository;
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.repository.LikeProductRepository;
import bc1.gream.domain.product.repository.ProductRepository;
import bc1.gream.domain.sell.entity.Sell;
import bc1.gream.domain.sell.repository.SellRepository;
Expand Down Expand Up @@ -47,6 +48,8 @@ public class BaseDataRepositoryTest implements ProductTest, UserTest, CouponTest
@Autowired
protected ProductRepository productRepository;
@Autowired
protected LikeProductRepository likeProductRepository;
@Autowired
protected UserRepository userRepository;
@Autowired
protected CouponRepository couponRepository;
Expand Down Expand Up @@ -82,6 +85,7 @@ protected void tearDownBaseDataRepositoryTest() {
gifticonRepository.deleteAllInBatch();
orderRepository.deleteAllInBatch();
couponRepository.deleteAllInBatch();
likeProductRepository.deleteAllInBatch();
userRepository.deleteAllInBatch();
productRepository.deleteAllInBatch();
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/bc1/gream/test/BaseIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import bc1.gream.domain.order.entity.Order;
import bc1.gream.domain.order.repository.OrderRepository;
import bc1.gream.domain.product.entity.Product;
import bc1.gream.domain.product.repository.LikeProductRepository;
import bc1.gream.domain.product.repository.ProductRepository;
import bc1.gream.domain.sell.entity.Sell;
import bc1.gream.domain.sell.repository.SellRepository;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class BaseIntegrationTest implements ProductTest, UserTest, CouponTest, B
@Autowired
protected ProductRepository productRepository;
@Autowired
protected LikeProductRepository likeProductRepository;
@Autowired
protected UserRepository userRepository;
@Autowired
protected CouponRepository couponRepository;
Expand Down Expand Up @@ -91,6 +94,7 @@ protected void tearDownBaseIntegrationTest() {
gifticonRepository.deleteAllInBatch();
orderRepository.deleteAllInBatch();
couponRepository.deleteAllInBatch();
likeProductRepository.deleteAllInBatch();
productRepository.deleteAllInBatch();
userRepository.deleteAllInBatch();
}
Expand Down

0 comments on commit 77d1b39

Please sign in to comment.