Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 게시글 임시저장 API #136 #138

Merged
merged 2 commits into from
Jul 27, 2024
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 @@ -8,7 +8,6 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,65 +76,9 @@ public ResponseCode updateComment(CommentCreateRequest updateCommentDTO, Account
return ResponseCode.UPDATE_COMMENT_SUCCESS;
}



/* 댓글 목록 불러오기 */
// @Transactional(readOnly = true)
// public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) {
// List<CommentsListResponse.CommentDetail> commentDetailDTOs = new ArrayList<>();
// int count = 0;
// Long parentCursor = null;
//
// // Cursor가 자식 댓글에 해당하는 경우 처리
// if (cursor != null) {
// // Cursor가 부모 댓글이 아닌 자식 댓글을 나타내는 경우
// Comment childComment = commentRepository.findById(cursor).orElse(null);
// if (childComment != null && childComment.getParent() != null) {
// Comment parentComment = childComment.getParent();
// List<Comment> remainingChildren = commentRepository.findChildrenCommentsByParentId(parentComment.getId(), cursor);
// for (Comment child : remainingChildren) {
// if (count >= pageSize) break;
// commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(child));
// count++;
// }
// parentCursor = parentComment.getId();
//
// // 만약 자식 댓글을 모두 가져왔고, 페이지가 꽉 차지 않았다면 다음 부모 댓글로 넘어감
// if (count < pageSize && remainingChildren.size() < pageSize) {
// parentCursor = parentComment.getId();
// }
// } else {
// parentCursor = cursor; // cursor가 부모 댓글인 경우
// }
// }
//
// // 부모 댓글과 자식 댓글을 가져오는 처리
// if (count < pageSize) {
// Pageable pageable = PageRequest.of(0, pageSize - count);
// List<Comment> parentComments = commentRepository.findParentCommentsByPostId(postId, parentCursor, pageable);
// for (Comment parent : parentComments) {
// if (count >= pageSize) break;
// commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(parent));
// count++;
//
// List<Comment> children = commentRepository.findChildrenCommentsByParentId(parent.getId(), null);
// for (Comment child : children) {
// if (count >= pageSize) break;
// commentDetailDTOs.add(CommentsListResponse.CommentDetail.from(child));
// count++;
// }
// }
// }
//
// Long nextCursor = (count < pageSize) ? null : commentDetailDTOs.get(commentDetailDTOs.size() - 1).getCommentId();
// boolean isLast = (count < pageSize);
//
// return CommentsListResponse.from(commentDetailDTOs,nextCursor,isLast);
// }
/* 댓글 목록 불러오기 */
@Transactional(readOnly = true)
public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) {
Post post = findPostById(postId);
List<CommentsListResponse.CommentDetail> commentDetailDTOs = new ArrayList<>();
int count = 0;
Long parentCursor = null;
Expand Down Expand Up @@ -234,12 +178,6 @@ private Comment findCommentById(Long commentId) {
.orElseThrow(() -> new RuntimeException("댓글을 찾을 수 없습니다."));
}


private Account findAccountById(Long accountId) {
return accountRepository.findById(accountId)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다."));
}

private Post findPostById(Long postId) {
return postRepository.findById(postId)
.orElseThrow(() -> new RuntimeException("게시물을 찾을 수 없습니다."));
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/meltingpot/server/domain/entity/post/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class Post extends BaseEntity {
@Enumerated(EnumType.STRING)
private PostType postType;

private Boolean isDraft;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private Account account;
Expand All @@ -61,4 +63,7 @@ public void setPostImages(List<PostImage> postImages) {
.collect(Collectors.toList());
}

public void setIsDraft(boolean isDraft) {
this.isDraft = isDraft;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package meltingpot.server.domain.repository;

import meltingpot.server.domain.entity.comment.Comment;
import meltingpot.server.domain.entity.comment.CommentImage;
import org.springframework.data.jpa.repository.JpaRepository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package meltingpot.server.domain.repository;

import aj.org.objectweb.asm.commons.Remapper;
import meltingpot.server.domain.entity.Account;
import meltingpot.server.domain.entity.comment.Comment;
import meltingpot.server.domain.entity.post.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -16,12 +12,9 @@
import java.util.Optional;

public interface CommentRepository extends JpaRepository<Comment, Long> {
Page<Comment> findByPostId(Long postId, Pageable pageable);

Optional<Comment> findById(Long id);



@Query("SELECT c FROM Comment c WHERE c.post.id = :postId AND c.parent IS NULL AND (:parentCursor IS NULL OR c.id > :parentCursor) ORDER BY c.id ASC")
List<Comment> findParentCommentsByPostId(@Param("postId") Long postId, @Param("parentCursor") Long parentCursor, Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package meltingpot.server.domain.repository;

import aj.org.objectweb.asm.commons.Remapper;
import meltingpot.server.domain.entity.Account;
import meltingpot.server.domain.entity.post.Post;
import meltingpot.server.domain.entity.enums.PostType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -14,6 +12,7 @@
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;

public interface PostRepository extends JpaRepository<Post, Long> {

Expand All @@ -23,4 +22,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
Slice<Post> findAllByAccountAndDeletedAtIsNullOrderByIdDesc(Account account, Pageable page);

Slice<Post> findByIdAndDeletedAtIsNull(Long id);

Optional<Post> findByAccountAndIsDraftTrue(Account account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import meltingpot.server.domain.entity.comment.Comment;
import meltingpot.server.domain.entity.post.Post;
import meltingpot.server.domain.repository.CommentRepository;
import meltingpot.server.post.dto.PostDetailResponse;
import meltingpot.server.post.dto.PostsListResponse;
import org.springframework.http.ResponseEntity;
Expand All @@ -27,11 +24,14 @@
public class PostController {
private final PostService postService;

@Operation(summary = "게시물 작성, 이미지가 없을 때는 빈 값으로 주시면 됩니다. ")
@Operation(summary = "게시물 작성", description="requestParam으로 임시저장 여부를 알려주세요." +
"이미지가 없을 때는 빈 값으로 주시면 됩니다.")
@PostMapping("")
public ResponseEntity<ResponseData> createPost( @CurrentUser Account account,@RequestBody PostCreateRequest createPostDTO) {
public ResponseEntity<ResponseData> createPost( @CurrentUser Account account,
@RequestBody PostCreateRequest postCreateRequest,
@RequestParam boolean isDraft) {
try{
return ResponseData.toResponseEntity(postService.createPost(createPostDTO,account));
return ResponseData.toResponseEntity(postService.createPost(postCreateRequest,account,isDraft));
}catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.POST_CREATE_FAIL);
}
Expand Down Expand Up @@ -83,4 +83,14 @@ public ResponseEntity<ResponseData> deletePost(@CurrentUser Account account,@Pat
}
}

@GetMapping("/temp-saved")
@Operation(summary = "임시저장된 글 불러오기", description = "임시저장된 글 불러오기")
public ResponseEntity<ResponseData<PostDetailResponse>> getTempPost(@CurrentUser Account account) {
try {
return ResponseData.toResponseEntity(ResponseCode.POST_DETAIL_FETCH_SUCCEESS,postService.getTempPost(account));
} catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.POST_NOT_FOUND,null);
}
}

}
17 changes: 15 additions & 2 deletions src/main/java/meltingpot/server/post/dto/PostDetailResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import lombok.*;
import meltingpot.server.comment.dto.CommentsListResponse;
import meltingpot.server.domain.entity.post.Post;
import meltingpot.server.domain.entity.post.PostImage;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -52,4 +50,19 @@ public static PostDetailResponse of(Post post, CommentsListResponse commentsList
.updatedAt(post.getUpdatedAt())
.build();
}

public static PostDetailResponse from(Post post) {
List<ImageData> imgData = post.getPostImages().stream()
.map(postImage -> new ImageData(postImage.getId(), postImage.getImageUrl()))
.collect(Collectors.toList());

return PostDetailResponse.builder()
.postId(post.getId())
.name(post.getAccount().getName())
.title(post.getTitle())
.content(post.getContent())
.imgData(imgData)
.updatedAt(post.getUpdatedAt())
.build();
}
}
Loading