Skip to content
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
28 changes: 10 additions & 18 deletions src/main/java/stackpot/stackpot/pot/repository/PotRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
import java.util.Optional;

public interface PotRepository extends JpaRepository<Pot, Long> {
Page<Pot> findByRecruitmentDetails_RecruitmentRole(Role recruitmentRole, Pageable pageable);

Optional<Pot> findPotWithRecruitmentDetailsByPotId(Long potId);

List<Pot> findByPotApplication_User_Id(Long userId);

List<Pot> findByUserId(Long userId);

Page<Pot> findAll(Pageable pageable);

List<Pot> findByPotMembers_UserIdAndPotStatusOrderByCreatedAtDesc(Long userId, String status);

List<Pot> findByUserIdAndPotStatus(Long userId, String status);

Page<Pot> findByUserIdAndPotStatus(Long userId, String potStatus, Pageable pageable);
@Query("SELECT p FROM Pot p WHERE p.user.id = :userId AND p.potStatus = :potStatus ORDER BY p.createdAt DESC")
Page<Pot> findByUserIdAndPotStatusOrderByCreatedAtDesc(@Param("userId") Long userId,
@Param("potStatus") String potStatus,
Pageable pageable);

@Query("SELECT p FROM Pot p " +
"WHERE LOWER(p.potName) LIKE LOWER(CONCAT('%', :keyword, '%')) " +
Expand All @@ -49,33 +48,26 @@ public interface PotRepository extends JpaRepository<Pot, Long> {
"SELECT pm FROM PotMember pm WHERE pm.pot = p AND pm.user.id = :userId)")
List<Pot> findCompletedPotsByUserId(@Param("userId") Long userId);

List<Pot> findByPotMembers_User_Id(Long potMembersUserId);

@Query("SELECT p FROM Pot p WHERE p.user.id = :userId AND p.potStatus = 'COMPLETED' AND (:cursor IS NULL OR p.potId < :cursor) ORDER BY p.potId DESC")
List<Pot> findCompletedPotsCreatedByUser(@Param("userId") Long userId, @Param("cursor") Long cursor);

@Modifying
@Query("DELETE FROM Pot f WHERE f.user.id = :userId")
void deleteByUserId(@Param("userId") Long userId);
// @Modifying
// @Query("DELETE FROM Pot p WHERE p.user.id = :userId AND p.potId IN :potIds AND p.potStatus = 'RECRUITING'")
// void deleteByUserIdAndPotIds(@Param("userId") Long userId, @Param("potIds") List<Long> potIds);
boolean existsByUserId(Long userId);

// 지원자 수 기준으로 모든 Pot 정렬
@Query("SELECT p FROM Pot p LEFT JOIN PotApplication pa ON p = pa.pot " +
"WHERE p.potStatus = :potStatus " +
"GROUP BY p " +
"ORDER BY COUNT(pa.applicationId) DESC, p.createdAt DESC")
Page<Pot> findAllOrderByApplicantsCountDesc(Pageable pageable);
"ORDER BY p.createdAt DESC, COUNT(pa.applicationId) DESC ")
Page<Pot> findAllOrderByApplicantsCountDesc(@Param("potStatus") String potStatus, Pageable pageable);

Comment on lines +58 to 62
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

메서드명-정렬 불일치 및 카운트 쿼리 부재

메서드명은 ApplicantsCountDesc지만 JPQL은 createdAt 우선입니다. 또한 GROUP BY가 있어 자동 count 쿼리 효율 저하 가능. 아래처럼 정렬 순서와 countQuery를 명시해주세요.

-    @Query("SELECT p FROM Pot p LEFT JOIN PotApplication pa ON p = pa.pot " +
-            "WHERE p.potStatus = :potStatus " +
-            "GROUP BY p " +
-            "ORDER BY p.createdAt DESC, COUNT(pa.applicationId) DESC ")
+    @Query(value = "SELECT p FROM Pot p LEFT JOIN p.potApplication pa " +
+            "WHERE p.potStatus = :potStatus " +
+            "GROUP BY p " +
+            "ORDER BY COUNT(pa.applicationId) DESC, p.createdAt DESC",
+           countQuery = "SELECT COUNT(p) FROM Pot p WHERE p.potStatus = :potStatus")
     Page<Pot> findAllOrderByApplicantsCountDesc(@Param("potStatus") String potStatus, Pageable pageable);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"WHERE p.potStatus = :potStatus " +
"GROUP BY p " +
"ORDER BY COUNT(pa.applicationId) DESC, p.createdAt DESC")
Page<Pot> findAllOrderByApplicantsCountDesc(Pageable pageable);
"ORDER BY p.createdAt DESC, COUNT(pa.applicationId) DESC ")
Page<Pot> findAllOrderByApplicantsCountDesc(@Param("potStatus") String potStatus, Pageable pageable);
@Query(value = "SELECT p FROM Pot p LEFT JOIN p.potApplication pa " +
"WHERE p.potStatus = :potStatus " +
"GROUP BY p " +
"ORDER BY COUNT(pa.applicationId) DESC, p.createdAt DESC",
countQuery = "SELECT COUNT(p) FROM Pot p WHERE p.potStatus = :potStatus")
Page<Pot> findAllOrderByApplicantsCountDesc(@Param("potStatus") String potStatus, Pageable pageable);
🤖 Prompt for AI Agents
In src/main/java/stackpot/stackpot/pot/repository/PotRepository.java around
lines 58-62, the JPQL ordering is inconsistent with the method name (it orders
by createdAt first) and the GROUP BY will cause an inefficient automatic count;
change the main query ORDER BY to "COUNT(pa.applicationId) DESC, p.createdAt
DESC" to match ApplicantsCountDesc, and add an explicit countQuery (e.g. "SELECT
COUNT(DISTINCT p) FROM Pot p WHERE p.potStatus = :potStatus") on the @Query
annotation so Spring Data uses a simple count instead of a grouped count.

/// 여러 Role을 기준으로 지원자 수 많은 순 정렬
@Query("SELECT p FROM Pot p " +
"LEFT JOIN PotRecruitmentDetails prd ON p = prd.pot " +
"LEFT JOIN PotApplication pa ON p = pa.pot " +
"WHERE prd.recruitmentRole IN :roles " +
"WHERE prd.recruitmentRole IN :roles AND p.potStatus = :potStatus " +
"GROUP BY p " +
"ORDER BY COUNT(pa.applicationId) DESC, p.createdAt DESC")
Page<Pot> findByRecruitmentRolesInOrderByApplicantsCountDesc(@Param("roles") List<Role> roles, Pageable pageable);
"ORDER BY p.createdAt DESC, COUNT(pa.applicationId) DESC ")
Page<Pot> findByRecruitmentRolesInOrderByApplicantsCountDesc(@Param("roles") List<Role> roles, @Param("potStatus") String potStatus, Pageable pageable);

Comment on lines +67 to 71
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

집계 값 왜곡 가능성(CROSS JOIN 중복) 및 정렬 순서 수정 필요

prd/pa 이중 조인으로 pa 행이 곱연산되어 COUNT가 과대집계될 수 있습니다. DISTINCT로 방지하고, 정렬도 지원자 수 우선으로 수정, countQuery를 명시하세요.

-    @Query("SELECT p FROM Pot p " +
-            "LEFT JOIN PotRecruitmentDetails prd ON p = prd.pot " +
-            "LEFT JOIN PotApplication pa ON p = pa.pot " +
-            "WHERE prd.recruitmentRole IN :roles AND p.potStatus = :potStatus " +
-            "GROUP BY p " +
-            "ORDER BY p.createdAt DESC, COUNT(pa.applicationId) DESC ")
+    @Query(value = "SELECT p FROM Pot p " +
+            "LEFT JOIN p.recruitmentDetails prd " +
+            "LEFT JOIN p.potApplication pa " +
+            "WHERE prd.recruitmentRole IN :roles AND p.potStatus = :potStatus " +
+            "GROUP BY p " +
+            "ORDER BY COUNT(DISTINCT pa.applicationId) DESC, p.createdAt DESC",
+           countQuery = "SELECT COUNT(DISTINCT p.potId) FROM Pot p " +
+                        "LEFT JOIN p.recruitmentDetails prd " +
+                        "WHERE prd.recruitmentRole IN :roles AND p.potStatus = :potStatus")
     Page<Pot> findByRecruitmentRolesInOrderByApplicantsCountDesc(@Param("roles") List<Role> roles, @Param("potStatus") String potStatus, Pageable pageable);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"WHERE prd.recruitmentRole IN :roles AND p.potStatus = :potStatus " +
"GROUP BY p " +
"ORDER BY COUNT(pa.applicationId) DESC, p.createdAt DESC")
Page<Pot> findByRecruitmentRolesInOrderByApplicantsCountDesc(@Param("roles") List<Role> roles, Pageable pageable);
"ORDER BY p.createdAt DESC, COUNT(pa.applicationId) DESC ")
Page<Pot> findByRecruitmentRolesInOrderByApplicantsCountDesc(@Param("roles") List<Role> roles, @Param("potStatus") String potStatus, Pageable pageable);
@Query(
value = "SELECT p FROM Pot p " +
"LEFT JOIN p.recruitmentDetails prd " +
"LEFT JOIN p.potApplication pa " +
"WHERE prd.recruitmentRole IN :roles AND p.potStatus = :potStatus " +
"GROUP BY p " +
"ORDER BY COUNT(DISTINCT pa.applicationId) DESC, p.createdAt DESC",
countQuery = "SELECT COUNT(DISTINCT p.potId) FROM Pot p " +
"LEFT JOIN p.recruitmentDetails prd " +
"WHERE prd.recruitmentRole IN :roles AND p.potStatus = :potStatus"
)
Page<Pot> findByRecruitmentRolesInOrderByApplicantsCountDesc(
@Param("roles") List<Role> roles,
@Param("potStatus") String potStatus,
Pageable pageable
);
🤖 Prompt for AI Agents
In src/main/java/stackpot/stackpot/pot/repository/PotRepository.java around
lines 67–71, the JPQL query can overcount applicants because the double join
(prd/pa) multiplies rows; update the query to use COUNT(DISTINCT
pa.applicationId) to avoid duplicate aggregation, ensure joins are LEFT JOINs so
pots with zero applicants are included, change ORDER BY to sort by applicants
count first then p.createdAt DESC, and add an explicit countQuery for pagination
that mirrors the joins (but without the SELECT clause aggregation) so Spring
Data Page counts are correct.

List<Pot> findByPotMembers_UserIdOrderByCreatedAtDesc(Long userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public Map<String, Object> getMyRecruitingPotsWithPaging(Integer page, Integer s
User user = authService.getCurrentUser();

Pageable pageable = PageRequest.of(page - 1, size);
Page<Pot> potPage = potRepository.findByUserIdAndPotStatus(user.getId(), "RECRUITING", pageable);
Page<Pot> potPage = potRepository.findByUserIdAndPotStatusOrderByCreatedAtDesc(user.getId(), "RECRUITING", pageable);

List<Pot> pots = potPage.getContent();

Expand Down Expand Up @@ -264,12 +264,12 @@ public Map<String, Object> getAllPotsWithPaging(List<Role> roles, int page, int
Page<Pot> potPage;

if (onlyMine != null && onlyMine && user != null) {
potPage = potRepository.findByUserIdAndPotStatus(user.getId(), "RECRUITING", pageable);
potPage = potRepository.findByUserIdAndPotStatusOrderByCreatedAtDesc(user.getId(), "RECRUITING", pageable);
} else {
if (roles == null || roles.isEmpty()) {
potPage = potRepository.findAllOrderByApplicantsCountDesc(pageable);
potPage = potRepository.findAllOrderByApplicantsCountDesc("RECRUITING", pageable);
} else {
potPage = potRepository.findByRecruitmentRolesInOrderByApplicantsCountDesc(roles, pageable);
potPage = potRepository.findByRecruitmentRolesInOrderByApplicantsCountDesc(roles, "RECRUITING", pageable);
}
}

Expand Down