-
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.
Merge pull request #28 from team-acode/feat/search
feat: 검색
- Loading branch information
Showing
15 changed files
with
324 additions
and
17 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
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
45 changes: 45 additions & 0 deletions
45
src/main/java/server/acode/domain/fragrance/controller/SearchController.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,45 @@ | ||
package server.acode.domain.fragrance.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import server.acode.domain.family.dto.response.PageableResponse; | ||
import server.acode.domain.fragrance.dto.request.SearchCond; | ||
import server.acode.domain.fragrance.dto.response.BrandInfo; | ||
import server.acode.domain.fragrance.dto.response.FragranceInfo; | ||
import server.acode.domain.fragrance.dto.response.SearchBrandResponse; | ||
import server.acode.domain.fragrance.service.SearchService; | ||
import server.acode.global.common.PageRequest; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/search") | ||
@RequiredArgsConstructor | ||
@Tag(name = "Search", description = "검색 API") | ||
public class SearchController { | ||
private final SearchService searchService; | ||
|
||
@Operation(summary = "브랜드 검색", | ||
description = "search 검색어: 필수\n\n" + | ||
"검색어 없는 경우 `400 SEARCH_NOT_FOUND`\n\n" + | ||
"페이지는 파라미터 없을 시 기본 page = 1, size = 10입니다") | ||
@GetMapping("/brand") | ||
public PageableResponse<BrandInfo> searchBrand(@RequestParam("search") String search, PageRequest pageRequest) { | ||
return searchService.searchBrand(search, pageRequest); | ||
} | ||
|
||
@Operation(summary = "향수 검색", | ||
description = "검색어 외에는 필요한 값만 파라미터에 넣으면 됩니다 \n\n" + | ||
"search 검색어: 필수\n\n" + | ||
" 검색어 없는 경우 `400 SEARCH_NOT_FOUND`\n\n" + | ||
"family 계열: 한글로 넣어주세요 ex. `플로럴`\n\n" + | ||
" * 계열 두 개 검색 시에는 두 계열 사이 공백 한 칸 (url 상으로는 %20) 넣어주세요 ex. `플로럴 프루티` \n\n" + | ||
"페이지는 파라미터 없을 시 기본 page = 1, size = 10입니다") | ||
@GetMapping("/fragrance") | ||
public PageableResponse<FragranceInfo> searchFragrance(SearchCond cond, PageRequest pageRequest) { | ||
return searchService.searchFragrance(cond, pageRequest); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/server/acode/domain/fragrance/dto/request/SearchCond.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 server.acode.domain.fragrance.dto.request; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class SearchCond { | ||
private String search; | ||
private String family; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/server/acode/domain/fragrance/dto/response/BrandInfo.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 server.acode.domain.fragrance.dto.response; | ||
|
||
import com.querydsl.core.annotations.QueryProjection; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class BrandInfo { | ||
private Long brandId; | ||
private String korName; | ||
private String roundImg; | ||
|
||
@QueryProjection | ||
public BrandInfo(Long brandId, String korName, String roundImg) { | ||
this.brandId = brandId; | ||
this.korName = korName; | ||
this.roundImg = roundImg; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/server/acode/domain/fragrance/dto/response/SearchBrandResponse.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,19 @@ | ||
package server.acode.domain.fragrance.dto.response; | ||
|
||
import com.querydsl.core.annotations.QueryProjection; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class SearchBrandResponse { | ||
private List<BrandInfo> brandList; | ||
|
||
public SearchBrandResponse(List<BrandInfo> brandList) { | ||
this.brandList = brandList; | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
src/main/java/server/acode/domain/fragrance/repository/BrandRepository.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
11 changes: 11 additions & 0 deletions
11
src/main/java/server/acode/domain/fragrance/repository/BrandRepositoryCustom.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,11 @@ | ||
package server.acode.domain.fragrance.repository; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Repository; | ||
import server.acode.domain.fragrance.dto.response.BrandInfo; | ||
|
||
@Repository | ||
public interface BrandRepositoryCustom { | ||
Page<BrandInfo> searchBrand(String search, Pageable pageable); | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/server/acode/domain/fragrance/repository/BrandRepositoryImpl.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,57 @@ | ||
package server.acode.domain.fragrance.repository; | ||
|
||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQuery; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
import org.springframework.stereotype.Repository; | ||
import server.acode.domain.fragrance.dto.response.BrandInfo; | ||
import server.acode.domain.fragrance.dto.response.QBrandInfo; | ||
|
||
import java.util.List; | ||
|
||
import static server.acode.domain.fragrance.entity.QBrand.*; | ||
|
||
@Repository | ||
public class BrandRepositoryImpl implements BrandRepositoryCustom { | ||
private final JPAQueryFactory queryFactory; | ||
|
||
public BrandRepositoryImpl(EntityManager em) { | ||
this.queryFactory = new JPAQueryFactory(em); | ||
} | ||
|
||
|
||
@Override | ||
public Page<BrandInfo> searchBrand(String search, Pageable pageable) { | ||
List<BrandInfo> contents = queryFactory | ||
.select(new QBrandInfo( | ||
brand.id.as("brandId"), | ||
brand.korName, | ||
brand.roundImg | ||
)) | ||
.from(brand) | ||
.where( | ||
brandNameContains(search) | ||
) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
JPAQuery<Long> countQuery = queryFactory | ||
.select(brand.count()) | ||
.from(brand) | ||
.where( | ||
brandNameContains(search) | ||
); | ||
|
||
return PageableExecutionUtils.getPage(contents, pageable, countQuery::fetchOne); | ||
} | ||
|
||
private BooleanExpression brandNameContains(String search) { | ||
return brand.korName.containsIgnoreCase(search) | ||
.or(brand.engName.containsIgnoreCase(search)); | ||
} | ||
} |
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.