Skip to content

Commit 3500952

Browse files
committed
feat: 자유 게시글 및 고등학생 질문글 검색 기능 추가
1 parent 8d9a2b2 commit 3500952

File tree

8 files changed

+65
-14
lines changed

8 files changed

+65
-14
lines changed

gd-yd-api/src/main/java/com/gdyd/gdydapi/controller/board/PostController.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import com.gdyd.gdydapi.response.board.*;
77
import com.gdyd.gdydapi.response.common.LikeListResponse;
88
import com.gdyd.gdydapi.response.common.PageResponse;
9-
import com.gdyd.gdydapi.response.common.ScrapListResponse;
109
import com.gdyd.gdydapi.response.common.ReportResponse;
10+
import com.gdyd.gdydapi.response.common.ScrapListResponse;
1111
import com.gdyd.gdydapi.service.board.PostCommandService;
1212
import com.gdyd.gdydapi.service.board.PostQueryService;
1313
import io.swagger.v3.oas.annotations.Operation;
@@ -42,17 +42,21 @@ public ResponseEntity<SavePostResponse> createPost(@RequestBody SavePostReqeust
4242
@Parameter(name = "page", description = "페이지 번호")
4343
@Parameter(name = "size", description = "페이지 크기")
4444
@Parameter(name = "criteria", description = "정렬 기준 (createdAt | likeCount)")
45+
@Parameter(name = "keyword", description = "검색 키워드 (제목 또는 콘텐츠)")
4546
@GetMapping
4647
public ResponseEntity<PageResponse<GetPostSummaryResponse>> getPostList(
4748
@RequestParam(value = "page", defaultValue = "0") int page,
4849
@RequestParam(value = "size", defaultValue = "20") int size,
49-
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria
50+
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria,
51+
@RequestParam(value = "keyword", required = false) String keyword
5052
) {
5153
Pageable pageable = switch (criteria) {
5254
case "likeCount" -> PageRequest.of(page, size, Sort.by(criteria, DEFAULT_CRITERIA).descending());
5355
default -> PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
5456
};
55-
PageResponse<GetPostSummaryResponse> response = postQueryService.getPostList(pageable);
57+
PageResponse<GetPostSummaryResponse> response = (keyword != null && !keyword.isBlank())
58+
? postQueryService.searchPostListByKeyword(keyword, pageable)
59+
: postQueryService.getPostList(pageable);
5660
return ResponseEntity.ok(response);
5761
}
5862

@@ -66,7 +70,7 @@ public ResponseEntity<PageResponse<GetPostSummaryResponse>> getBestPostList(
6670
@RequestParam(value = "page", defaultValue = "0") int page,
6771
@RequestParam(value = "size", defaultValue = "20") int size,
6872
@RequestParam(value = "period", defaultValue = "1") int period,
69-
@RequestParam(value = "like", defaultValue = "10") int like
73+
@RequestParam(value = "like", defaultValue = "10") Long like
7074
) {
7175
LocalDateTime weeksAgo = LocalDateTime.now().minusWeeks(period);
7276
Pageable pageable = PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());

gd-yd-api/src/main/java/com/gdyd/gdydapi/controller/mentoring/MentoringController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,21 @@ public ResponseEntity<DeleteUniversityStudentAnswerResponse> deleteUniversityStu
7878
@Parameter(name = "page", description = "페이지 번호")
7979
@Parameter(name = "size", description = "페이지 크기")
8080
@Parameter(name = "criteria", description = "정렬 기준 (createdAt | likeCount)")
81+
@Parameter(name = "keyword", description = "검색 키워드 (제목 또는 콘텐츠)")
8182
@GetMapping("/high-school-student-questions")
8283
public ResponseEntity<PageResponse<HighSchoolStudentQuestionResponse>> getHighSchoolStudentQuestionList(
8384
@RequestParam(value = "page", defaultValue = "0") int page,
8485
@RequestParam(value = "size", defaultValue = "20") int size,
85-
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria
86+
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria,
87+
@RequestParam(value = "keyword", required = false) String keyword
8688
) {
8789
Pageable pageable = switch (criteria) {
8890
case "likeCount" -> PageRequest.of(page, size, Sort.by(criteria, DEFAULT_CRITERIA).descending());
8991
default -> PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
9092
};
91-
PageResponse<HighSchoolStudentQuestionResponse> response = mentoringQueryService.getHighSchoolStudentQuestions(pageable);
93+
PageResponse<HighSchoolStudentQuestionResponse> response = (keyword != null && !keyword.isBlank())
94+
? mentoringQueryService.searchHighSchoolStudentQuestionsByKeyword(keyword, pageable)
95+
: mentoringQueryService.getHighSchoolStudentQuestions(pageable);
9296
return ResponseEntity.ok(response);
9397
}
9498

gd-yd-api/src/main/java/com/gdyd/gdydapi/service/board/PostQueryService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ public PageResponse<GetPostSummaryResponse> getPostList(Pageable pageable) {
2525
return PageResponse.of(pages.getContent().stream().map(GetPostSummaryResponse::from).toList());
2626
}
2727

28+
public PageResponse<GetPostSummaryResponse> searchPostListByKeyword(String keyword, Pageable pageable) {
29+
Page<Post> postPage = postService.findByKeywordWithPriority(keyword, pageable);
30+
return PageResponse.of(postPage.getContent().stream().map(GetPostSummaryResponse::from).toList());
31+
}
32+
2833
public GetPostResponse getPostAndCommentsByPostId(Long postId) {
2934
Post post = postService.getPostById(postId);
3035
return GetPostResponse.from(post);
3136
}
3237

33-
public PageResponse<GetPostSummaryResponse> getBestPostList(int like, LocalDateTime weeksAgo, Pageable pageable) {
38+
public PageResponse<GetPostSummaryResponse> getBestPostList(Long like, LocalDateTime weeksAgo, Pageable pageable) {
3439
List<Post> posts = postService.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
3540
return PageResponse.of(posts.stream().map(GetPostSummaryResponse::from).toList());
3641
}

gd-yd-api/src/main/java/com/gdyd/gdydapi/service/mentoring/MentoringQueryService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public PageResponse<HighSchoolStudentQuestionResponse> getHighSchoolStudentQuest
3636
return PageResponse.of(pages.getContent().stream().map(HighSchoolStudentQuestionResponse::from).toList());
3737
}
3838

39+
/**
40+
* 고등학생 질문 검색
41+
*/
42+
public PageResponse<HighSchoolStudentQuestionResponse> searchHighSchoolStudentQuestionsByKeyword(String keyword, Pageable pageable) {
43+
Page<HighSchoolStudentQuestion> pages = highSchoolStudentQuestionService.findByKeywordWithPriority(keyword, pageable);
44+
return PageResponse.of(pages.getContent().stream().map(HighSchoolStudentQuestionResponse::from).toList());
45+
}
46+
3947
public PageResponse<HighSchoolStudentQuestionResponse> getBestHighSchoolStudentQuestions(Long like, LocalDateTime weeksAgo, Pageable pageable) {
4048
List<HighSchoolStudentQuestion> questions = highSchoolStudentQuestionService.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
4149
return PageResponse.of(questions.stream().map(HighSchoolStudentQuestionResponse::from).toList());
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.gdyd.gdydcore.repository.board;
22

33
import com.gdyd.gdydcore.domain.board.Post;
4+
import org.springframework.data.domain.Page;
45
import org.springframework.data.domain.Pageable;
56
import org.springframework.data.jpa.repository.JpaRepository;
7+
import org.springframework.data.jpa.repository.Query;
8+
import org.springframework.data.repository.query.Param;
69

710
import java.time.LocalDateTime;
811
import java.util.List;
@@ -11,5 +14,14 @@
1114
public interface PostRepository extends JpaRepository<Post, Long> {
1215
Optional<Post> findByIdAndMemberId(Long postId, Long memberId);
1316

14-
List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(int like, LocalDateTime weeksAgo, Pageable pageable);
17+
List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable);
18+
19+
@Query("""
20+
SELECT p FROM Post p
21+
WHERE p.title LIKE %:keyword% OR p.content LIKE %:keyword%
22+
ORDER BY
23+
CASE WHEN p.title LIKE %:keyword% THEN 0 ELSE 1 END,
24+
p.createdAt DESC
25+
""")
26+
Page<Post> findByKeywordWithPriority(@Param("keyword") String keyword, Pageable pageable);
1527
}

gd-yd-core/src/main/java/com/gdyd/gdydcore/repository/mentoring/HighSchoolStudentQuestionRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.gdyd.gdydcore.domain.member.Grade;
44
import com.gdyd.gdydcore.domain.member.UniversityMajorCategory;
55
import com.gdyd.gdydcore.domain.mentoring.HighSchoolStudentQuestion;
6+
import org.springframework.data.domain.Page;
67
import org.springframework.data.domain.Pageable;
78
import org.springframework.data.jpa.repository.JpaRepository;
89
import org.springframework.data.jpa.repository.Query;
@@ -28,5 +29,14 @@ List<HighSchoolStudentQuestion> findTopQuestionsByTag(
2829
@Param("cutoffDate") LocalDateTime cutoffDate,
2930
Pageable pageable);
3031

32+
@Query("""
33+
SELECT q FROM HighSchoolStudentQuestion q
34+
WHERE q.title LIKE %:keyword% OR q.question LIKE %:keyword%
35+
ORDER BY
36+
CASE WHEN q.title LIKE %:keyword% THEN 0 ELSE 1 END,
37+
q.createdAt DESC
38+
""")
39+
Page<HighSchoolStudentQuestion> findByKeywordWithPriority(@Param("keyword") String keyword, Pageable pageable);
40+
3141
List<HighSchoolStudentQuestion> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable);
3242
}

gd-yd-core/src/main/java/com/gdyd/gdydcore/service/board/PostService.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
public class PostService {
2020
private final PostRepository postRepository;
2121

22-
@Transactional
23-
public void savePost(Post post) {
24-
postRepository.save(post);
25-
}
26-
2722
public Page<Post> getPostListByPagination(Pageable pageable) {
2823
return postRepository.findAll(pageable);
2924
}
@@ -38,10 +33,19 @@ public Post getPostByIdAndMemberId(Long postId, Long memberId) {
3833
.orElseThrow(() -> new BusinessException(ErrorCode.UNAUTHORIZED_MEMBER));
3934
}
4035

41-
public List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(int like, LocalDateTime weeksAgo, Pageable pageable) {
36+
public List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable) {
4237
return postRepository.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
4338
}
4439

40+
public Page<Post> findByKeywordWithPriority(String keyword, Pageable pageable) {
41+
return postRepository.findByKeywordWithPriority(keyword, pageable);
42+
}
43+
44+
@Transactional
45+
public void savePost(Post post) {
46+
postRepository.save(post);
47+
}
48+
4549
@Transactional
4650
public void deletePost(Long postId) {
4751
postRepository.deleteById(postId);

gd-yd-core/src/main/java/com/gdyd/gdydcore/service/mentoring/HighSchoolStudentQuestionService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public Page<HighSchoolStudentQuestion> findHighSchoolStudentQuestionByPagination
3030
return highSchoolStudentQuestionRepository.findAll(pageable);
3131
}
3232

33+
public Page<HighSchoolStudentQuestion> findByKeywordWithPriority(String keyword, Pageable pageable) {
34+
return highSchoolStudentQuestionRepository.findByKeywordWithPriority(keyword, pageable);
35+
}
36+
3337
public List<HighSchoolStudentQuestion> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable) {
3438
return highSchoolStudentQuestionRepository.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
3539
}

0 commit comments

Comments
 (0)