Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 메서드 네이밍 변경 #120

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Long uploadProduct(final Long memberId, final Long categoryId, final Prod
}

public Product addViewCount(final Long productId, final Boolean canAddViewCount) {
Product product = findBoardWithPessimisticLock(productId);
Product product = findProductWithPessimisticLock(productId);
product.view(canAddViewCount);
return product;
}
Expand Down Expand Up @@ -69,25 +69,25 @@ private Product findProduct(final Long productId) {
.orElseThrow(ProductNotFoundException::new);
}

private Product findBoardWithPessimisticLock(final Long productId) {
private Product findProductWithPessimisticLock(final Long productId) {
return productRepository.findByIdWithPessimisticLock(productId)
.orElseThrow(ProductNotFoundException::new);
}

public boolean likes(final Long productId, final Long memberId) {
Product product = findProduct(productId);
boolean isNeedToIncrease = isNeedToIncreaseLikeCount(productId, memberId);
product.likes(isNeedToIncrease);
return isNeedToIncrease;
Product product = findProductWithPessimisticLock(productId);
boolean shouldIncreaseLikeCount = shouldIncreaseLikeCount(productId, memberId);
product.likes(shouldIncreaseLikeCount);
return shouldIncreaseLikeCount;
}

private boolean isNeedToIncreaseLikeCount(final Long productId, final Long memberId) {
private boolean shouldIncreaseLikeCount(final Long productId, final Long memberId) {
if (productRepository.existsProductLikeByProductIdAndMemberId(productId, memberId)) {
productRepository.deleteProductLikeByProductIdAndMemberId(productId, memberId);
return false;
}

productRepository.saveProductLike(new ProductLike(productId, memberId));
productRepository.saveProductLike(ProductLike.from(memberId, productId));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public class ProductLike extends BaseEntity {
@Column(nullable = false)
private Long productId;

public ProductLike(final Long memberId, final Long productId) {
this.memberId = memberId;
this.productId = productId;
public static ProductLike from(final Long memberId, final Long productId) {
return ProductLike.builder()
.memberId(memberId)
.productId(productId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.server.market.domain.product.ProductLike;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ProductLikeJpaRepository extends JpaRepository<ProductLike, Long> {

boolean existsByProductIdAndMemberId(Long productId, Long memberId);
@Query("SELECT CASE WHEN COUNT(pl) > 0 THEN true ELSE false END FROM ProductLike pl WHERE pl.productId = :productId AND pl.memberId = :memberId")
boolean existsByProductIdAndMemberId(@Param("productId") Long productId, @Param("memberId") Long memberId);

void deleteByProductIdAndMemberId(Long productId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ public ResponseEntity<List<ProductPagingSimpleResponse>> findLikesProduct(

@PatchMapping("/{categoryId}/products/{productId}/likes")
public ResponseEntity<Boolean> likesProduct(
@PathVariable("productId") final Long productId,
@PathVariable("categoryId") final Long categoryId,
@PathVariable("productId") final Long productId,
@AuthMember final Long memberId
) {
System.out.println("gogo " + memberId + " " + productId);
boolean likes = productService.likes(productId, memberId);
return ResponseEntity.ok(likes);
}
Expand Down
Loading