Skip to content

Commit

Permalink
Merge pull request #138 from Me1tingPot/feature/#136
Browse files Browse the repository at this point in the history
[Feature] κ²Œμ‹œκΈ€ μž„μ‹œμ €μž₯ API #136
  • Loading branch information
moonyaeyoon authored Jul 27, 2024
2 parents 02b10a7 + 87f8336 commit cb057d3
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 109 deletions.
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

0 comments on commit cb057d3

Please sign in to comment.