Skip to content

Commit

Permalink
Merge pull request #10 from gutanbug/feat/search
Browse files Browse the repository at this point in the history
feat : 모든 게시판 검색 기능 추가
  • Loading branch information
gutanbug authored Apr 15, 2024
2 parents 775c99b + 9bb0d69 commit d901aed
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.dku.council.domain.post.controller;

import com.dku.council.domain.post.service.post.PostSearchService;
import com.dku.council.domain.post.service.post.SummarizedPostSearchDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
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
@Tag(name = "게시글 검색", description = "게시글 검색 관련 api")
@RequestMapping("/post/search")
public class PostSearchController {

private final PostSearchService postSearchService;

/**
* 모든 게시글 검색
*
* @param keyword 검색어
**/
@GetMapping
public SummarizedPostSearchDto searchPost(@RequestParam(required = false) String keyword,
@ParameterObject Pageable pageable) {
return postSearchService.searchPost(keyword, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dku.council.domain.post.model.dto.response;

import com.dku.council.domain.post.model.entity.Post;
import lombok.Getter;

@Getter
public class ResponseSingleSearchPost {

private final Long id;

private final String title;

public ResponseSingleSearchPost(Post post) {
this.id = post.getId();
this.title = post.getTitle();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.dku.council.domain.post.service.post;

import com.dku.council.domain.post.model.dto.response.ResponseSingleSearchPost;
import com.dku.council.domain.post.model.entity.posttype.*;
import com.dku.council.domain.post.repository.post.*;
import com.dku.council.domain.post.repository.spec.PostSpec;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class PostSearchService {

private final PetitionRepository petitionRepository;
private final NoticeRepository noticeRepository;
private final CoalitionRepository coalitionRepository;
private final ConferenceRepository conferenceRepository;
private final RuleRepository ruleRepository;

/**
* 모든 게시글 검색
*/
public SummarizedPostSearchDto searchPost(String keyword, Pageable pageable) {
Page<Notice> notices = getNoticeLists(keyword, pageable);
Page<Coalition> coalitions = getCoalitionLists(keyword, pageable);
Page<Petition> petitions = getPetitionLists(keyword, pageable);
Page<Conference> conferences = getConferencesLists(keyword, pageable);
Page<Rule> rules = getRulesLists(keyword, pageable);
return new SummarizedPostSearchDto(
notices.stream().map(ResponseSingleSearchPost::new).collect(Collectors.toList()),
coalitions.stream().map(ResponseSingleSearchPost::new).collect(Collectors.toList()),
petitions.stream().map(ResponseSingleSearchPost::new).collect(Collectors.toList()),
conferences.stream().map(ResponseSingleSearchPost::new).collect(Collectors.toList()),
rules.stream().map(ResponseSingleSearchPost::new).collect(Collectors.toList())
);
}

private Page<Notice> getNoticeLists(String keyword, Pageable pageable) {
Specification<Notice> spec = PostSpec.withTitleOrBody(keyword);
spec = spec.and(PostSpec.withActive());
return noticeRepository.findAll(spec, pageable);
}

private Page<Coalition> getCoalitionLists(String keyword, Pageable pageable) {
Specification<Coalition> spec = PostSpec.withTitleOrBody(keyword);
spec = spec.and(PostSpec.withActive());
return coalitionRepository.findAll(spec,pageable);
}

private Page<Petition> getPetitionLists(String keyword, Pageable pageable) {
Specification<Petition> spec = PostSpec.withTitleOrBody(keyword);
spec = spec.and(PostSpec.withActive());
return petitionRepository.findAll(spec,pageable);
}

private Page<Conference> getConferencesLists(String keyword, Pageable pageable) {
Specification<Conference> spec = PostSpec.withTitleOrBody(keyword);
spec = spec.and(PostSpec.withActive());
return conferenceRepository.findAll(spec,pageable);
}

private Page<Rule> getRulesLists(String keyword, Pageable pageable) {
Specification<Rule> spec = PostSpec.withTitleOrBody(keyword);
spec = spec.and(PostSpec.withActive());
return ruleRepository.findAll(spec,pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dku.council.domain.post.service.post;

import com.dku.council.domain.post.model.dto.response.ResponseSingleSearchPost;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Getter
@RequiredArgsConstructor
public class SummarizedPostSearchDto {

private final List<ResponseSingleSearchPost> notices;

private final List<ResponseSingleSearchPost> coalitions;

private final List<ResponseSingleSearchPost> petitions;

private final List<ResponseSingleSearchPost> conferences;

private final List<ResponseSingleSearchPost> rules;

}

0 comments on commit d901aed

Please sign in to comment.