11package grep .neogulcoder .domain .study .service ;
22
3+ import grep .neogulcoder .domain .calender .controller .dto .response .TeamCalendarResponse ;
4+ import grep .neogulcoder .domain .calender .service .TeamCalendarService ;
35import grep .neogulcoder .domain .recruitment .post .repository .RecruitmentPostRepository ;
46import grep .neogulcoder .domain .study .Study ;
57import grep .neogulcoder .domain .study .StudyMember ;
1214import grep .neogulcoder .domain .study .repository .StudyMemberRepository ;
1315import grep .neogulcoder .domain .study .repository .StudyQueryRepository ;
1416import grep .neogulcoder .domain .study .repository .StudyRepository ;
17+ import grep .neogulcoder .domain .studypost .controller .dto .response .FreePostInfo ;
18+ import grep .neogulcoder .domain .studypost .controller .dto .response .NoticePostInfo ;
19+ import grep .neogulcoder .domain .studypost .repository .StudyPostQueryRepository ;
20+ import grep .neogulcoder .domain .studypost .repository .StudyPostRepository ;
1521import grep .neogulcoder .domain .users .entity .User ;
1622import grep .neogulcoder .domain .users .repository .UserRepository ;
1723import grep .neogulcoder .global .exception .business .BusinessException ;
2733import org .springframework .web .multipart .MultipartFile ;
2834
2935import java .io .IOException ;
36+ import java .time .LocalDate ;
37+ import java .time .temporal .ChronoUnit ;
3038import java .util .List ;
3139import java .util .Optional ;
3240
@@ -46,6 +54,9 @@ public class StudyService {
4654 private final RecruitmentPostRepository recruitmentPostRepository ;
4755 private final StudyMemberQueryRepository studyMemberQueryRepository ;
4856 private final UserRepository userRepository ;
57+ private final StudyPostRepository studyPostRepository ;
58+ private final StudyPostQueryRepository studyPostQueryRepository ;
59+ private final TeamCalendarService teamCalendarService ;
4960
5061 public StudyItemPagingResponse getMyStudiesPaging (Pageable pageable , Long userId , Boolean finished ) {
5162 Page <StudyItemResponse > page = studyQueryRepository .findMyStudiesPaging (pageable , userId , finished );
@@ -62,12 +73,30 @@ public List<StudyItemResponse> getMyStudies(Long userId) {
6273 }
6374
6475 public StudyHeaderResponse getStudyHeader (Long studyId ) {
65- Study study = studyRepository .findByIdAndActivatedTrue (studyId )
66- .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
76+ Study study = findValidStudy (studyId );
6777
6878 return StudyHeaderResponse .from (study );
6979 }
7080
81+ public StudyResponse getStudy (Long studyId ) {
82+ Study study = findValidStudy (studyId );
83+
84+ int progressDays = (int ) ChronoUnit .DAYS .between (study .getStartDate ().toLocalDate (), LocalDate .now ()) + 1 ;
85+ int totalDays = (int ) ChronoUnit .DAYS .between (study .getStartDate ().toLocalDate (), study .getEndDate ().toLocalDate ()) + 1 ;
86+ progressDays = Math .max (0 , Math .min (progressDays , totalDays ));
87+ int totalPostCount = studyPostRepository .countByStudyIdAndActivatedTrue (studyId );
88+
89+ LocalDate now = LocalDate .now ();
90+ int currentYear = now .getYear ();
91+ int currentMonth = now .getMonthValue ();
92+ List <TeamCalendarResponse > teamCalendars = teamCalendarService .findByMonth (studyId , currentYear , currentMonth );
93+
94+ List <NoticePostInfo > noticePosts = studyPostQueryRepository .findLatestNoticeInfoBy (studyId );
95+ List <FreePostInfo > freePosts = studyPostQueryRepository .findLatestFreeInfoBy (studyId );
96+
97+ return StudyResponse .from (study , progressDays , totalDays , totalPostCount , teamCalendars , noticePosts , freePosts );
98+ }
99+
71100 public List <StudyImageResponse > getStudyImages (Long userId ) {
72101 List <Study > myStudiesImage = studyMemberRepository .findStudiesByUserId (userId );
73102
@@ -77,8 +106,7 @@ public List<StudyImageResponse> getStudyImages(Long userId) {
77106 }
78107
79108 public StudyInfoResponse getMyStudyContent (Long studyId , Long userId ) {
80- Study study = studyRepository .findByIdAndActivatedTrue (studyId )
81- .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
109+ Study study = findValidStudy (studyId );
82110
83111 validateStudyMember (studyId , userId );
84112 validateStudyLeader (studyId , userId );
@@ -118,8 +146,7 @@ public Long createStudy(StudyCreateRequest request, Long userId, MultipartFile i
118146
119147 @ Transactional
120148 public void updateStudy (Long studyId , StudyUpdateRequest request , Long userId , MultipartFile image ) throws IOException {
121- Study study = studyRepository .findById (studyId )
122- .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
149+ Study study = findValidStudy (studyId );
123150
124151 validateLocation (request .getStudyType (), request .getLocation ());
125152 validateStudyMember (studyId , userId );
@@ -142,8 +169,7 @@ public void updateStudy(Long studyId, StudyUpdateRequest request, Long userId, M
142169
143170 @ Transactional
144171 public void deleteStudy (Long studyId , Long userId ) {
145- Study study = studyRepository .findById (studyId )
146- .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
172+ Study study = findValidStudy (studyId );
147173
148174 validateStudyMember (studyId , userId );
149175 validateStudyLeader (studyId , userId );
@@ -156,12 +182,16 @@ public void deleteStudy(Long studyId, Long userId) {
156182
157183 @ Transactional
158184 public void deleteStudyByAdmin (Long studyId ) {
159- Study study = studyRepository .findById (studyId )
160- .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
185+ Study study = findValidStudy (studyId );
161186
162187 study .delete ();
163188 }
164189
190+ private Study findValidStudy (Long studyId ) {
191+ return studyRepository .findById (studyId )
192+ .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
193+ }
194+
165195 private static void validateLocation (StudyType studyType , String location ) {
166196 if ((studyType == StudyType .OFFLINE || studyType == StudyType .HYBRID ) && (location == null || location .isBlank ())) {
167197 throw new BusinessException (STUDY_LOCATION_REQUIRED );
0 commit comments