Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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", "댓글이 성공적으로 달렸습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.example.Midnight.Snacker.domain.Comment;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommentRepository extends JpaRepository<Comment, Long> {
import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findAllByPostId(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.example.Midnight.Snacker.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Long> {
import java.util.List;

public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findAllByOrderByDateDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +13,9 @@
public interface PostService {

Post AddPost(String title, String body, String imageUrl, LocalDateTime date, Member member); //게시글 등록
//List<PostInfoDTO> getPostInfo();
//PostResponseDTO.getPostResponseDTO getPost();
List<PostInfoDTO> getPostInfo();
PostResponseDTO.getPostResponseDTO getPosts();
List<CommentResponseDTO.CommentInfoDTO> getComments(Long postId);
PostResponseDTO.getIndiPostResponseDTO getPost(Long postId);
void DeletePost(long id); //게시글 삭제
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -37,7 +42,6 @@ public Post AddPost(String title, String body, String imageUrl, LocalDateTime da
Post savedPost = postRepository.save(newPost);

return savedPost;

} //게시글 등록

@Override
Expand All @@ -46,10 +50,10 @@ public void DeletePost(long id) {
postRepository.delete(post);
} // 게시글 삭제

/*@Override
@Override
@Transactional
public List<PostInfoDTO> getPostInfo(){
List<Post> posts = postRepository.findAll();
List<Post> posts = postRepository.findAllByOrderByDateDesc();

return posts.stream()
. map(post -> new PostInfoDTO(
Expand All @@ -58,14 +62,43 @@ public List<PostInfoDTO> getPostInfo(){
post.getBody(),
post.getDate().toLocalDate(),
post.getImageUrl(),
post.
post.getComments().size()
)).toList();
}

@Override
@Transactional
public PostResponseDTO.getPostResponseDTO getPosts(){
List<PostInfoDTO> posts = getPostInfo();

return new PostResponseDTO.getPostResponseDTO(posts);
}

@Override
@Transactional
public List<CommentResponseDTO.CommentInfoDTO> getComments(Long postId){
List<Comment> 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<CommentResponseDTO.CommentInfoDTO> comments = getComments(postId);

Post post = postRepository.findById(postId).get();

return new PostResponseDTO.getIndiPostResponseDTO(
post.getMember().getNickname(), post.getTitle(),
post.getBody(), post.getImageUrl(), comments);
}

}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ public ApiResponse<Void> DeletePost(@PathVariable(name = "postId") long postId){
@Operation(
summary = "모든 게시분 조회 API")
public ApiResponse<PostResponseDTO.getPostResponseDTO> 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<PostResponseDTO.getIndiPostResponseDTO> getPost(
@PathVariable(name = "postId") Long postId) {
PostResponseDTO.getIndiPostResponseDTO response =
postService.getPost(postId);
return ApiResponse.of(SuccessStatus.INQUERY_INDIVI_POST_OK, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,4 +22,16 @@ public static class addPostResponseDTO{
public static class getPostResponseDTO{
private List<PostInfoDTO> posts;
}

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class getIndiPostResponseDTO{
private String nickname;
private String title;
private String body;
private String imageUrl;
private List<CommentResponseDTO.CommentInfoDTO> comments;
}
}
Loading