From 9bf7927e875263b4c6c39fd75773a7952acfaa46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?toni=20=28=EC=9D=B4=EC=86=8C=EC=9D=80=29?= <144209738+saokiritoni@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:51:31 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20isMine=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goal/controller/FeedController.java | 14 +++- .../goal/controller/GoalController.java | 5 +- .../goal/dto/GoalCommentResponseDto.java | 1 + .../goal/dto/GoalDetailResponseDto.java | 1 + .../goal/service/GoalCommentService.java | 16 +++-- .../domain/goal/service/GoalService.java | 72 ++++++++++--------- 6 files changed, 67 insertions(+), 42 deletions(-) diff --git a/src/main/java/dongguk/osori/domain/goal/controller/FeedController.java b/src/main/java/dongguk/osori/domain/goal/controller/FeedController.java index b6712a3..110d1bf 100644 --- a/src/main/java/dongguk/osori/domain/goal/controller/FeedController.java +++ b/src/main/java/dongguk/osori/domain/goal/controller/FeedController.java @@ -40,12 +40,22 @@ public ResponseEntity> getTodayFeedGoals(HttpSession @Operation(summary = "단일 목표 상세 조회", description = "단일 목표와 그에 달린 모든 댓글을 조회합니다.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "목표 상세 조회 성공"), + @ApiResponse(responseCode = "401", description = "인증되지 않은 사용자"), @ApiResponse(responseCode = "404", description = "목표를 찾을 수 없음") }) @GetMapping("/{goalId}") - public ResponseEntity getGoalDetailsWithComments(@PathVariable("goalId") Long goalId) { - Optional goalDetails = goalService.getGoalDetailsWithComments(goalId); + public ResponseEntity getGoalDetailsWithComments( + @PathVariable("goalId") Long goalId, + HttpSession session) { + + Long userId = (Long) session.getAttribute("userId"); + if (userId == null) { + return ResponseEntity.status(401).build(); + } + + Optional goalDetails = goalService.getGoalDetailsWithComments(goalId, userId); return goalDetails.map(ResponseEntity::ok) .orElseGet(() -> ResponseEntity.notFound().build()); } + } diff --git a/src/main/java/dongguk/osori/domain/goal/controller/GoalController.java b/src/main/java/dongguk/osori/domain/goal/controller/GoalController.java index ea21dd2..f43b292 100644 --- a/src/main/java/dongguk/osori/domain/goal/controller/GoalController.java +++ b/src/main/java/dongguk/osori/domain/goal/controller/GoalController.java @@ -50,12 +50,15 @@ public ResponseEntity getGoalById(@PathVariable("goalId") if (userId == null) { return ResponseEntity.status(401).build(); } - Optional goalDetail = goalService.getGoalDetailsWithComments(goalId); + + // isMine + Optional goalDetail = goalService.getGoalDetailsWithComments(goalId, userId); return goalDetail.map(ResponseEntity::ok) .orElseGet(() -> ResponseEntity.notFound().build()); } + @Operation(summary = "로그인한 사용자의 목표 생성", description = "로그인한 사용자의 새로운 목표를 생성합니다.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "목표 생성 성공"), diff --git a/src/main/java/dongguk/osori/domain/goal/dto/GoalCommentResponseDto.java b/src/main/java/dongguk/osori/domain/goal/dto/GoalCommentResponseDto.java index 8581fd5..96a1e33 100644 --- a/src/main/java/dongguk/osori/domain/goal/dto/GoalCommentResponseDto.java +++ b/src/main/java/dongguk/osori/domain/goal/dto/GoalCommentResponseDto.java @@ -13,5 +13,6 @@ public class GoalCommentResponseDto { private String content; private LocalDateTime createdAt; private int emoji; + private boolean isMine; } diff --git a/src/main/java/dongguk/osori/domain/goal/dto/GoalDetailResponseDto.java b/src/main/java/dongguk/osori/domain/goal/dto/GoalDetailResponseDto.java index d5867dd..4eb5aa2 100644 --- a/src/main/java/dongguk/osori/domain/goal/dto/GoalDetailResponseDto.java +++ b/src/main/java/dongguk/osori/domain/goal/dto/GoalDetailResponseDto.java @@ -14,5 +14,6 @@ public class GoalDetailResponseDto { private String nickname; private LocalDateTime createdAt; private boolean completed; + private boolean isMine; private List comments; } diff --git a/src/main/java/dongguk/osori/domain/goal/service/GoalCommentService.java b/src/main/java/dongguk/osori/domain/goal/service/GoalCommentService.java index 68ed3fe..5ae2f84 100644 --- a/src/main/java/dongguk/osori/domain/goal/service/GoalCommentService.java +++ b/src/main/java/dongguk/osori/domain/goal/service/GoalCommentService.java @@ -49,20 +49,22 @@ public GoalCommentResponseDto addComment(Long goalId, Long userId, GoalCommentRe questService.updateMissionStatus(userId, MissionType.COMMENTED_ON_FRIEND_GOAL); - return new GoalCommentResponseDto( comment.getCommentId(), user.getNickname(), comment.getContent(), comment.getCreatedAt(), - comment.getEmoji() + comment.getEmoji(), + true // 댓글 작성자는 로그인된 사용자이므로 항상 true ); } - // 댓글 조회 @Transactional - public Optional getGoalDetailsWithComments(Long goalId) { + public Optional getGoalDetailsWithComments(Long goalId, Long userId) { + User loggedInUser = userRepository.findById(userId) + .orElseThrow(() -> new RuntimeException("User not found")); + return goalRepository.findById(goalId) .map(goal -> { List comments = goal.getComments().stream() @@ -71,7 +73,8 @@ public Optional getGoalDetailsWithComments(Long goalId) { comment.getUser().getNickname(), comment.getContent(), comment.getCreatedAt(), - comment.getEmoji() + comment.getEmoji(), + comment.getUser().equals(loggedInUser) // 댓글 작성자와 로그인된 사용자 비교 )) .collect(Collectors.toList()); @@ -81,12 +84,12 @@ public Optional getGoalDetailsWithComments(Long goalId) { goal.getUser().getNickname(), goal.getCreatedAt(), goal.isCompleted(), + goal.getUser().equals(loggedInUser), // 목표 작성자와 로그인된 사용자 비교 comments ); }); } - // 댓글 삭제 @Transactional public void deleteComment(Long commentId, Long userId) { @@ -100,3 +103,4 @@ public void deleteComment(Long commentId, Long userId) { goalCommentRepository.deleteById(commentId); } } + 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 871a937..fd6b590 100644 --- a/src/main/java/dongguk/osori/domain/goal/service/GoalService.java +++ b/src/main/java/dongguk/osori/domain/goal/service/GoalService.java @@ -26,12 +26,10 @@ public class GoalService { private final GoalRepository goalRepository; private final UserRepository userRepository; - private final GoalCommentRepository goalCommentRepository; private final QuestService questService; // 로그인된 사용자 가져오기 private User getLoggedInUser(Long userId) { - log.debug("Fetching user with ID: {}", userId); // 디버깅용 return userRepository.findById(userId) .orElseThrow(() -> new RuntimeException("User not found")); } @@ -39,7 +37,6 @@ private User getLoggedInUser(Long userId) { // 로그인된 사용자의 모든 목표 조회 public List getUserGoals(Long userId) { User user = getLoggedInUser(userId); - log.debug("Goals retrieved: {}", user.getGoals()); // 디버깅용 return user.getGoals().stream() .map(goal -> new GoalResponseDto(goal.getGoalId(), goal.getContent(), goal.getCreatedAt(), goal.isCompleted())) .collect(Collectors.toList()); @@ -93,32 +90,28 @@ public Optional updateGoalCompletionStatus(Long goalId, GoalCom // 목표 삭제 @Transactional public String deleteGoal(Long userId, Long goalId) { - log.debug("Deleting goal ID: {} for user ID: {}", goalId, userId); // 디버깅용 Goal goal = goalRepository.findById(goalId) .orElseThrow(() -> new RuntimeException("존재하지 않는 목표")); User user = getLoggedInUser(userId); - log.debug("User found: {}, Goal user: {}", user, goal.getUser()); // 디버깅용 if(!user.equals(goal.getUser())) { log.warn("User mismatch: {} is not the owner of goal ID: {}", userId, goalId); // 디버깅용 return "잘못된 사용자, 삭제 실패"; } - goalRepository.deleteById(goalId); // Goal에서 목표 삭제 - log.debug("Goal deleted successfully: {}", goalId); // 디버깅용 + goalRepository.deleteById(goalId); return "목표 삭제 완료"; } // 피드에서 오늘 날짜의 팔로우한 사람들의 목표 조회 @Transactional public List getTodayFeedGoals(Long userId) { - User loggedInUser = getLoggedInUser(userId); // 로그인된 사용자 가져오기 + User loggedInUser = getLoggedInUser(userId); LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); LocalDateTime endOfDay = startOfDay.plusDays(1); - List followingUsers = loggedInUser.getFollowingUsers(); // 팔로우한 사용자 목록 가져오기 + List followingUsers = loggedInUser.getFollowingUsers(); - // 오늘 날짜에 생성된 팔로우한 사용자의 목표 조회 return goalRepository.findByUserInAndCreatedAtBetween(followingUsers, startOfDay, endOfDay) .stream() .map(goal -> new FeedGoalDto( @@ -132,61 +125,74 @@ public List getTodayFeedGoals(Long userId) { .collect(Collectors.toList()); } - @Transactional public List getTodayFeedGoalsAsDetails(Long userId) { - User loggedInUser = getLoggedInUser(userId); // 로그인된 사용자 가져오기 + User loggedInUser = getLoggedInUser(userId); // 로그인된 사용자 정보 로드 LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); LocalDateTime endOfDay = startOfDay.plusDays(1); - List followingUsers = loggedInUser.getFollowingUsers(); // 팔로우한 사용자 목록 가져오기 + List followingUsers = loggedInUser.getFollowingUsers(); - // 오늘 날짜에 생성된 팔로우한 사용자의 목표 조회 return goalRepository.findByUserInAndCreatedAtBetween(followingUsers, startOfDay, endOfDay) .stream() - .map(goal -> new GoalDetailResponseDto( - goal.getGoalId(), - goal.getContent(), - goal.getUser().getNickname(), - goal.getCreatedAt(), - goal.isCompleted(), - goal.getComments().stream() - .map(comment -> new GoalCommentResponseDto( - comment.getCommentId(), - comment.getUser().getNickname(), - comment.getContent(), - comment.getCreatedAt(), - comment.getEmoji() - )) - .collect(Collectors.toList()) - )) + .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()); + + // 목표 DTO 생성 + return new GoalDetailResponseDto( + goal.getGoalId(), + goal.getContent(), + goal.getUser().getNickname(), + goal.getCreatedAt(), + goal.isCompleted(), + goal.getUser().getUserId().equals(userId), // 목표 작성자와 로그인된 사용자 ID 비교 + comments + ); + }) .collect(Collectors.toList()); } - // Goal ID를 기준으로 목표 상세 조회 (댓글 포함) @Transactional - public Optional getGoalDetailsWithComments(Long goalId) { + public Optional getGoalDetailsWithComments(Long goalId, Long userId) { + User loggedInUser = userRepository.findById(userId) + .orElseThrow(() -> new RuntimeException("User not found")); + return goalRepository.findById(goalId) .map(goal -> { + // 댓글 리스트 생성 List comments = goal.getComments().stream() .map(comment -> new GoalCommentResponseDto( comment.getCommentId(), comment.getUser().getNickname(), comment.getContent(), comment.getCreatedAt(), - comment.getEmoji() + comment.getEmoji(), + comment.getUser().getUserId().equals(userId) // 작성자와 로그인된 사용자 비교 )) .collect(Collectors.toList()); + // 목표 생성 return new GoalDetailResponseDto( goal.getGoalId(), goal.getContent(), goal.getUser().getNickname(), goal.getCreatedAt(), goal.isCompleted(), + goal.getUser().getUserId().equals(userId), // 목표 작성자와 로그인된 사용자 비교 comments ); }); } + }