Skip to content

Commit e4aea9c

Browse files
authored
Fix: 유저 정보 조회 api 물 관련 수정 (#165)
1 parent 2911465 commit e4aea9c

3 files changed

Lines changed: 41 additions & 22 deletions

File tree

‎src/main/java/com/example/cp_main_be/domain/member/user/presentation/UserController.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.example.cp_main_be.domain.member.user.dto.response.UserRegisterResponse;
99
import com.example.cp_main_be.domain.member.user.service.UserService;
1010
import com.example.cp_main_be.global.common.ApiResponse;
11-
import com.example.cp_main_be.global.exception.UserNotFoundException;
1211
import io.swagger.v3.oas.annotations.Operation;
1312
import io.swagger.v3.oas.annotations.tags.Tag;
1413
import jakarta.validation.Valid;
@@ -94,12 +93,8 @@ public ResponseEntity<ApiResponse<List<Long>>> getMyGardenIds(
9493
@Operation(summary = "유저 정보 조회", description = "유저 정보를 조회합니다")
9594
@GetMapping("/{userId}")
9695
public ResponseEntity<ApiResponse<UserProfileResponse>> getUserInfo(
97-
@PathVariable("userId") Long userId) {
98-
// SecurityContextHolder에서 현재 인증된 사용자(UUID)를 가져옴
99-
User user =
100-
userRepository
101-
.findById(userId)
102-
.orElseThrow(() -> new UserNotFoundException("해당 유저를 찾을 수 없습니다."));
103-
return ResponseEntity.ok(ApiResponse.success(userService.getUserProfile(user.getId(), userId)));
96+
@PathVariable("userId") Long targetUserId, @AuthenticationPrincipal User user) {
97+
return ResponseEntity.ok(
98+
ApiResponse.success(userService.getUserProfile(user.getId(), targetUserId)));
10499
}
105100
}

‎src/main/java/com/example/cp_main_be/domain/member/user/service/UserService.java‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import com.example.cp_main_be.domain.social.follow.domain.repository.FollowRepository;
1717
import com.example.cp_main_be.global.common.CustomApiException;
1818
import com.example.cp_main_be.global.common.ErrorCode;
19+
import com.example.cp_main_be.global.util.TimeUtil;
1920
import java.time.LocalDateTime;
20-
import java.time.ZoneId;
2121
import java.util.*;
2222
import java.util.stream.Collectors;
2323
import lombok.RequiredArgsConstructor;
@@ -165,10 +165,12 @@ public List<Long> getMyGardenIds(User user) {
165165
* @return UserProfileResponse DTO
166166
*/
167167
public UserProfileResponse getUserProfile(Long currentUserId, Long profileUserId) {
168+
// 현재 로그인한 유저
168169
User currentUser =
169170
userRepository
170171
.findById(currentUserId) // ID로 최신 유저 정보를 조회합니다.
171172
.orElseThrow(() -> new CustomApiException(ErrorCode.USER_NOT_FOUND));
173+
// 프로필 조회할 유저
172174
User profileUser =
173175
userRepository
174176
.findByIdWithGardensAndAvatars(profileUserId)
@@ -208,7 +210,7 @@ public UserProfileResponse getUserProfile(Long currentUserId, Long profileUserId
208210
}
209211

210212
// 2. 남에게 물 줄 수 있는 남은 횟수 계산
211-
LocalDateTime startOfWateringDay = getStartOfCurrentWateringDay();
213+
LocalDateTime startOfWateringDay = TimeUtil.getStartOfCurrentWateringDay();
212214

213215
// 2-1. 프로필 주인이 남에게 물을 줄 수 있는 남은 횟수 (응답 DTO용)
214216
int profileUserWateringCount =
@@ -237,6 +239,7 @@ public UserProfileResponse getUserProfile(Long currentUserId, Long profileUserId
237239
.map(
238240
garden -> {
239241
// DB를 반복 조회하는 대신, 미리 조회한 Set에서 확인하여 성능을 개선합니다.
242+
// 로그인한 유저가 해당 정원에 물을 아직 안줬고 횟수가 남았다면 true
240243
boolean alreadyWateredByMe = wateredGardenIds.contains(garden.getId());
241244
boolean isWateringAbleByMe =
242245
leftWaterCountForCurrentUser > 0 && !alreadyWateredByMe;
@@ -268,16 +271,4 @@ public UserProfileResponse getUserProfile(Long currentUserId, Long profileUserId
268271
.userGardens(userGardens)
269272
.build();
270273
}
271-
272-
// GardenService에 있던 private 메서드를 가져오거나 공통 유틸 클래스로 분리해야 합니다.
273-
private LocalDateTime getStartOfCurrentWateringDay() {
274-
LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
275-
LocalDateTime todayNoon = now.toLocalDate().atTime(12, 0);
276-
277-
if (now.isBefore(todayNoon)) {
278-
return todayNoon.minusDays(1);
279-
} else {
280-
return todayNoon;
281-
}
282-
}
283274
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.cp_main_be.global.util;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.ZoneId;
5+
6+
/** 도메인과 관련된 시간 계산을 처리하는 유틸리티 클래스입니다. */
7+
public final class TimeUtil {
8+
9+
private static final ZoneId KST = ZoneId.of("Asia/Seoul");
10+
11+
private TimeUtil() {
12+
// 유틸리티 클래스는 인스턴스화할 수 없습니다.
13+
}
14+
15+
/**
16+
* 물주기 주기의 시작 시간을 계산합니다. (매일 정오 12시 초기화)
17+
*
18+
* @return 현재 물주기 주기의 시작 LocalDateTime
19+
*/
20+
public static LocalDateTime getStartOfCurrentWateringDay() {
21+
LocalDateTime now = LocalDateTime.now(KST);
22+
LocalDateTime todayNoon = now.toLocalDate().atTime(12, 0);
23+
24+
if (now.isBefore(todayNoon)) {
25+
return todayNoon.minusDays(1); // 아직 정오가 안 지났으면, 어제 정오가 시작 시간
26+
} else {
27+
return todayNoon; // 정오가 지났으면, 오늘 정오가 시작 시간
28+
}
29+
}
30+
31+
// 향후 햇빛주기 등 다른 시간 관련 로직도 여기에 추가할 수 있습니다.
32+
// public static LocalDateTime getStartOfCurrentSunlightDay() { ... }
33+
}

0 commit comments

Comments
 (0)