Skip to content

Commit

Permalink
Merge pull request #48 from Bamsongee/feat/#46-view-comments
Browse files Browse the repository at this point in the history
Feat: 유저 별 댓글 조회
  • Loading branch information
Yunji-Yun authored Aug 14, 2024
2 parents 64a1f31 + 64e0898 commit 1065bf2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/ohmea/todayrecipe/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ohmea.todayrecipe.controller;

import com.ohmea.todayrecipe.dto.comment.CommentResponseDTO;
import com.ohmea.todayrecipe.dto.recipe.RecipeResponseDTO;
import com.ohmea.todayrecipe.dto.response.ResponseDTO;
import com.ohmea.todayrecipe.dto.user.JoinDTO;
Expand All @@ -8,6 +9,7 @@
import com.ohmea.todayrecipe.entity.RefreshEntity;
import com.ohmea.todayrecipe.jwt.JWTUtil;
import com.ohmea.todayrecipe.repository.RefreshRedisRepository;
import com.ohmea.todayrecipe.service.CommentService;
import com.ohmea.todayrecipe.service.UserService;
import com.ohmea.todayrecipe.util.TokenErrorResponse;
import io.jsonwebtoken.ExpiredJwtException;
Expand All @@ -32,6 +34,7 @@ public class UserController {
private final UserService userService;
private final JWTUtil jwtUtil;
private final RefreshRedisRepository refreshRedisRepository;
private final CommentService commentService;

@GetMapping("/")
public ResponseDTO<String> init(){
Expand Down Expand Up @@ -116,6 +119,16 @@ public ResponseEntity<ResponseDTO<UserResponseDTO>> updateUser(@RequestBody Upda
.body(new ResponseDTO<UserResponseDTO>(200, "user 수정이 완료되었습니다.", userResponseDTO));
}

// 유저 별 댓글 조회
@GetMapping("/mypage/comments")
public ResponseEntity<ResponseDTO<List<CommentResponseDTO>>> getUserComments() {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
List<CommentResponseDTO> comments = commentService.getCommentsByUser(username);
return ResponseEntity
.status(HttpStatus.OK)
.body(new ResponseDTO<>(200, "사용자가 작성한 댓글 조회 완료", comments));
}

// 레시피 찜
@PostMapping("/recipe/like/{ranking}")
public ResponseEntity<ResponseDTO> likeRecipe(@PathVariable String ranking) {
Expand Down Expand Up @@ -186,4 +199,5 @@ public ResponseEntity<ResponseDTO> getCombinedRecommendations() {
.status(HttpStatus.OK.value())
.body(new ResponseDTO<>(200, "레시피 맞춤 알고리즘 조회 완료", response));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface CommentRepository extends JpaRepository<CommentEntity,Long> {

List<CommentEntity> findByRecipe_Ranking(String ranking);

// 유저 댓글 조회
@Query("SELECT c FROM CommentEntity c WHERE c.user.username = :username")
List<CommentEntity> findByUsername(@Param("username") String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,14 @@ public void saveComment(String content, Long id, String username) {

commentRepository.save(comment);
}

public List<CommentResponseDTO> getCommentsByUser(String username) {
List<CommentEntity> commentEntityList = commentRepository.findByUsername(username);
List<CommentResponseDTO> commentResponseDTOList = new ArrayList<>();
commentEntityList.forEach(entity -> {
commentResponseDTOList.add(CommentResponseDTO.toDto(entity));
});
return commentResponseDTOList;
}
}

0 comments on commit 1065bf2

Please sign in to comment.