Skip to content

Commit

Permalink
feat : create SearchController, Service
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungGyo-00 committed Jan 27, 2024
1 parent ebd13a4 commit f45d1be
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
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 src/main/java/com/example/reddiserver/service/SearchService.java
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();
}
}

0 comments on commit f45d1be

Please sign in to comment.