forked from EveryUniv/next-student-council-backend
-
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 #10 from gutanbug/feat/search
feat : 모든 게시판 검색 기능 추가
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/com/dku/council/domain/post/controller/PostSearchController.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,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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/dku/council/domain/post/model/dto/response/ResponseSingleSearchPost.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,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(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/dku/council/domain/post/service/post/PostSearchService.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,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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/dku/council/domain/post/service/post/SummarizedPostSearchDto.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,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; | ||
|
||
} |