diff --git a/src/main/java/dongguk/osori/domain/goal/repository/GoalRepository.java b/src/main/java/dongguk/osori/domain/goal/repository/GoalRepository.java index ce1be2d..30ab8b7 100644 --- a/src/main/java/dongguk/osori/domain/goal/repository/GoalRepository.java +++ b/src/main/java/dongguk/osori/domain/goal/repository/GoalRepository.java @@ -14,6 +14,8 @@ public interface GoalRepository extends JpaRepository { Optional findByGoalId(Long goalId); Optional findByGoalIdAndUser(Long goalId, User user); List findByUserInAndCreatedAtBetween(List users, LocalDateTime start, LocalDateTime end); + List findByUserAndCreatedAtBetween(User user, LocalDateTime start, LocalDateTime end); + } diff --git a/src/main/java/dongguk/osori/domain/goal/service/GoalService.java b/src/main/java/dongguk/osori/domain/goal/service/GoalService.java index fd6b590..afc6e25 100644 --- a/src/main/java/dongguk/osori/domain/goal/service/GoalService.java +++ b/src/main/java/dongguk/osori/domain/goal/service/GoalService.java @@ -103,40 +103,44 @@ public String deleteGoal(Long userId, Long goalId) { return "목표 삭제 완료"; } - // 피드에서 오늘 날짜의 팔로우한 사람들의 목표 조회 @Transactional - public List getTodayFeedGoals(Long userId) { - User loggedInUser = getLoggedInUser(userId); + public List getTodayFeedGoalsAsDetails(Long userId) { + User loggedInUser = getLoggedInUser(userId); // 로그인된 사용자 정보 로드 LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); LocalDateTime endOfDay = startOfDay.plusDays(1); - List followingUsers = loggedInUser.getFollowingUsers(); + // 1. 자신의 목표 조회 + List myGoals = goalRepository.findByUserAndCreatedAtBetween(loggedInUser, startOfDay, endOfDay); + List myGoalsDtos = myGoals.stream() + .map(goal -> { + List comments = goal.getComments().stream() + .map(comment -> new GoalCommentResponseDto( + comment.getCommentId(), + comment.getUser().getNickname(), + comment.getContent(), + comment.getCreatedAt(), + comment.getEmoji(), + comment.getUser().getUserId().equals(userId) // 댓글 작성자와 로그인된 사용자 ID 비교 + )) + .collect(Collectors.toList()); - return goalRepository.findByUserInAndCreatedAtBetween(followingUsers, startOfDay, endOfDay) - .stream() - .map(goal -> new FeedGoalDto( - goal.getGoalId(), - goal.getUser().getNickname(), - goal.getCreatedAt(), - goal.isCompleted(), - goal.getContent(), - goal.getComments().size() - )) + return new GoalDetailResponseDto( + goal.getGoalId(), + goal.getContent(), + goal.getUser().getNickname(), + goal.getCreatedAt(), + goal.isCompleted(), + true, // 내 목표이므로 항상 true + comments + ); + }) .collect(Collectors.toList()); - } - - @Transactional - public List getTodayFeedGoalsAsDetails(Long userId) { - User loggedInUser = getLoggedInUser(userId); // 로그인된 사용자 정보 로드 - LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); - LocalDateTime endOfDay = startOfDay.plusDays(1); + // 2. 팔로우한 사용자들의 목표 조회 List followingUsers = loggedInUser.getFollowingUsers(); - - return goalRepository.findByUserInAndCreatedAtBetween(followingUsers, startOfDay, endOfDay) - .stream() + List followingGoals = goalRepository.findByUserInAndCreatedAtBetween(followingUsers, startOfDay, endOfDay); + List followingGoalsDtos = followingGoals.stream() .map(goal -> { - // 댓글 리스트 생성 List comments = goal.getComments().stream() .map(comment -> new GoalCommentResponseDto( comment.getCommentId(), @@ -148,20 +152,22 @@ public List getTodayFeedGoalsAsDetails(Long userId) { )) .collect(Collectors.toList()); - // 목표 DTO 생성 return new GoalDetailResponseDto( goal.getGoalId(), goal.getContent(), goal.getUser().getNickname(), goal.getCreatedAt(), goal.isCompleted(), - goal.getUser().getUserId().equals(userId), // 목표 작성자와 로그인된 사용자 ID 비교 + false, // 팔로우한 사람의 목표이므로 false comments ); }) .collect(Collectors.toList()); - } + // 3. 결과 리스트 생성: 자신의 목표를 먼저 추가한 후 팔로우한 사람들의 목표 추가 + myGoalsDtos.addAll(followingGoalsDtos); + return myGoalsDtos; + } @Transactional public Optional getGoalDetailsWithComments(Long goalId, Long userId) {