diff --git a/src/main/java/com/moneymong/domain/agency/api/AgencyController.java b/src/main/java/com/moneymong/domain/agency/api/AgencyController.java index 185ae1e..9f08b64 100644 --- a/src/main/java/com/moneymong/domain/agency/api/AgencyController.java +++ b/src/main/java/com/moneymong/domain/agency/api/AgencyController.java @@ -47,6 +47,12 @@ public SearchAgencyResponse getAgencyList( return agencyService.getAgencyList(user.getId(), pageable); } + @Operation(summary = "소속 목록 검색") + @GetMapping("/search") + public List searchAgencyList(@RequestParam("keyword") String keyword, @AuthenticationPrincipal JwtAuthentication user) { + return agencyService.search(user.getId(), keyword); + } + @Operation(summary = "소속 내 멤버 목록 조회") @GetMapping("/{agencyId}/agency-users") public AgencyUserResponses getAgencyUserList( diff --git a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryCustom.java b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryCustom.java index 2c87a96..0e6dc85 100644 --- a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryCustom.java +++ b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryCustom.java @@ -6,8 +6,12 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import java.util.List; + public interface AgencyRepositoryCustom { Page findByUniversityNameByPaging(String universityName, Pageable pageable); Page findByUniversityNameAndAgencyTypeByPaging(String universityName, AgencyType type, Pageable pageable); + + List findByKeyword(String university, String keyword); } diff --git a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java index b2ff304..61cdb94 100644 --- a/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java +++ b/src/main/java/com/moneymong/domain/agency/repository/AgencyRepositoryImpl.java @@ -16,6 +16,7 @@ import java.util.function.Supplier; import static com.moneymong.domain.agency.entity.QAgency.agency; +import static com.moneymong.domain.agency.entity.enums.AgencyType.GENERAL; @Repository @RequiredArgsConstructor @@ -51,6 +52,23 @@ public Page findByUniversityNameAndAgencyTypeByPaging(String universityN } + @Override + public List findByKeyword(String university, String keyword) { + return queryFactory + .selectFrom(agency) + .distinct() + .where( + agency.agencyType.eq(GENERAL) + .and(agency.agencyName.containsIgnoreCase(keyword)) + .or( + eqUniversityName(university) + .and(agency.agencyName.containsIgnoreCase(keyword)) + ) + ) + .fetch(); + } + + private JPAQuery getCountQuery(String universityName) { return queryFactory.selectFrom(agency) .where(eqUniversityName(universityName)); diff --git a/src/main/java/com/moneymong/domain/agency/service/AgencyService.java b/src/main/java/com/moneymong/domain/agency/service/AgencyService.java index 0cbda14..d81a018 100644 --- a/src/main/java/com/moneymong/domain/agency/service/AgencyService.java +++ b/src/main/java/com/moneymong/domain/agency/service/AgencyService.java @@ -91,6 +91,16 @@ public CreateAgencyResponse create(Long userId, CreateAgencyRequest request) { return new CreateAgencyResponse(agency.getId()); } + @Transactional + public List search(Long userId, String keyword) { + String university = getUniversityName(userId); + List agencies = agencyRepository.findByKeyword(university, keyword); + + return agencies.stream() + .map(AgencyResponse::from) + .toList(); + } + private String getUniversityName(Long userId) { return userUniversityRepository.findByUserId(userId) .map(UserUniversity::getUniversityName)