Skip to content

Commit a202f12

Browse files
committed
Refactor: 디버깅코드추가
1 parent fa07149 commit a202f12

File tree

2 files changed

+59
-26
lines changed

2 files changed

+59
-26
lines changed

backend/src/main/java/com/together/backend/domain/calendar/controller/CalendarController.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.together.backend.global.security.oauth2.dto.CustomOAuth2User;
1111
import com.together.backend.domain.user.model.entity.User;
1212
import com.together.backend.domain.user.repository.UserRepository;
13+
import lombok.extern.slf4j.Slf4j;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.http.ResponseEntity;
1516
import org.springframework.security.core.annotation.AuthenticationPrincipal;
@@ -20,6 +21,7 @@
2021
import java.util.List;
2122
import java.util.Optional;
2223

24+
@Slf4j
2325
@RestController
2426
@RequestMapping("/api/calendar")
2527
public class CalendarController {
@@ -36,26 +38,31 @@ public ResponseEntity<BaseResponse<List<CalendarRecordResponse>>> saveRecord(
3638
@RequestBody CalendarRecordRequest request,
3739
@AuthenticationPrincipal CustomOAuth2User customUser
3840
) {
41+
log.info("[API] /api/calendar POST 호출: customUser={}", customUser != null ? customUser.getEmail() : "null");
42+
3943
if (customUser == null) {
44+
log.warn("[API] 인증 실패 (customUser==null)");
4045
return ResponseEntity
4146
.status(HttpStatus.UNAUTHORIZED)
4247
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
4348
}
44-
// 실제 DB의 User 조회
4549
String email = customUser.getEmail();
4650
Optional<User> userOpt = userRepository.findByEmail(email);
4751
if (userOpt.isEmpty()) {
52+
log.warn("[API] 인증 실패 (userOpt.isEmpty): email={}", email);
4853
return ResponseEntity
4954
.status(HttpStatus.UNAUTHORIZED)
5055
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
5156
}
52-
User user = userOpt.get(); // 또는 userOpt.orElseThrow(() -> ...);
57+
User user = userOpt.get();
5358

5459
try {
5560
calendarService.saveCalendarRecord(user, request);
61+
log.info("[API] /api/calendar POST 성공: userId={}", user.getUserId());
5662
return ResponseEntity.status(HttpStatus.OK)
5763
.body(new BaseResponse<>(BaseResponseStatus.OK));
5864
} catch (Exception e) {
65+
log.error("[API] /api/calendar POST 실패: userId={}, message={}", user.getUserId(), e.getMessage(), e);
5966
return ResponseEntity
6067
.status(HttpStatus.INTERNAL_SERVER_ERROR)
6168
.body(new BaseResponse<>(BaseResponseStatus.INTERNAL_SERVER_ERROR));
@@ -67,15 +74,18 @@ public ResponseEntity<BaseResponse<List<CalendarSummaryResponse>>> getCalendarSu
6774
@RequestParam("month") String month,
6875
@AuthenticationPrincipal CustomOAuth2User customUser
6976
) {
77+
log.info("[API] /api/calendar/summary GET 호출: customUser={}, month={}", customUser != null ? customUser.getEmail() : "null", month);
78+
7079
if(customUser == null) {
80+
log.warn("[API] 인증 실패 (customUser==null)");
7181
return ResponseEntity
7282
.status(HttpStatus.UNAUTHORIZED)
7383
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
7484
}
7585

76-
// DB의 User 엔티티 조회
7786
Optional<User> userOpt = userRepository.findByEmail(customUser.getEmail());
7887
if(userOpt.isEmpty()) {
88+
log.warn("[API] 인증 실패 (userOpt.isEmpty): email={}", customUser.getEmail());
7989
return ResponseEntity
8090
.status(HttpStatus.UNAUTHORIZED)
8191
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
@@ -84,11 +94,14 @@ public ResponseEntity<BaseResponse<List<CalendarSummaryResponse>>> getCalendarSu
8494

8595
try {
8696
List<CalendarSummaryResponse> result = calendarService.getCalendarSummary(user, month);
97+
log.info("[API] /api/calendar/summary GET 성공: userId={}, count={}", user.getUserId(), result.size());
8798
return ResponseEntity.ok(new BaseResponse<>(result));
8899
} catch (DateTimeException e) {
100+
log.warn("[API] 잘못된 날짜 포맷: month={}, message={}", month, e.getMessage());
89101
return ResponseEntity.badRequest()
90102
.body(new BaseResponse<>(BaseResponseStatus.BAD_REQUEST, null));
91103
} catch (Exception e) {
104+
log.error("[API] /api/calendar/summary GET 실패: userId={}, message={}", user.getUserId(), e.getMessage(), e);
92105
return ResponseEntity
93106
.status(HttpStatus.INTERNAL_SERVER_ERROR)
94107
.body(new BaseResponse<>(BaseResponseStatus.INTERNAL_SERVER_ERROR));
@@ -100,15 +113,17 @@ public ResponseEntity<BaseResponse<CalendarDetailResponse>> getCalendarDetail(
100113
@RequestParam("date") String dateStr,
101114
@AuthenticationPrincipal CustomOAuth2User customUser
102115
) {
103-
// 인증 확인
116+
log.info("[API] /api/calendar/detail GET 호출: customUser={}, date={}", customUser != null ? customUser.getEmail() : "null", dateStr);
117+
104118
if (customUser == null) {
119+
log.warn("[API] 인증 실패 (customUser==null)");
105120
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
106121
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
107122
}
108123

109-
// 유저 엔티티 조회
110124
Optional<User> userOpt = userRepository.findByEmail(customUser.getEmail());
111125
if(userOpt.isEmpty()) {
126+
log.warn("[API] 인증 실패 (userOpt.isEmpty): email={}", customUser.getEmail());
112127
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
113128
.body(new BaseResponse<>(BaseResponseStatus.UNAUTHORIZED));
114129
}
@@ -119,14 +134,17 @@ public ResponseEntity<BaseResponse<CalendarDetailResponse>> getCalendarDetail(
119134
LocalDate date = LocalDate.parse(dateStr);
120135

121136
CalendarDetailResponse result = calendarService.getCalendarDetail(user, date);
137+
log.info("[API] /api/calendar/detail GET 성공: userId={}, date={}", user.getUserId(), dateStr);
122138
return ResponseEntity.ok(new BaseResponse<>(result));
123139
} catch (DateTimeException e) {
140+
log.warn("[API] 잘못된 날짜 포맷: date={}, message={}", dateStr, e.getMessage());
124141
return ResponseEntity.badRequest()
125142
.body(new BaseResponse<>(BaseResponseStatus.BAD_REQUEST, null));
126143
} catch (Exception e) {
144+
log.error("[API] /api/calendar/detail GET 실패: userId={}, date={}, message={}", user.getUserId(), dateStr, e.getMessage(), e);
127145
return ResponseEntity
128146
.status(HttpStatus.INTERNAL_SERVER_ERROR)
129147
.body(new BaseResponse<>(BaseResponseStatus.INTERNAL_SERVER_ERROR));
130148
}
131149
}
132-
}
150+
}

backend/src/main/java/com/together/backend/domain/calendar/service/CalendarService.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@ public void saveCalendarRecord(User user, CalendarRecordRequest request) {
214214

215215

216216

217-
// 캘린더 랜딩 시 로직
218217
@Transactional
219218
public List<CalendarSummaryResponse> getCalendarSummary(User me, String month) {
220-
// month: "2025-07"
219+
log.info("[getCalendarSummary] 호출: userId={}, email={}, role={}, month={}", me.getUserId(), me.getEmail(), me.getRole(), month);
221220
LocalDate start = LocalDate.parse(month + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
222221
LocalDate end = start.plusMonths(1).minusDays(1);
223222

@@ -227,18 +226,25 @@ public List<CalendarSummaryResponse> getCalendarSummary(User me, String month) {
227226
female = me;
228227
} else if (me.getRole() == Role.ROLE_PARTNER) {
229228
Couple coupleEntity = coupleRepository.findByPartnerUserId(me.getUserId())
230-
.orElseThrow(() -> new RuntimeException("커플 정보 없음"));
229+
.orElseThrow(() -> {
230+
log.error("[getCalendarSummary] 커플 정보 없음: partnerUserId={}", me.getUserId());
231+
return new RuntimeException("커플 정보 없음");
232+
});
231233
if (coupleEntity.getUser() == null) {
234+
log.error("[getCalendarSummary] 커플 row의 user_id가 null");
232235
throw new RuntimeException("커플 row의 user_id가 null입니다.");
233236
}
234237
female = coupleEntity.getUser();
235238
} else {
239+
log.warn("[getCalendarSummary] 지원하지 않는 role: userId={}, role={}", me.getUserId(), me.getRole());
236240
throw new IllegalArgumentException("지원하지 않는 role입니다.");
237241
}
238242

239243
List<BasicRecord> records = basicRecordRepository
240244
.findAllByUserAndOccuredAtBetween(female, start.atStartOfDay(), end.atTime(23, 59, 59));
241245

246+
log.info("[getCalendarSummary] 조회결과: userId={}, count={}", female.getUserId(), records.size());
247+
242248
return records.stream()
243249
.map(record -> CalendarSummaryResponse.builder()
244250
.date(record.getOccuredAt().toLocalDate().toString())
@@ -251,35 +257,42 @@ public List<CalendarSummaryResponse> getCalendarSummary(User me, String month) {
251257

252258

253259

254-
// 날짜별 상세 조회 로직
255260
public CalendarDetailResponse getCalendarDetail(User me, LocalDate date) {
261+
log.info("[getCalendarDetail] 호출: userId={}, email={}, role={}, date={}", me.getUserId(), me.getEmail(), me.getRole(), date);
256262
User female, male;
257-
// 1. 내 role 기준으로 여성 / 남성 구분
258-
if (me.getRole() == Role.ROLE_USER) { // 내가 여성(주유저)일 때
259-
// 커플 테이블에서 내 partner_user_id로 남성 조회
263+
264+
if (me.getRole() == Role.ROLE_USER) {
260265
Couple coupleEntity = coupleRepository.findByUser_UserId(me.getUserId())
261-
.orElseThrow(() -> new RuntimeException("커플 정보 없음"));
266+
.orElseThrow(() -> {
267+
log.error("[getCalendarDetail] 커플 정보 없음: userId={}", me.getUserId());
268+
return new RuntimeException("커플 정보 없음");
269+
});
262270
User partner = userRepository.findByUserId(coupleEntity.getPartnerUserId())
263-
.orElseThrow(() -> new RuntimeException("파트너 유저 없음"));
271+
.orElseThrow(() -> {
272+
log.error("[getCalendarDetail] 파트너 유저 없음: partnerUserId={}", coupleEntity.getPartnerUserId());
273+
return new RuntimeException("파트너 유저 없음");
274+
});
264275
female = me;
265276
male = partner;
266-
} else if (me.getRole() == Role.ROLE_PARTNER) { // 내가 남성(파트너)일 때
267-
// 커플 테이블에서 내 user_id를 partner_user_id로 갖는 커플 row를 찾고, 여성(주유저) 조회
277+
} else if (me.getRole() == Role.ROLE_PARTNER) {
268278
Couple coupleEntity = coupleRepository.findByPartnerUserId(me.getUserId())
269-
.orElseThrow(() -> new RuntimeException("커플 정보 없음"));
279+
.orElseThrow(() -> {
280+
log.error("[getCalendarDetail] 커플 정보 없음: partnerUserId={}", me.getUserId());
281+
return new RuntimeException("커플 정보 없음");
282+
});
270283
User partner = coupleEntity.getUser();
271-
if (partner == null)
284+
if (partner == null) {
285+
log.error("[getCalendarDetail] 커플 row의 user_id가 null");
272286
throw new RuntimeException("커플 row의 user_id가 null입니다.");
287+
}
273288
female = partner;
274289
male = me;
275290
} else {
291+
log.warn("[getCalendarDetail] 지원하지 않는 role: userId={}, role={}", me.getUserId(), me.getRole());
276292
throw new IllegalArgumentException("지원하지 않는 role입니다.");
277293
}
278294

279-
280-
281-
282-
// 2. 복용 기록(항상 여자 user_id로 조회!)
295+
// 복용 기록
283296
Optional<UserPill> userPillOpt = userPillRepository.findByUser_UserId(female.getUserId());
284297
Boolean takenPill = null;
285298

@@ -288,21 +301,23 @@ public CalendarDetailResponse getCalendarDetail(User me, LocalDate date) {
288301
intakeRecordRepository.findByUserPillAndIntakeDate(userPillOpt.get(), date);
289302
takenPill = intakeOpt.map(IntakeRecord::getIsTaken).orElse(null);
290303
}
291-
292-
// 3. 감정 기록(항상 여자 user_id로 조회!)
304+
// 감정 기록
293305
MoodType moodEmoji = basicRecordRepository
294306
.findAllByUserAndOccuredAtBetween(female, date.atStartOfDay(), date.atTime(23, 59, 59))
295307
.stream()
296308
.findFirst()
297309
.map(BasicRecord::getMoodEmoji)
298310
.orElse(null);
299311

300-
// 4. 관계 기록 (항상 여자/남자 id 조합으로 조회)
312+
// 관계 기록
301313
Optional<RelationRecord> relationOpt
302314
= relationRecordRepository.findByUserAndPartnerAndRecordDate(female, male, date);
303315
Boolean hadSex = relationOpt.map(RelationRecord::getHadSex).orElse(null);
304316
CondomUsage usedCondom = relationOpt.map(RelationRecord::getHadCondom).orElse(null);
305317

318+
log.info("[getCalendarDetail] 결과: femaleUserId={}, maleUserId={}, date={}, takenPill={}, mood={}, hadSex={}, usedCondom={}",
319+
female.getUserId(), male.getUserId(), date, takenPill, moodEmoji, hadSex, usedCondom);
320+
306321
return CalendarDetailResponse.builder()
307322
.date(date.toString())
308323
.takenPill(takenPill)

0 commit comments

Comments
 (0)