Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/example/cp_main_be/CpMainBeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableJpaAuditing
public class CpMainBeApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ public class Garden {
private boolean isLocked;

@CreatedDate
@Column(updatable = false)
@Column(nullable = false)
private LocalDateTime createdAt;

@LastModifiedDate private LocalDateTime updatedAt;
@LastModifiedDate
@Column(nullable = false)
private LocalDateTime updatedAt;

@Builder
public Garden(User user, Integer slotNumber, GardenBackground gardenBackground, Avatar avatar) {
Expand All @@ -77,6 +79,8 @@ public Garden(User user, Integer slotNumber, GardenBackground gardenBackground,

public void unlock() {
this.isLocked = false;
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}

public void increaseWaterCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,22 @@ public UserProfileResponse getUserProfile(Long currentUserId, Long profileUserId
followStatus = FollowStatus.NOT_FOLLOWING;
}

// 2. 남에게 물 줄 수 있는 남은 횟수 계산 (현재 접속 유저 기준)
// 2. 남에게 물 줄 수 있는 남은 횟수 계산
LocalDateTime startOfWateringDay = getStartOfCurrentWateringDay();
int todayWateringCountForOthers =

// 2-1. 프로필 주인이 남에게 물을 줄 수 있는 남은 횟수 (응답 DTO용)
int profileUserWateringCount =
friendWateringLogRepository.countByWaterGiverAndWateredAtAfter(
profileUser, startOfWateringDay);
long leftWaterCountForProfileUser =
Math.max(0, (long) MAX_FRIEND_WATERING_PER_DAY - profileUserWateringCount);

// 2-2. 현재 접속 유저가 남에게 물을 줄 수 있는 남은 횟수 (물주기 가능 여부 판단용)
int currentUserWateringCount =
friendWateringLogRepository.countByWaterGiverAndWateredAtAfter(
currentUser, startOfWateringDay);
long leftWaterCountForOthers =
Math.max(0, (long) MAX_FRIEND_WATERING_PER_DAY - todayWateringCountForOthers);
long leftWaterCountForCurrentUser =
Math.max(0, (long) MAX_FRIEND_WATERING_PER_DAY - currentUserWateringCount);

// [성능 개선] N+1 문제를 해결하기 위해, 오늘 내가 물 준 정원 ID 목록을 한 번에 조회합니다.
Set<Long> wateredGardenIds =
Expand All @@ -229,7 +238,8 @@ public UserProfileResponse getUserProfile(Long currentUserId, Long profileUserId
garden -> {
// DB를 반복 조회하는 대신, 미리 조회한 Set에서 확인하여 성능을 개선합니다.
boolean alreadyWateredByMe = wateredGardenIds.contains(garden.getId());
boolean isWateringAbleByMe = leftWaterCountForOthers > 0 && !alreadyWateredByMe;
boolean isWateringAbleByMe =
leftWaterCountForCurrentUser > 0 && !alreadyWateredByMe;

HomeResponseDto.AvatarInfo avatarInfoForGarden =
HomeResponseDto.AvatarInfo.builder()
Expand All @@ -254,7 +264,7 @@ public UserProfileResponse getUserProfile(Long currentUserId, Long profileUserId
.userNickname(profileUser.getNickname())
.profileImageUrl(profileImageUrl)
.followStatus(followStatus)
.leftWaterCountForOthers(leftWaterCountForOthers)
.leftWaterCountForOthers(leftWaterCountForProfileUser)
.userGardens(userGardens)
.build();
}
Expand Down