Skip to content

Commit 11cac63

Browse files
authored
Merge pull request #81 from GD-YD/refactor/Post-Sorting-API
자유게시글, 질문글 목록 정렬 방식과 베스트글 리펙토링
2 parents d1f81d4 + 76ade66 commit 11cac63

File tree

9 files changed

+32
-20
lines changed

9 files changed

+32
-20
lines changed

GD-YD-BE-Config

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
public class PostController {
3030
private final PostCommandService postCommandService;
3131
private final PostQueryService postQueryService;
32+
private static final String DEFAULT_CRITERIA = "createdAt";
3233

3334
@Operation(summary = "Post 생성 API", description = "Post를 생성하는 API")
3435
@PostMapping
@@ -48,24 +49,28 @@ public ResponseEntity<PageResponse<GetPostSummaryResponse>> getPostList(
4849
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria
4950
) {
5051
Pageable pageable = switch (criteria) {
51-
case "likeCount" -> PageRequest.of(page, size, Sort.by("likeCount", "createdAt").descending());
52-
default -> PageRequest.of(page, size, Sort.by("createdAt"));
52+
case "likeCount" -> PageRequest.of(page, size, Sort.by(criteria, DEFAULT_CRITERIA).descending());
53+
default -> PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
5354
};
5455
PageResponse<GetPostSummaryResponse> response = postQueryService.getPostList(pageable);
5556
return ResponseEntity.ok(response);
5657
}
5758

5859
@Operation(summary = "인기 Post 조회 API", description = "특정 기간 내 인기 게시글 n개를 불러오는 API")
60+
@Parameter(name = "page", description = "페이지 번호")
5961
@Parameter(name = "size", description = "인기글 개수")
6062
@Parameter(name = "period", description = "인기글 기준 기간 (주 단위: 값이 1일 경우 1주일 내)")
63+
@Parameter(name = "like", description = "인기글 기준 좋아요 수")
6164
@GetMapping("/best")
6265
public ResponseEntity<PageResponse<GetPostSummaryResponse>> getBestPostList(
66+
@RequestParam(value = "page", defaultValue = "0") int page,
6367
@RequestParam(value = "size", defaultValue = "20") int size,
64-
@RequestParam(value = "period", defaultValue = "1") int period
68+
@RequestParam(value = "period", defaultValue = "1") int period,
69+
@RequestParam(value = "like", defaultValue = "10") int like
6570
) {
6671
LocalDateTime weeksAgo = LocalDateTime.now().minusWeeks(period);
67-
Pageable pageable = PageRequest.of(0, size, Sort.by("likeCount", "createdAt").descending());
68-
PageResponse<GetPostSummaryResponse> response = postQueryService.getBestPostList(weeksAgo, pageable);
72+
Pageable pageable = PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
73+
PageResponse<GetPostSummaryResponse> response = postQueryService.getBestPostList(like, weeksAgo, pageable);
6974
return ResponseEntity.ok(response);
7075
}
7176

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public class MentoringController {
3131
private final MentoringCommandService mentoringCommandService;
3232
private final MentoringQueryService mentoringQueryService;
33+
private static final String DEFAULT_CRITERIA = "createdAt";
3334

3435
@Operation(summary = "고등학생 질문 등록 API", description = "고등학생이 질문을 등록하는 API")
3536
@PostMapping("/high-school-student-question")
@@ -62,24 +63,28 @@ public ResponseEntity<PageResponse<HighSchoolStudentQuestionResponse>> getHighSc
6263
@RequestParam(value = "criteria", defaultValue = "createdAt") String criteria
6364
) {
6465
Pageable pageable = switch (criteria) {
65-
case "likeCount" -> PageRequest.of(page, size, Sort.by("likeCount", "createdAt").descending());
66-
default -> PageRequest.of(page, size, Sort.by("createdAt"));
66+
case "likeCount" -> PageRequest.of(page, size, Sort.by(criteria, DEFAULT_CRITERIA).descending());
67+
default -> PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
6768
};
6869
PageResponse<HighSchoolStudentQuestionResponse> response = mentoringQueryService.getHighSchoolStudentQuestions(pageable);
6970
return ResponseEntity.ok(response);
7071
}
7172

7273
@Operation(summary = "인기 질문글 조회 API", description = "특정 기간 내 인기 질문글 n개를 불러오는 API")
74+
@Parameter(name = "page", description = "페이지 번호")
7375
@Parameter(name = "size", description = "인기글 개수")
7476
@Parameter(name = "period", description = "인기글 기준 기간 (주 단위: 값이 1일 경우 1주일 내)")
77+
@Parameter(name = "like", description = "인기글 기준 좋아요 수")
7578
@GetMapping("/high-school-student-questions/best")
7679
public ResponseEntity<PageResponse<HighSchoolStudentQuestionResponse>> getBestHighSchoolStudentQuestionList(
80+
@RequestParam(value = "page", defaultValue = "0") int page,
7781
@RequestParam(value = "size", defaultValue = "20") int size,
78-
@RequestParam(value = "period", defaultValue = "1") int period
82+
@RequestParam(value = "period", defaultValue = "1") int period,
83+
@RequestParam(value = "like", defaultValue = "10") Long like
7984
) {
8085
LocalDateTime weeksAgo = LocalDateTime.now().minusWeeks(period);
81-
Pageable pageable = PageRequest.of(0, size, Sort.by("likeCount", "createdAt").descending());
82-
PageResponse<HighSchoolStudentQuestionResponse> response = mentoringQueryService.getBestHighSchoolStudentQuestions(weeksAgo, pageable);
86+
Pageable pageable = PageRequest.of(page, size, Sort.by(DEFAULT_CRITERIA).descending());
87+
PageResponse<HighSchoolStudentQuestionResponse> response = mentoringQueryService.getBestHighSchoolStudentQuestions(like, weeksAgo, pageable);
8388
return ResponseEntity.ok(response);
8489
}
8590

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public GetPostResponse getPostAndCommentsByPostId(Long postId) {
3030
return GetPostResponse.from(post);
3131
}
3232

33-
public PageResponse<GetPostSummaryResponse> getBestPostList(LocalDateTime weeksAgo, Pageable pageable) {
34-
List<Post> posts = postService.findAllByCreatedAtIsAfter(weeksAgo, pageable);
33+
public PageResponse<GetPostSummaryResponse> getBestPostList(int like, LocalDateTime weeksAgo, Pageable pageable) {
34+
List<Post> posts = postService.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
3535
return PageResponse.of(posts.stream().map(GetPostSummaryResponse::from).toList());
3636
}
3737
}

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

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

39-
public PageResponse<HighSchoolStudentQuestionResponse> getBestHighSchoolStudentQuestions(LocalDateTime weeksAgo, Pageable pageable) {
40-
List<HighSchoolStudentQuestion> questions = highSchoolStudentQuestionService.findAllByCreatedAtIsAfter(weeksAgo, pageable);
39+
public PageResponse<HighSchoolStudentQuestionResponse> getBestHighSchoolStudentQuestions(Long like, LocalDateTime weeksAgo, Pageable pageable) {
40+
List<HighSchoolStudentQuestion> questions = highSchoolStudentQuestionService.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
4141
return PageResponse.of(questions.stream().map(HighSchoolStudentQuestionResponse::from).toList());
4242
}
4343

gd-yd-core/src/main/java/com/gdyd/gdydcore/repository/board/PostRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
public interface PostRepository extends JpaRepository<Post, Long> {
1212
Optional<Post> findByIdAndMemberId(Long postId, Long memberId);
1313

14-
List<Post> findAllByCreatedAtIsAfter(LocalDateTime weeksAgo, Pageable pageable);
14+
List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(int like, LocalDateTime weeksAgo, Pageable pageable);
1515
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ List<HighSchoolStudentQuestion> findTopQuestionsByTag(
2727
@Param("universityGradeTag") Grade universityGradeTag,
2828
@Param("cutoffDate") LocalDateTime cutoffDate,
2929
Pageable pageable);
30+
31+
List<HighSchoolStudentQuestion> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable);
3032
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public Post getPostByIdAndMemberId(Long postId, Long memberId) {
3838
.orElseThrow(() -> new BusinessException(ErrorCode.UNAUTHORIZED_MEMBER));
3939
}
4040

41-
public List<Post> findAllByCreatedAtIsAfter(LocalDateTime weeksAgo, Pageable pageable) {
42-
return postRepository.findAllByCreatedAtIsAfter(weeksAgo, pageable);
41+
public List<Post> findByLikeCountGreaterThanEqualAndCreatedAtAfter(int like, LocalDateTime weeksAgo, Pageable pageable) {
42+
return postRepository.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
4343
}
4444

4545
@Transactional

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

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

33-
public List<HighSchoolStudentQuestion> findAllByCreatedAtIsAfter(LocalDateTime weeksAgo, Pageable pageable) {
34-
return highSchoolStudentQuestionRepository.findAllByCreatedAtIsAfter(weeksAgo, pageable);
33+
public List<HighSchoolStudentQuestion> findByLikeCountGreaterThanEqualAndCreatedAtAfter(Long like, LocalDateTime weeksAgo, Pageable pageable) {
34+
return highSchoolStudentQuestionRepository.findByLikeCountGreaterThanEqualAndCreatedAtAfter(like, weeksAgo, pageable);
3535
}
3636

3737
public List<HighSchoolStudentQuestion> findTopQuestionsByScore(String universityNameTag, UniversityMajorCategory universityMajorTag, Grade universityGradeTag, LocalDateTime cutoffDate, Pageable pageable) {

0 commit comments

Comments
 (0)