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 520c021..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,6 +3,8 @@ import com.example.Midnight.Snacker.domain.Comment; import org.springframework.data.jpa.repository.JpaRepository; -public interface CommentRepository extends 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/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/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 7392411..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; @@ -12,7 +13,9 @@ public interface PostService { Post AddPost(String title, String body, String imageUrl, LocalDateTime date, Member member); //게시글 등록 - //List getPostInfo(); - //PostResponseDTO.getPostResponseDTO getPost(); + List getPostInfo(); + 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 e829a95..ee32442 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,12 +1,15 @@ 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; import com.example.Midnight.Snacker.repository.MemberRepository; 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; @@ -22,6 +25,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) { @@ -37,7 +42,6 @@ public Post AddPost(String title, String body, String imageUrl, LocalDateTime da Post savedPost = postRepository.save(newPost); return savedPost; - } //게시글 등록 @Override @@ -46,10 +50,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( @@ -58,14 +62,43 @@ public List getPostInfo(){ post.getBody(), post.getDate().toLocalDate(), post.getImageUrl(), - post. + post.getComments().size() + )).toList(); + } + + @Override + @Transactional + 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.getPostResponseDTO getPost(Member member){ + 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; + } }