-
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.
feat : create SearchController, Service
- Loading branch information
1 parent
ebd13a4
commit f45d1be
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/com/example/reddiserver/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,33 @@ | ||
package com.example.reddiserver.controller; | ||
|
||
import com.example.reddiserver.common.ApiResponse; | ||
import com.example.reddiserver.dto.post.response.PostResponseDto; | ||
import com.example.reddiserver.dto.search.response.SearchResponseDto; | ||
import com.example.reddiserver.service.NotionService; | ||
import com.example.reddiserver.service.PostService; | ||
import com.example.reddiserver.service.SearchService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.domain.PageRequest; | ||
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; | ||
|
||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/search") | ||
@Slf4j | ||
public class SearchController { | ||
|
||
private final SearchService searchService; | ||
|
||
@Operation(summary = "검색") | ||
@GetMapping("/") | ||
public ApiResponse<SearchResponseDto> getSearchList(@RequestParam String keyword, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { | ||
SearchResponseDto searchResponseDtoList = searchService.getSearchList(keyword, PageRequest.of(page, size)); | ||
return ApiResponse.successResponse(searchResponseDtoList, "검색 완료"); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/example/reddiserver/service/SearchService.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,36 @@ | ||
package com.example.reddiserver.service; | ||
|
||
import com.example.reddiserver.dto.search.response.SearchBrands; | ||
import com.example.reddiserver.dto.search.response.SearchPosts; | ||
import com.example.reddiserver.dto.search.response.SearchResponseDto; | ||
import com.example.reddiserver.repository.BrandRepository; | ||
import com.example.reddiserver.repository.PostRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class SearchService { | ||
private final BrandRepository brandRepository; | ||
private final PostRepository postRepository; | ||
|
||
public SearchResponseDto getSearchList(String keyword, Pageable pageable) { | ||
List<SearchPosts> posts = postRepository.findByTitleContainingOrContentContaining("%"+keyword+"%", pageable).getContent().stream() | ||
.map(SearchPosts::convertPostToSearchPosts) | ||
.collect(Collectors.toList()); | ||
List<SearchBrands> brands = brandRepository.findByNameContainingOrContentContaining("%"+keyword+"%", pageable).getContent().stream() | ||
.map(SearchBrands::convertBrandToSearchBrands) | ||
.collect(Collectors.toList()); | ||
|
||
return SearchResponseDto.builder() | ||
.posts(posts) | ||
.brands(brands) | ||
.build(); | ||
} | ||
} |