-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 상품의 태그 Top3 중 있으면 검색해서 조회
- Loading branch information
Showing
10 changed files
with
294 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/funeat/product/persistence/ProductRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.funeat.product.persistence; | ||
|
||
import com.funeat.product.domain.Product; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
|
||
public interface ProductRepositoryCustom { | ||
|
||
List<Product> searchProductsByTopTagsFirst(final Long tagId, final Pageable pageable); | ||
|
||
List<Product> searchProductsByTopTags(final Long tagId, final Long lastProductId, final Pageable pageable); | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/funeat/product/persistence/ProductRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.funeat.product.persistence; | ||
|
||
import com.funeat.product.domain.Product; | ||
import com.funeat.tag.domain.Tag; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.persistence.TypedQuery; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class ProductRepositoryImpl implements ProductRepositoryCustom { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Override | ||
public List<Product> searchProductsByTopTagsFirst(final Long tagId, final Pageable pageable) { | ||
String jpql = "SELECT DISTINCT p FROM Product p " + | ||
"WHERE p.id IN ( " + | ||
" SELECT p2.id FROM Product p2 " + | ||
" JOIN p2.reviews r2 " + | ||
" JOIN r2.reviewTags rt2 " + | ||
" WHERE rt2.tag.id = :tagId AND rt2.tag.id IN ( " + | ||
" SELECT rt3.tag.id FROM Review r3 " + | ||
" JOIN r3.reviewTags rt3 " + | ||
" WHERE r3.product.id = p2.id " + | ||
" GROUP BY rt3.tag.id " + | ||
" ORDER BY COUNT(rt3.tag.id) DESC " + | ||
" ) " + | ||
" GROUP BY p2.id " + | ||
" HAVING COUNT(DISTINCT rt2.tag.id) <= 3 " + | ||
") " + | ||
"ORDER BY p.id DESC"; | ||
|
||
TypedQuery<Product> query = entityManager.createQuery(jpql, Product.class); | ||
query.setParameter("tagId", tagId); | ||
query.setFirstResult((int) pageable.getOffset()); | ||
query.setMaxResults(pageable.getPageSize()); | ||
|
||
return query.getResultList(); | ||
} | ||
|
||
@Override | ||
public List<Product> searchProductsByTopTags(Long tagId, Long lastProductId, Pageable pageable) { | ||
String jpql = "SELECT DISTINCT p FROM Product p " + | ||
"WHERE p.id < :lastProductId AND p.id IN ( " + | ||
" SELECT p2.id FROM Product p2 " + | ||
" JOIN p2.reviews r2 " + | ||
" JOIN r2.reviewTags rt2 " + | ||
" WHERE rt2.tag.id = :tagId AND rt2.tag.id IN ( " + | ||
" SELECT rt3.tag.id FROM Review r3 " + | ||
" JOIN r3.reviewTags rt3 " + | ||
" WHERE r3.product.id = p2.id " + | ||
" GROUP BY rt3.tag.id " + | ||
" ORDER BY COUNT(rt3.tag.id) DESC " + | ||
" ) " + | ||
" GROUP BY p2.id " + | ||
" HAVING COUNT(DISTINCT rt2.tag.id) <= 3 " + | ||
") " + | ||
"ORDER BY p.id DESC"; | ||
|
||
TypedQuery<Product> query = entityManager.createQuery(jpql, Product.class); | ||
query.setParameter("tagId", tagId); | ||
query.setParameter("lastProductId", lastProductId); | ||
query.setFirstResult((int) pageable.getOffset()); | ||
query.setMaxResults(pageable.getPageSize()); | ||
|
||
return query.getResultList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.