Skip to content
This repository was archived by the owner on Jan 11, 2026. It is now read-only.

Commit 5e1301c

Browse files
authored
[SPOT-262][MERGE] 스터디 게시글 목록 조회 시 전체 페이지 수 반환
[MODIFY] 스터디 게시글 목록 조회 시 전체 페이지 수 반환
2 parents 63cbb20 + 04ed66a commit 5e1301c

6 files changed

Lines changed: 59 additions & 33 deletions

File tree

src/main/java/com/example/spot/repository/StudyPostRepository.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.spot.repository;
22

3+
import com.example.spot.domain.enums.Theme;
34
import com.example.spot.domain.study.StudyPost;
45

56
import java.util.Arrays;
@@ -20,5 +21,9 @@ public interface StudyPostRepository extends JpaRepository<StudyPost, Long>, Stu
2021

2122
Optional<StudyPost> findByIdAndMemberId(Long postId, Long memberId);
2223

23-
List<StudyPost> findAllByStudyIdAndIsAnnouncement(Long studyId, Boolean isAnnouncement, PageRequest pageRequest);
24+
Long countByStudyId(Long studyId);
25+
26+
Long countByStudyIdAndIsAnnouncement(Long studyId, Boolean aTrue);
27+
28+
Long countByStudyIdAndTheme(Long studyId, Theme theme);
2429
}

src/main/java/com/example/spot/repository/querydsl/StudyPostRepositoryCustom.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import com.example.spot.domain.enums.Theme;
44
import com.example.spot.domain.study.StudyPost;
5+
import org.springframework.data.domain.PageRequest;
56
import org.springframework.data.domain.Pageable;
67

78
import java.util.List;
89

910
public interface StudyPostRepositoryCustom {
1011

12+
// 스터디 공지 게시글 페이징 조회
13+
List<StudyPost> findAnnouncementsByStudyId(Long studyId, Pageable pageable);
14+
1115
// 테마별 스터디 게시글 페이징 조회
1216
List<StudyPost> findAllByStudyIdAndTheme(Long studyId, Theme theme, Pageable pageable);
1317

src/main/java/com/example/spot/repository/querydsl/impl/StudyPostRepositoryCustomImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.querydsl.jpa.impl.JPAQuery;
88
import com.querydsl.jpa.impl.JPAQueryFactory;
99
import lombok.RequiredArgsConstructor;
10+
import org.springframework.data.domain.PageRequest;
1011
import org.springframework.data.domain.Pageable;
1112

1213
import java.util.List;
@@ -16,6 +17,18 @@ public class StudyPostRepositoryCustomImpl implements StudyPostRepositoryCustom
1617

1718
private final JPAQueryFactory queryFactory;
1819

20+
@Override
21+
public List<StudyPost> findAnnouncementsByStudyId(Long studyId, Pageable pageable) {
22+
QStudyPost studyPost = QStudyPost.studyPost;
23+
return queryFactory.selectFrom(studyPost)
24+
.where(studyPost.study.id.eq(studyId))
25+
.where(studyPost.isAnnouncement.eq(true))
26+
.orderBy(studyPost.createdAt.desc())
27+
.offset(pageable.getOffset())
28+
.limit(pageable.getPageSize())
29+
.fetch();
30+
}
31+
1932
@Override
2033
public List<StudyPost> findAllByStudyIdAndTheme(Long studyId, Theme theme, Pageable pageable) {
2134

src/main/java/com/example/spot/service/studypost/StudyPostQueryServiceImpl.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,41 +53,51 @@ public class StudyPostQueryServiceImpl implements StudyPostQueryService {
5353
@Override
5454
public StudyPostResDTO.PostListDTO getAllPosts(PageRequest pageRequest, Long studyId, ThemeQuery themeQuery) {
5555

56-
//=== Exception ===//
5756
Long memberId = SecurityUtils.getCurrentUserId();
5857
SecurityUtils.verifyUserId(memberId);
5958

6059
memberRepository.findById(memberId)
6160
.orElseThrow(() -> new MemberHandler(ErrorStatus._MEMBER_NOT_FOUND));
62-
Study study = studyRepository.findById(studyId)
61+
studyRepository.findById(studyId)
6362
.orElseThrow(() -> new StudyHandler(ErrorStatus._STUDY_NOT_FOUND));
6463

6564
memberStudyRepository.findByMemberIdAndStudyIdAndStatus(memberId, studyId, ApplicationStatus.APPROVED)
6665
.orElseThrow(() -> new StudyHandler(ErrorStatus._STUDY_MEMBER_NOT_FOUND));
6766

68-
//=== Feature ===//
6967
List<StudyPost> studyPosts;
68+
Long totalPosts;
69+
70+
// query가 없는 경우
7071
if (themeQuery == null) {
71-
// query가 없는 경우
7272
studyPosts = studyPostRepository.findAllByStudyId(studyId, pageRequest);
73-
} else if (themeQuery.equals(ThemeQuery.ANNOUNCEMENT)) {
74-
// query가 ANNOUNCEMENT인 경우
75-
studyPosts = studyPostRepository.findAllByStudyIdAndIsAnnouncement(studyId, Boolean.TRUE, pageRequest);
76-
} else {
77-
// query가 스터디 테마인 경우
73+
totalPosts = studyPostRepository.countByStudyId(studyId);
74+
}
75+
// query가 ANNOUNCEMENT인 경우
76+
else if (themeQuery.equals(ThemeQuery.ANNOUNCEMENT)) {
77+
studyPosts = studyPostRepository.findAnnouncementsByStudyId(studyId, pageRequest);
78+
totalPosts = studyPostRepository.countByStudyIdAndIsAnnouncement(studyId, Boolean.TRUE);
79+
}
80+
// query가 스터디 테마인 경우
81+
else {
7882
Theme theme = themeQuery.toTheme();
7983
studyPosts = studyPostRepository.findAllByStudyIdAndTheme(studyId, theme, pageRequest);
84+
totalPosts = studyPostRepository.countByStudyIdAndTheme(studyId, theme);
8085
}
8186

82-
return StudyPostResDTO.PostListDTO.toDTO(study, studyPosts.stream()
83-
.map(studyPost -> {
84-
if (studyLikedPostRepository.existsByMemberIdAndStudyPostId(memberId, studyPost.getId())) {
85-
return StudyPostResDTO.PostDTO.toDTO(studyPost, true);
86-
} else {
87-
return StudyPostResDTO.PostDTO.toDTO(studyPost, false);
88-
}
89-
})
90-
.toList());
87+
return StudyPostResDTO.PostListDTO.builder()
88+
.studyId(studyId)
89+
.posts(studyPosts.stream()
90+
.map(studyPost -> {
91+
if (studyLikedPostRepository.existsByMemberIdAndStudyPostId(memberId, studyPost.getId())) {
92+
return StudyPostResDTO.PostDTO.toDTO(studyPost, true);
93+
} else {
94+
return StudyPostResDTO.PostDTO.toDTO(studyPost, false);
95+
}
96+
})
97+
.toList())
98+
// (페이지 수) = ceil((전체 게시글 수) / (페이지별 게시글 수))
99+
.totalPages((long) Math.ceil((double) totalPosts / pageRequest.getPageSize()))
100+
.build();
91101

92102
}
93103

src/main/java/com/example/spot/web/dto/memberstudy/response/StudyPostResDTO.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,13 @@ public static class ImageListDTO {
3636
}
3737

3838
@Getter
39-
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
40-
@Builder(access = AccessLevel.PRIVATE)
39+
@RequiredArgsConstructor
40+
@Builder
4141
public static class PostListDTO {
4242

4343
private final Long studyId;
4444
private final List<PostDTO> posts;
45-
46-
public static PostListDTO toDTO(Study study, List<PostDTO> postDTOS) {
47-
return PostListDTO.builder()
48-
.studyId(study.getId())
49-
.posts(postDTOS)
50-
.build();
51-
}
45+
private final Long totalPages;
5246
}
5347

5448
@Getter

src/test/java/com/example/spot/service/studypost/StudyPostQueryServiceTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void getAllPosts_All_Success() {
137137
pageRequest = PageRequest.of(0, 10);
138138
when(studyPostRepository.findAllByStudyId(studyId, pageRequest))
139139
.thenReturn(List.of(studyPost1, studyPost2, studyPost3));
140-
when(studyPostRepository.findAllByStudyIdAndIsAnnouncement(studyId, true, pageRequest))
140+
when(studyPostRepository.findAnnouncementsByStudyId(studyId, pageRequest))
141141
.thenReturn(List.of(studyPost2));
142142
when(studyPostRepository.findAllByStudyIdAndTheme(studyId, Theme.FREE_TALK, pageRequest))
143143
.thenReturn(List.of(studyPost1, studyPost3));
@@ -166,7 +166,7 @@ void getAllPosts_Theme_Success() {
166166
pageRequest = PageRequest.of(0, 10);
167167
when(studyPostRepository.findAllByStudyId(studyId, pageRequest))
168168
.thenReturn(List.of(studyPost1, studyPost2, studyPost3));
169-
when(studyPostRepository.findAllByStudyIdAndIsAnnouncement(studyId, true, pageRequest))
169+
when(studyPostRepository.findAnnouncementsByStudyId(studyId, pageRequest))
170170
.thenReturn(List.of(studyPost2));
171171
when(studyPostRepository.findAllByStudyIdAndTheme(studyId, Theme.FREE_TALK, pageRequest))
172172
.thenReturn(List.of(studyPost1, studyPost3));
@@ -195,7 +195,7 @@ void getAllPosts_Announcements_Success() {
195195
pageRequest = PageRequest.of(0, 10);
196196
when(studyPostRepository.findAllByStudyId(studyId, pageRequest))
197197
.thenReturn(List.of(studyPost1, studyPost2, studyPost3));
198-
when(studyPostRepository.findAllByStudyIdAndIsAnnouncement(studyId, true, pageRequest))
198+
when(studyPostRepository.findAnnouncementsByStudyId(studyId, pageRequest))
199199
.thenReturn(List.of(studyPost2));
200200
when(studyPostRepository.findAllByStudyIdAndTheme(studyId, Theme.FREE_TALK, pageRequest))
201201
.thenReturn(List.of(studyPost1, studyPost3));
@@ -224,7 +224,7 @@ void getAllPosts_NotStudyMember_Fail() {
224224
pageRequest = PageRequest.of(0, 10);
225225
when(studyPostRepository.findAllByStudyId(studyId, pageRequest))
226226
.thenReturn(List.of(studyPost1, studyPost2, studyPost3));
227-
when(studyPostRepository.findAllByStudyIdAndIsAnnouncement(studyId, true, pageRequest))
227+
when(studyPostRepository.findAnnouncementsByStudyId(studyId, pageRequest))
228228
.thenReturn(List.of(studyPost2));
229229
when(studyPostRepository.findAllByStudyIdAndTheme(studyId, Theme.FREE_TALK, pageRequest))
230230
.thenReturn(List.of(studyPost1, studyPost3));
@@ -246,7 +246,7 @@ void getAllPosts_NotCategorized_Fail() {
246246
pageRequest = PageRequest.of(0, 10);
247247
when(studyPostRepository.findAllByStudyId(studyId, pageRequest))
248248
.thenReturn(List.of(studyPost1, studyPost2, studyPost3));
249-
when(studyPostRepository.findAllByStudyIdAndIsAnnouncement(studyId, true, pageRequest))
249+
when(studyPostRepository.findAnnouncementsByStudyId(studyId, pageRequest))
250250
.thenReturn(List.of(studyPost2));
251251
when(studyPostRepository.findAllByStudyIdAndTheme(studyId, Theme.FREE_TALK, pageRequest))
252252
.thenReturn(List.of(studyPost1, studyPost3));

0 commit comments

Comments
 (0)