searchKeyword(@RequestParam(required = false, defaultValue = "") @Nullable String keyword, @RequestParam(required = false, defaultValue = "") @Nullable String loan, @Request"> searchKeyword(@RequestParam(required = false, defaultValue = "") @Nullable String keyword, @RequestParam(required = false, defaultValue = "") @Nullable String loan, @Request"> searchKeyword(@RequestParam(required = false, defaultValue = "") @Nullable String keyword, @RequestParam(required = false, defaultValue = "") @Nullable String loan, @Request">
package com.example.creditmarket.controller;
import com.example.creditmarket.dto.MainListResponse;
import com.example.creditmarket.service.SearchService;
import lombok.RequiredArgsConstructor;
import org.springframework.lang.Nullable;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequiredArgsConstructor
@Api(tags = {"검색 서비스"}, description = "키워드와 카테고리 검색을 담당합니다.")
public class SearchController {
private final SearchService searchService;
@GetMapping("/search/results")
public List<MainListResponse> searchKeyword(@RequestParam(required = false, defaultValue = "") @Nullable String keyword,
@RequestParam(required = false, defaultValue = "") @Nullable String loan,
@RequestParam(required = false, defaultValue = "") @Nullable String age,
@RequestParam(required = false, defaultValue = "") @Nullable String gender,
@RequestParam(required = false, defaultValue = "") @Nullable String interest,
@RequestParam(required = false, defaultValue = "0") @Nullable Double rate,
@RequestParam(required = false, defaultValue = "1") int page
) {
return searchService.searchResult(keyword.trim(), loan.trim(), age.trim(), gender.trim(), interest.trim(), rate, (page-1));
}
}
package com.example.creditmarket.service;
import com.example.creditmarket.dto.MainListResponse;
import com.example.creditmarket.entity.EntityOption;
import com.example.creditmarket.repository.OptionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@RequiredArgsConstructor
public class SearchService {
private final OptionRepository optionRepository;
/**
* 키워드와 카테고리 검색 서비스
* @param keyword : 검색 키워드
* @param loan : 대출종류
* @param age : 나이
* @param gender : 성별
* @param interest : 금리유형
* @param rate : 평균금리
* @return
*/
public List<MainListResponse> searchResult(String keyword, String loan, String age,
String gender, String interest, Double rate,
int page) {
Pageable pageable = PageRequest.of(page, 10);
if (rate == 0 || rate == null) rate = 20.0;
Page<EntityOption> result = optionRepository.search(loan, age, gender, interest, rate, keyword, pageable);
List<EntityOption> lists = result.getContent();
if (lists != null || lists.size() != 0) {
List<MainListResponse> mainListResponseList = new ArrayList<>();
for (int i = 0; i < lists.size(); i++) {
mainListResponseList.add(i, new MainListResponse(lists.get(i).getEntityFProduct().getFproduct_company_name(),
lists.get(i).getEntityFProduct().getFproduct_name(),
lists.get(i).getEntityFProduct().getFproduct_credit_product_type_name(),
lists.get(i).getOptions_crdt_grad_avg()
));
}
return mainListResponseList;
}
return null;
}
}
package com.example.creditmarket.repository;
import com.example.creditmarket.entity.EntityOption;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface OptionRepository extends JpaRepository<EntityOption, Long> {
@Query(value = "SELECT O " +
"FROM EntityOption O " +
"JOIN O.entityFProduct P " +
"WHERE P.fproduct_credit_product_type_name LIKE %:loan% " +
"AND CAST(P.fproduct_minimum_age AS string) LIKE %:age% " +
"AND P.fproduct_target_gender LIKE %:gender% " +
"AND O.options_interest_type LIKE %:interest% " +
"AND O.options_crdt_grad_avg <= :rate " +
"AND (" +
" P.fproduct_company_name LIKE %:keyword% " +
" OR P.fproduct_name LIKE %:keyword% " +
" OR P.fproduct_credit_product_type_name LIKE %:keyword% " +
" OR O.options_interest_type LIKE %:keyword% " +
") " +
"ORDER BY O.options_crdt_grad_avg ASC "
)
Page<EntityOption> search(@Param("loan") String loan,
@Param("age") String age,
@Param("gender") String gender,
@Param("interest") String interest,
@Param("rate") Double rate,
@Param("keyword") String keyword,
Pageable pageable);
}
package com.example.creditmarket.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MainListResponse {
private String companyName; // 은행
private String productName; // 대출 상품
private String productTypeName; // 대출 종류
private Double interestRateAvg; // 평균 금리
}