11package com .movelog .domain .record .service ;
22
3+ import com .movelog .domain .news .domain .News ;
4+ import com .movelog .domain .news .dto .response .NewsCalendarRes ;
35import com .movelog .domain .record .domain .Keyword ;
46import com .movelog .domain .record .domain .Record ;
57import com .movelog .domain .record .domain .VerbType ;
68import com .movelog .domain .record .dto .request .CreateRecordReq ;
79import com .movelog .domain .record .dto .request .SearchKeywordReq ;
810import com .movelog .domain .record .dto .response .RecentRecordImagesRes ;
11+ import com .movelog .domain .record .dto .response .RecordCalendarRes ;
912import com .movelog .domain .record .dto .response .SearchKeywordRes ;
1013import com .movelog .domain .record .dto .response .TodayRecordStatus ;
1114import com .movelog .domain .record .repository .KeywordRepository ;
1215import com .movelog .domain .record .repository .RecordRepository ;
16+ import com .movelog .domain .user .application .UserService ;
1317import com .movelog .domain .user .domain .User ;
1418import com .movelog .domain .user .domain .repository .UserRepository ;
19+ import com .movelog .domain .user .exception .UserNotFoundException ;
1520import com .movelog .global .config .security .token .UserPrincipal ;
1621import com .movelog .global .util .S3Util ;
1722import jakarta .validation .ConstraintViolation ;
1823import lombok .RequiredArgsConstructor ;
1924import lombok .extern .slf4j .Slf4j ;
25+ import org .springframework .data .domain .Page ;
26+ import org .springframework .data .domain .PageRequest ;
27+ import org .springframework .data .domain .Pageable ;
2028import org .springframework .stereotype .Service ;
2129import org .springframework .transaction .annotation .Transactional ;
2230import org .springframework .web .multipart .MultipartFile ;
3442@ Slf4j
3543public class RecordService {
3644 private final RecordRepository recordRepository ;
37- private final UserRepository userRepository ;
45+ private final UserService userService ;
46+
3847 private final KeywordRepository keywordRepository ;
3948 private final S3Util s3Util ;
49+ private final UserRepository userRepository ;
4050
4151 @ Transactional
42- public void createRecord (Long userId , CreateRecordReq createRecordReq , MultipartFile img ) {
43- User user = validUserById (userId );
44- // User user = validUserById (5L);
52+ public void createRecord (UserPrincipal userPrincipal , CreateRecordReq createRecordReq , MultipartFile img ) {
53+ User user = validUserById (userPrincipal );
54+ // User user = userRepository.findById (5L).orElseThrow(UserNotFoundException::new );
4555 validateCreateRecordReq (createRecordReq );
4656
4757 String recordImgUrl = s3Util .uploadToRecordFolder (img );
@@ -83,9 +93,9 @@ public void createRecord(Long userId, CreateRecordReq createRecordReq, Multipart
8393 }
8494
8595
86- public TodayRecordStatus retrieveTodayRecord (Long userId ) {
96+ public TodayRecordStatus retrieveTodayRecord (UserPrincipal userPrincipal ) {
8797 // 유저 유효성 검사 및 조회
88- User user = validUserById (userId );
98+ User user = validUserById (userPrincipal );
8999
90100 // 오늘의 시작 시간과 끝 시간 계산
91101 LocalDate today = LocalDate .now ();
@@ -120,7 +130,7 @@ public TodayRecordStatus retrieveTodayRecord(Long userId) {
120130 }
121131
122132 public List <RecentRecordImagesRes > retrieveRecentRecordImages (UserPrincipal userPrincipal , Long keywordId ) {
123- User user = validUserById (userPrincipal . getId () );
133+ User user = validUserById (userPrincipal );
124134 // User user = validUserById(5L);
125135 Keyword keyword = validKeywordById (keywordId );
126136 List <Record > records = recordRepository .findTop5ByKeywordOrderByActionTimeDesc (keyword );
@@ -136,7 +146,7 @@ public List<RecentRecordImagesRes> retrieveRecentRecordImages(UserPrincipal user
136146
137147
138148 public List <SearchKeywordRes > searchKeyword (UserPrincipal userPrincipal , SearchKeywordReq searchKeywordReq ) {
139- User user = validUserById (userPrincipal . getId () );
149+ User user = validUserById (userPrincipal );
140150 // User user = validUserById(5L);
141151 String keyword = searchKeywordReq .getSearchKeyword ();
142152 List <Keyword > keywords = keywordRepository .findAllByUserAndKeywordContaining (user , keyword );
@@ -161,8 +171,29 @@ public List<SearchKeywordRes> searchKeyword(UserPrincipal userPrincipal, SearchK
161171 return sortedResults ;
162172 }
163173
164- private User validUserById (Long userId ) {
165- Optional <User > userOptional = userRepository .findById (userId );
174+ public Page <RecordCalendarRes > getRecordByDate (UserPrincipal userPrincipal , String date , Integer page ) {
175+ User user = validUserById (userPrincipal );
176+ // User user = userRepository.findById(5L).orElseThrow(UserNotFoundException::new);
177+ LocalDateTime start = LocalDateTime .parse (date + "T00:00:00" );
178+ LocalDateTime end = LocalDateTime .parse (date + "T23:59:59" );
179+
180+ Pageable pageable = PageRequest .of (0 , 15 ); // 원하는 페이지와 크기를 지정
181+ Page <Record > recordList = recordRepository .findRecordByUserAndCreatedAtBetween (user , start , end , pageable );
182+
183+ return recordList .map (record -> RecordCalendarRes .builder ()
184+ .recordId (record .getRecordId ())
185+ .recordImageUrl (record .getRecordImage ())
186+ .noun (record .getKeyword ().getKeyword ())
187+ .verb (VerbType .getStringVerbType (record .getKeyword ().getVerbType ()))
188+ .createdAt (record .getCreatedAt ())
189+ .build ());
190+
191+ }
192+
193+
194+ private User validUserById (UserPrincipal userPrincipal ) {
195+ Optional <User > userOptional = userService .findById (userPrincipal .getId ());
196+ if (userOptional .isEmpty ()) { throw new UserNotFoundException (); }
166197 return userOptional .get ();
167198 }
168199
@@ -183,4 +214,6 @@ private void validateCreateRecordReq(CreateRecordReq createRecordReq) {
183214 throw new IllegalArgumentException ("noun is required." );
184215 }
185216 }
217+
218+
186219}
0 commit comments