Skip to content

Commit

Permalink
Feat: 피드에 내 목표도 함께 반환
Browse files Browse the repository at this point in the history
  • Loading branch information
saokiritoni committed Dec 5, 2024
1 parent 9bf7927 commit 5f2691b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface GoalRepository extends JpaRepository<Goal, Long> {
Optional<Goal> findByGoalId(Long goalId);
Optional<Goal> findByGoalIdAndUser(Long goalId, User user);
List<Goal> findByUserInAndCreatedAtBetween(List<User> users, LocalDateTime start, LocalDateTime end);
List<Goal> findByUserAndCreatedAtBetween(User user, LocalDateTime start, LocalDateTime end);


}

62 changes: 34 additions & 28 deletions src/main/java/dongguk/osori/domain/goal/service/GoalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,40 +103,44 @@ public String deleteGoal(Long userId, Long goalId) {
return "목표 삭제 완료";
}

// 피드에서 오늘 날짜의 팔로우한 사람들의 목표 조회
@Transactional
public List<FeedGoalDto> getTodayFeedGoals(Long userId) {
User loggedInUser = getLoggedInUser(userId);
public List<GoalDetailResponseDto> getTodayFeedGoalsAsDetails(Long userId) {
User loggedInUser = getLoggedInUser(userId); // 로그인된 사용자 정보 로드
LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
LocalDateTime endOfDay = startOfDay.plusDays(1);

List<User> followingUsers = loggedInUser.getFollowingUsers();
// 1. 자신의 목표 조회
List<Goal> myGoals = goalRepository.findByUserAndCreatedAtBetween(loggedInUser, startOfDay, endOfDay);
List<GoalDetailResponseDto> myGoalsDtos = myGoals.stream()
.map(goal -> {
List<GoalCommentResponseDto> 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<GoalDetailResponseDto> getTodayFeedGoalsAsDetails(Long userId) {
User loggedInUser = getLoggedInUser(userId); // 로그인된 사용자 정보 로드
LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
LocalDateTime endOfDay = startOfDay.plusDays(1);

// 2. 팔로우한 사용자들의 목표 조회
List<User> followingUsers = loggedInUser.getFollowingUsers();

return goalRepository.findByUserInAndCreatedAtBetween(followingUsers, startOfDay, endOfDay)
.stream()
List<Goal> followingGoals = goalRepository.findByUserInAndCreatedAtBetween(followingUsers, startOfDay, endOfDay);
List<GoalDetailResponseDto> followingGoalsDtos = followingGoals.stream()
.map(goal -> {
// 댓글 리스트 생성
List<GoalCommentResponseDto> comments = goal.getComments().stream()
.map(comment -> new GoalCommentResponseDto(
comment.getCommentId(),
Expand All @@ -148,20 +152,22 @@ public List<GoalDetailResponseDto> 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<GoalDetailResponseDto> getGoalDetailsWithComments(Long goalId, Long userId) {
Expand Down

0 comments on commit 5f2691b

Please sign in to comment.