Skip to content

자유 게시글 및 고등학생 질문글 검색 기능 추가 #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.gdyd.gdydapi.response.board.*;
import com.gdyd.gdydapi.response.common.LikeListResponse;
import com.gdyd.gdydapi.response.common.PageResponse;
import com.gdyd.gdydapi.response.common.ScrapListResponse;
import com.gdyd.gdydapi.response.common.ReportResponse;
import com.gdyd.gdydapi.response.common.ScrapListResponse;
import com.gdyd.gdydapi.service.board.PostCommandService;
import com.gdyd.gdydapi.service.board.PostQueryService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -42,17 +42,21 @@ public ResponseEntity<SavePostResponse> createPost(@RequestBody SavePostReqeust
@Parameter(name = "page", description = "페이지 번호")
@Parameter(name = "size", description = "페이지 크기")
@Parameter(name = "criteria", description = "정렬 기준 (createdAt | likeCount)")
@Parameter(name = "keyword", description = "검색 키워드 (제목 또는 콘텐츠)")
@GetMapping
public ResponseEntity<PageResponse<GetPostSummaryResponse>> getPostList(
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "20") int size,
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria,
@RequestParam(value = "keyword", required = false) String keyword
) {
Pageable pageable = switch (criteria) {
case "likeCount" -> PageRequest.of(page, size, Sort.by(criteria, DEFAULT_CRITERIA).descending());
default -> PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
};
PageResponse<GetPostSummaryResponse> response = postQueryService.getPostList(pageable);
PageResponse<GetPostSummaryResponse> response = (keyword != null && !keyword.isBlank())
? postQueryService.searchPostListByKeyword(keyword, pageable)
: postQueryService.getPostList(pageable);
return ResponseEntity.ok(response);
}

Expand All @@ -66,7 +70,7 @@ public ResponseEntity<PageResponse<GetPostSummaryResponse>> getBestPostList(
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "20") int size,
@RequestParam(value = "period", defaultValue = "1") int period,
@RequestParam(value = "like", defaultValue = "10") int like
@RequestParam(value = "like", defaultValue = "10") Long like
) {
LocalDateTime weeksAgo = LocalDateTime.now().minusWeeks(period);
Pageable pageable = PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,21 @@ public ResponseEntity<DeleteUniversityStudentAnswerResponse> deleteUniversityStu
@Parameter(name = "page", description = "페이지 번호")
@Parameter(name = "size", description = "페이지 크기")
@Parameter(name = "criteria", description = "정렬 기준 (createdAt | likeCount)")
@Parameter(name = "keyword", description = "검색 키워드 (제목 또는 콘텐츠)")
@GetMapping("/high-school-student-questions")
public ResponseEntity<PageResponse<HighSchoolStudentQuestionResponse>> getHighSchoolStudentQuestionList(
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "20") int size,
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria,
@RequestParam(value = "keyword", required = false) String keyword
) {
Pageable pageable = switch (criteria) {
case "likeCount" -> PageRequest.of(page, size, Sort.by(criteria, DEFAULT_CRITERIA).descending());
default -> PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
};
PageResponse<HighSchoolStudentQuestionResponse> response = mentoringQueryService.getHighSchoolStudentQuestions(pageable);
PageResponse<HighSchoolStudentQuestionResponse> response = (keyword != null && !keyword.isBlank())
? mentoringQueryService.searchHighSchoolStudentQuestionsByKeyword(keyword, pageable)
: mentoringQueryService.getHighSchoolStudentQuestions(pageable);
return ResponseEntity.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ public PageResponse<GetPostSummaryResponse> getPostList(Pageable pageable) {
return PageResponse.of(pages.getContent().stream().map(GetPostSummaryResponse::from).toList());
}

public PageResponse<GetPostSummaryResponse> searchPostListByKeyword(String keyword, Pageable pageable) {
Page<Post> postPage = postService.findByKeywordWithPriority(keyword, pageable);
return PageResponse.of(postPage.getContent().stream().map(GetPostSummaryResponse::from).toList());
}

public GetPostResponse getPostAndCommentsByPostId(Long postId) {
Post post = postService.getPostById(postId);
return GetPostResponse.from(post);
}

public PageResponse<GetPostSummaryResponse> getBestPostList(int like, LocalDateTime weeksAgo, Pageable pageable) {
public PageResponse<GetPostSummaryResponse> getBestPostList(Long like, LocalDateTime weeksAgo, Pageable pageable) {
List<Post> posts = postService.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
return PageResponse.of(posts.stream().map(GetPostSummaryResponse::from).toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public PageResponse<HighSchoolStudentQuestionResponse> getHighSchoolStudentQuest
return PageResponse.of(pages.getContent().stream().map(HighSchoolStudentQuestionResponse::from).toList());
}

/**
* 고등학생 질문 검색
*/
public PageResponse<HighSchoolStudentQuestionResponse> searchHighSchoolStudentQuestionsByKeyword(String keyword, Pageable pageable) {
Page<HighSchoolStudentQuestion> pages = highSchoolStudentQuestionService.findByKeywordWithPriority(keyword, pageable);
return PageResponse.of(pages.getContent().stream().map(HighSchoolStudentQuestionResponse::from).toList());
}

public PageResponse<HighSchoolStudentQuestionResponse> getBestHighSchoolStudentQuestions(Long like, LocalDateTime weeksAgo, Pageable pageable) {
List<HighSchoolStudentQuestion> questions = highSchoolStudentQuestionService.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
return PageResponse.of(questions.stream().map(HighSchoolStudentQuestionResponse::from).toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.gdyd.gdydcore.repository.board;

import com.gdyd.gdydcore.domain.board.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(int like, LocalDateTime weeksAgo, Pageable pageable);
List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable);

@Query("""
SELECT p FROM Post p
WHERE p.title LIKE %:keyword% OR p.content LIKE %:keyword%
ORDER BY
CASE WHEN p.title LIKE %:keyword% THEN 0 ELSE 1 END,
p.createdAt DESC
""")
Page<Post> findByKeywordWithPriority(@Param("keyword") String keyword, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gdyd.gdydcore.domain.member.Grade;
import com.gdyd.gdydcore.domain.member.UniversityMajorCategory;
import com.gdyd.gdydcore.domain.mentoring.HighSchoolStudentQuestion;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -28,5 +29,14 @@ List<HighSchoolStudentQuestion> findTopQuestionsByTag(
@Param("cutoffDate") LocalDateTime cutoffDate,
Pageable pageable);

@Query("""
SELECT q FROM HighSchoolStudentQuestion q
WHERE q.title LIKE %:keyword% OR q.question LIKE %:keyword%
ORDER BY
CASE WHEN q.title LIKE %:keyword% THEN 0 ELSE 1 END,
q.createdAt DESC
""")
Page<HighSchoolStudentQuestion> findByKeywordWithPriority(@Param("keyword") String keyword, Pageable pageable);

List<HighSchoolStudentQuestion> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
public class PostService {
private final PostRepository postRepository;

@Transactional
public void savePost(Post post) {
postRepository.save(post);
}

public Page<Post> getPostListByPagination(Pageable pageable) {
return postRepository.findAll(pageable);
}
Expand All @@ -38,10 +33,19 @@ public Post getPostByIdAndMemberId(Long postId, Long memberId) {
.orElseThrow(() -> new BusinessException(ErrorCode.UNAUTHORIZED_MEMBER));
}

public List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(int like, LocalDateTime weeksAgo, Pageable pageable) {
public List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable) {
return postRepository.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
}

public Page<Post> findByKeywordWithPriority(String keyword, Pageable pageable) {
return postRepository.findByKeywordWithPriority(keyword, pageable);
}

@Transactional
public void savePost(Post post) {
postRepository.save(post);
}

@Transactional
public void deletePost(Long postId) {
postRepository.deleteById(postId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public Page<HighSchoolStudentQuestion> findHighSchoolStudentQuestionByPagination
return highSchoolStudentQuestionRepository.findAll(pageable);
}

public Page<HighSchoolStudentQuestion> findByKeywordWithPriority(String keyword, Pageable pageable) {
return highSchoolStudentQuestionRepository.findByKeywordWithPriority(keyword, pageable);
}

public List<HighSchoolStudentQuestion> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable) {
return highSchoolStudentQuestionRepository.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
}
Expand Down
Loading