From 3b6ceeb323dd953a0f8a444e8929a1952bb98fb2 Mon Sep 17 00:00:00 2001 From: seoyeon0103 Date: Sun, 12 Jan 2025 07:21:59 +0900 Subject: [PATCH 1/2] commit --- .../Snacker/repository/CommentRepository.java | 1 - .../Snacker/repository/PostRepository.java | 4 +++- .../Snacker/service/PostService/PostService.java | 4 ++-- .../service/PostService/PostServiceImpl.java | 16 ++++++++++------ 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java index 520c021..7861076 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java @@ -4,5 +4,4 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface CommentRepository extends JpaRepository { - } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/PostRepository.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/PostRepository.java index 2192768..264a841 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/PostRepository.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/PostRepository.java @@ -3,6 +3,8 @@ import com.example.Midnight.Snacker.domain.Post; import org.springframework.data.jpa.repository.JpaRepository; -public interface PostRepository extends JpaRepository { +import java.util.List; +public interface PostRepository extends JpaRepository { + List findAllByOrderByDateDesc(); } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostService.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostService.java index 7392411..7b35adf 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostService.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostService.java @@ -12,7 +12,7 @@ public interface PostService { Post AddPost(String title, String body, String imageUrl, LocalDateTime date, Member member); //게시글 등록 - //List getPostInfo(); - //PostResponseDTO.getPostResponseDTO getPost(); + List getPostInfo(); + PostResponseDTO.getPostResponseDTO getPost(); void DeletePost(long id); //게시글 삭제 } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java index 45feab3..719c2a4 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java @@ -3,6 +3,7 @@ import com.example.Midnight.Snacker.apiPayload.exception.handler.PostHandler; import com.example.Midnight.Snacker.domain.Member; import com.example.Midnight.Snacker.domain.Post; +import com.example.Midnight.Snacker.repository.CommentRepository; import com.example.Midnight.Snacker.repository.MemberRepository; import com.example.Midnight.Snacker.repository.PostRepository; import com.example.Midnight.Snacker.apiPayload.code.status.ErrorStatus; @@ -23,6 +24,8 @@ public class PostServiceImpl implements PostService { private final PostRepository postRepository; private final MemberRepository memberRepository; + private final CommentRepository commentRepository; + @Override public Post AddPost(String title, String body, String imageUrl, LocalDateTime date, Member member) { Post newPost = Post.builder() @@ -45,10 +48,10 @@ public void DeletePost(long id) { postRepository.delete(post); } // 게시글 삭제 - /*@Override + @Override @Transactional public List getPostInfo(){ - List posts = postRepository.findAll(); + List posts = postRepository.findAllByOrderByDateDesc(); return posts.stream() . map(post -> new PostInfoDTO( @@ -57,14 +60,15 @@ public List getPostInfo(){ post.getBody(), post.getDate().toLocalDate(), post.getImageUrl(), - post. + post.getComments().size() )).toList(); } - @Override @Transactional - public PostResponseDTO.getPostResponseDTO getPost(Member member){ + public PostResponseDTO.getPostResponseDTO getPost(){ + List posts = getPostInfo(); - }*/ + return new PostResponseDTO.getPostResponseDTO(posts); + } } From 8b414495b216264f866da48b43c487fd8e44a468 Mon Sep 17 00:00:00 2001 From: seoyeon0103 Date: Sun, 12 Jan 2025 08:11:34 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiPayload/code/status/SuccessStatus.java | 1 + .../Midnight/Snacker/domain/Comment.java | 2 +- .../Snacker/repository/CommentRepository.java | 3 ++ .../CommentService/CommentServiceImpl.java | 3 ++ .../service/PostService/PostService.java | 5 ++- .../service/PostService/PostServiceImpl.java | 31 ++++++++++++++++++- .../web/controller/PostController.java | 13 +++++++- .../dto/CommentDTO/CommentResponseDTO.java | 14 +++++++++ .../web/dto/PostDTO/PostResponseDTO.java | 13 ++++++++ 9 files changed, 81 insertions(+), 4 deletions(-) diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java index 0efe9be..095b4b4 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java @@ -20,6 +20,7 @@ public enum SuccessStatus implements BaseCode { //Post POST_DELETE_OK(HttpStatus.OK, "POST2001", "게시글이 성공적으로 삭제되었습니다."), INQUERY_POST_OK(HttpStatus.OK, "POST201", "게시글 전체를 불러왔습니다."), + INQUERY_INDIVI_POST_OK(HttpStatus.OK, "POST201", "게시글을 불러왔습니다."), //Comment COMMENT_POST_OK(HttpStatus.OK, "COMMENT2001", "댓글이 성공적으로 달렸습니다."), diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/domain/Comment.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/domain/Comment.java index 0f3ff7a..eec337b 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/domain/Comment.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/domain/Comment.java @@ -21,7 +21,7 @@ public class Comment extends BaseEntity { private String content; @Column(nullable = false) - private LocalDateTime createdAt; + private LocalDateTime date = LocalDateTime.now(); //외래키 이름은 postId로 저장되도록 함. @ManyToOne(fetch = FetchType.LAZY) diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java index 7861076..3f9d15d 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java @@ -3,5 +3,8 @@ import com.example.Midnight.Snacker.domain.Comment; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface CommentRepository extends JpaRepository { + List findAllByPostId(Long postId); } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java index f98b043..98a0bc3 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java @@ -12,6 +12,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDate; +import java.time.LocalDateTime; + @Service @RequiredArgsConstructor public class CommentServiceImpl implements CommentService { diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostService.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostService.java index 7b35adf..40c277b 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostService.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostService.java @@ -1,6 +1,7 @@ package com.example.Midnight.Snacker.service.PostService; import com.example.Midnight.Snacker.domain.Member; import com.example.Midnight.Snacker.domain.Post; +import com.example.Midnight.Snacker.web.dto.CommentDTO.CommentResponseDTO; import com.example.Midnight.Snacker.web.dto.PostDTO.PostInfoDTO; import com.example.Midnight.Snacker.web.dto.PostDTO.PostRequestDTO; import com.example.Midnight.Snacker.web.dto.PostDTO.PostResponseDTO; @@ -13,6 +14,8 @@ public interface PostService { Post AddPost(String title, String body, String imageUrl, LocalDateTime date, Member member); //게시글 등록 List getPostInfo(); - PostResponseDTO.getPostResponseDTO getPost(); + PostResponseDTO.getPostResponseDTO getPosts(); + List getComments(Long postId); + PostResponseDTO.getIndiPostResponseDTO getPost(Long postId); void DeletePost(long id); //게시글 삭제 } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java index 719c2a4..dffd6a0 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java @@ -1,6 +1,7 @@ package com.example.Midnight.Snacker.service.PostService; import com.example.Midnight.Snacker.apiPayload.exception.handler.PostHandler; +import com.example.Midnight.Snacker.domain.Comment; import com.example.Midnight.Snacker.domain.Member; import com.example.Midnight.Snacker.domain.Post; import com.example.Midnight.Snacker.repository.CommentRepository; @@ -8,6 +9,7 @@ import com.example.Midnight.Snacker.repository.PostRepository; import com.example.Midnight.Snacker.apiPayload.code.status.ErrorStatus; import com.example.Midnight.Snacker.web.controller.PostController; +import com.example.Midnight.Snacker.web.dto.CommentDTO.CommentResponseDTO; import com.example.Midnight.Snacker.web.dto.PostDTO.PostInfoDTO; import com.example.Midnight.Snacker.web.dto.PostDTO.PostResponseDTO; import lombok.AllArgsConstructor; @@ -66,9 +68,36 @@ public List getPostInfo(){ @Override @Transactional - public PostResponseDTO.getPostResponseDTO getPost(){ + public PostResponseDTO.getPostResponseDTO getPosts(){ List posts = getPostInfo(); return new PostResponseDTO.getPostResponseDTO(posts); } + + @Override + @Transactional + public List getComments(Long postId){ + List comments = commentRepository.findAllByPostId(postId); + + return comments.stream() + . map(comment -> new CommentResponseDTO.CommentInfoDTO( + comment.getId(), + comment.getMember().getNickname(), + comment.getContent(), + comment.getDate().toLocalDate() + )).toList(); + } + + @Override + @Transactional + public PostResponseDTO.getIndiPostResponseDTO getPost(Long postId){ + List comments = getComments(postId); + + Post post = postRepository.findById(postId).get(); + + return new PostResponseDTO.getIndiPostResponseDTO( + post.getMember().getNickname(), post.getTitle(), + post.getBody(), post.getImageUrl(), comments); + } + } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/PostController.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/PostController.java index 65c20f0..3faeade 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/PostController.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/PostController.java @@ -59,7 +59,18 @@ public ApiResponse DeletePost(@PathVariable(name = "postId") long postId){ @Operation( summary = "모든 게시분 조회 API") public ApiResponse getAllPosts() { - PostResponseDTO.getPostResponseDTO response = null; + PostResponseDTO.getPostResponseDTO response = postService.getPosts(); return ApiResponse.of(SuccessStatus.INQUERY_POST_OK,response); } + + @GetMapping("/api/post/{postId}") + @Operation( + summary = "개별 게시물 조회 API" + ) + public ApiResponse getPost( + @PathVariable(name = "postId") Long postId) { + PostResponseDTO.getIndiPostResponseDTO response = + postService.getPost(postId); + return ApiResponse.of(SuccessStatus.INQUERY_INDIVI_POST_OK, response); + } } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentResponseDTO.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentResponseDTO.java index 9cca0d7..08a26e1 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentResponseDTO.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentResponseDTO.java @@ -4,6 +4,9 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.springframework.data.convert.Jsr310Converters; + +import java.time.LocalDate; public class CommentResponseDTO { @@ -14,4 +17,15 @@ public class CommentResponseDTO { public static class CommentPostResponseDTO{ long commentId; } + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class CommentInfoDTO{ + private Long commentId; + private String nickname; + private String content; + private LocalDate date; + } } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/PostDTO/PostResponseDTO.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/PostDTO/PostResponseDTO.java index 0442529..869e219 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/PostDTO/PostResponseDTO.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/PostDTO/PostResponseDTO.java @@ -1,5 +1,6 @@ package com.example.Midnight.Snacker.web.dto.PostDTO; +import com.example.Midnight.Snacker.web.dto.CommentDTO.CommentResponseDTO; import lombok.*; import java.util.List; @@ -21,4 +22,16 @@ public static class addPostResponseDTO{ public static class getPostResponseDTO{ private List posts; } + + @Setter + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class getIndiPostResponseDTO{ + private String nickname; + private String title; + private String body; + private String imageUrl; + private List comments; + } }