-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from TecheerTeam/fix#83
Fix#83
- Loading branch information
Showing
8 changed files
with
180 additions
and
28 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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/techeer/abandoneddog/global/config/QuerydslConfig.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,18 @@ | ||
package com.techeer.abandoneddog.global.config; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class QuerydslConfig { | ||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/techeer/abandoneddog/pet_board/repository/PetBoardRepositoryCustom.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,9 @@ | ||
package com.techeer.abandoneddog.pet_board.repository; | ||
|
||
import com.techeer.abandoneddog.pet_board.entity.PetBoard; | ||
import com.techeer.abandoneddog.pet_board.entity.Status; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
public interface PetBoardRepositoryCustom { | ||
Page<PetBoard> searchPetBoards(String categories, Status status, Integer minAge, Integer maxAge, String title, Boolean isYoung, Pageable pageable); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/techeer/abandoneddog/pet_board/repository/PetBoardRepositoryImpl.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,65 @@ | ||
package com.techeer.abandoneddog.pet_board.repository; | ||
|
||
import com.querydsl.core.BooleanBuilder; | ||
import com.querydsl.jpa.impl.JPAQuery; | ||
import com.techeer.abandoneddog.animal.entity.QPetInfo; | ||
import com.techeer.abandoneddog.pet_board.entity.PetBoard; | ||
import com.techeer.abandoneddog.pet_board.entity.QPetBoard; | ||
import com.techeer.abandoneddog.pet_board.entity.Status; | ||
import jakarta.persistence.EntityManager; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
@Repository | ||
public class PetBoardRepositoryImpl implements PetBoardRepositoryCustom { | ||
|
||
@Autowired | ||
private EntityManager entityManager; | ||
|
||
@Override | ||
public Page<PetBoard> searchPetBoards(String categories, Status status, Integer minAge, Integer maxAge, String title, Boolean isYoung, Pageable pageable) { | ||
QPetBoard petBoard = QPetBoard.petBoard; | ||
QPetInfo petInfo = QPetInfo.petInfo; | ||
|
||
JPAQuery<PetBoard> query = new JPAQuery<>(entityManager); | ||
BooleanBuilder builder = new BooleanBuilder(); | ||
|
||
query.from(petBoard) | ||
.join(petBoard.petInfo, petInfo); | ||
|
||
if (categories != null) { | ||
builder.and(petInfo.kindCd.eq(categories)); | ||
} | ||
if (status != null) { | ||
builder.and(petBoard.status.eq(status)); | ||
} | ||
if (minAge != null) { | ||
builder.and(petInfo.age.goe(minAge)); | ||
} | ||
if (maxAge != null) { | ||
builder.and(petInfo.age.loe(maxAge)); | ||
} | ||
if (isYoung != null) { | ||
builder.and(petInfo.isYoung.eq(isYoung)); | ||
} | ||
if (title != null) { | ||
builder.and(petBoard.title.containsIgnoreCase(title)); | ||
} | ||
|
||
query.where(builder); | ||
|
||
long total = query.fetchCount(); | ||
List<PetBoard> results = query | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
return new PageImpl<>(results, pageable, total); | ||
} | ||
|
||
|
||
} |
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