From 1ea7bc1480dc4b2c791232fe063e2b8c3086aa09 Mon Sep 17 00:00:00 2001 From: 9898s Date: Fri, 27 Oct 2023 08:47:26 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20=EC=BD=94=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostController.java | 24 ++++---- .../post/dto/PostDetailResponseDto.java | 52 +++++++++-------- .../post/entity/Post.java | 58 ++++++++++--------- .../post/repository/PostRepository.java | 6 +- .../post/service/PostService.java | 39 +++++++------ 5 files changed, 95 insertions(+), 84 deletions(-) diff --git a/src/main/java/com/snsIntegrationFeedService/post/controller/PostController.java b/src/main/java/com/snsIntegrationFeedService/post/controller/PostController.java index 0f374f8..e024f63 100644 --- a/src/main/java/com/snsIntegrationFeedService/post/controller/PostController.java +++ b/src/main/java/com/snsIntegrationFeedService/post/controller/PostController.java @@ -1,26 +1,28 @@ package com.snsIntegrationFeedService.post.controller; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + import com.snsIntegrationFeedService.post.dto.PostDetailResponseDto; import com.snsIntegrationFeedService.post.service.PostService; + import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; @Tag(name = "Post API", description = "Post 관련 API 정보를 담고 있습니다.") @RequiredArgsConstructor @RestController public class PostController { - private final PostService postService; + private final PostService postService; - @Operation(summary = "게시글 상세보기", description = "유저가 게시물을 클릭 시 사용되는 API") - @GetMapping("/api/post/{postId}") - public ResponseEntity getPostDetail(@PathVariable String postId) { - PostDetailResponseDto postDetailResponseDto = postService.getPostDetail(postId); - return ResponseEntity.ok().body(postDetailResponseDto); - } + @Operation(summary = "게시글 상세보기", description = "유저가 게시물을 클릭 시 사용되는 API") + @GetMapping("/api/post/{postId}") + public ResponseEntity getPostDetail(@PathVariable String postId) { + PostDetailResponseDto postDetailResponseDto = postService.getPostDetail(postId); + return ResponseEntity.ok().body(postDetailResponseDto); + } } diff --git a/src/main/java/com/snsIntegrationFeedService/post/dto/PostDetailResponseDto.java b/src/main/java/com/snsIntegrationFeedService/post/dto/PostDetailResponseDto.java index efe4f7a..2d54660 100644 --- a/src/main/java/com/snsIntegrationFeedService/post/dto/PostDetailResponseDto.java +++ b/src/main/java/com/snsIntegrationFeedService/post/dto/PostDetailResponseDto.java @@ -1,8 +1,10 @@ package com.snsIntegrationFeedService.post.dto; -import com.snsIntegrationFeedService.post.entity.Post; import java.time.LocalDateTime; import java.util.List; + +import com.snsIntegrationFeedService.post.entity.Post; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -14,29 +16,29 @@ @Getter public class PostDetailResponseDto { - private String postId; - private String type; - private String title; - private String content; - private List hashtag; - private long viewCount; - private long likeCount; - private long shareCount; - private LocalDateTime modifiedAt; - private LocalDateTime createdAt; + private String postId; + private String type; + private String title; + private String content; + private List hashtag; + private long viewCount; + private long likeCount; + private long shareCount; + private LocalDateTime modifiedAt; + private LocalDateTime createdAt; - public static PostDetailResponseDto from(Post post, List postHashtags) { - return PostDetailResponseDto.builder() - .postId(post.getPostId()) - .type(post.getType().name()) - .title(post.getTitle()) - .content(post.getContent()) - .hashtag(postHashtags) - .viewCount(post.getViewCount()) - .likeCount(post.getLikeCount()) - .shareCount(post.getShareCount()) - .modifiedAt(post.getModifiedAt()) - .createdAt(post.getModifiedAt()) - .build(); - } + public static PostDetailResponseDto from(Post post, List postHashtags) { + return PostDetailResponseDto.builder() + .postId(post.getPostId()) + .type(post.getType().name()) + .title(post.getTitle()) + .content(post.getContent()) + .hashtag(postHashtags) + .viewCount(post.getViewCount()) + .likeCount(post.getLikeCount()) + .shareCount(post.getShareCount()) + .modifiedAt(post.getModifiedAt()) + .createdAt(post.getModifiedAt()) + .build(); + } } diff --git a/src/main/java/com/snsIntegrationFeedService/post/entity/Post.java b/src/main/java/com/snsIntegrationFeedService/post/entity/Post.java index 3f0fabb..a510892 100644 --- a/src/main/java/com/snsIntegrationFeedService/post/entity/Post.java +++ b/src/main/java/com/snsIntegrationFeedService/post/entity/Post.java @@ -1,8 +1,12 @@ package com.snsIntegrationFeedService.post.entity; +import java.util.ArrayList; +import java.util.List; + import com.snsIntegrationFeedService.common.entity.Timestamped; import com.snsIntegrationFeedService.postHashtag.entity.PostHashtag; import com.snsIntegrationFeedService.user.entity.User; + import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; @@ -14,48 +18,46 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import java.util.ArrayList; -import java.util.List; import lombok.Getter; @Entity @Getter public class Post extends Timestamped { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id") - private User user; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; - @Column(nullable = false) - private String postId; + @Column(nullable = false) + private String postId; - @Column(nullable = false) - @Enumerated(value = EnumType.STRING) - private PostTypeEnum type; + @Column(nullable = false) + @Enumerated(value = EnumType.STRING) + private PostTypeEnum type; - @Column(nullable = false) - private String title; + @Column(nullable = false) + private String title; - @Column(columnDefinition = "TEXT", nullable = false) - private String content; + @Column(columnDefinition = "TEXT", nullable = false) + private String content; - @Column(nullable = false) - private Long viewCount; + @Column(nullable = false) + private Long viewCount; - @Column(nullable = false) - private Long likeCount; + @Column(nullable = false) + private Long likeCount; - @Column(nullable = false) - private Long shareCount; + @Column(nullable = false) + private Long shareCount; - @OneToMany(mappedBy = "post", orphanRemoval = true) - private List postHashtagList = new ArrayList<>(); + @OneToMany(mappedBy = "post", orphanRemoval = true) + private List postHashtagList = new ArrayList<>(); - public void view() { - this.viewCount++; - } + public void view() { + this.viewCount++; + } } diff --git a/src/main/java/com/snsIntegrationFeedService/post/repository/PostRepository.java b/src/main/java/com/snsIntegrationFeedService/post/repository/PostRepository.java index e31ac39..4b734a5 100644 --- a/src/main/java/com/snsIntegrationFeedService/post/repository/PostRepository.java +++ b/src/main/java/com/snsIntegrationFeedService/post/repository/PostRepository.java @@ -1,10 +1,12 @@ package com.snsIntegrationFeedService.post.repository; -import com.snsIntegrationFeedService.post.entity.Post; import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; +import com.snsIntegrationFeedService.post.entity.Post; + public interface PostRepository extends JpaRepository { - Optional findByPostId(String postId); + Optional findByPostId(String postId); } \ No newline at end of file diff --git a/src/main/java/com/snsIntegrationFeedService/post/service/PostService.java b/src/main/java/com/snsIntegrationFeedService/post/service/PostService.java index 323a37e..80d6a32 100644 --- a/src/main/java/com/snsIntegrationFeedService/post/service/PostService.java +++ b/src/main/java/com/snsIntegrationFeedService/post/service/PostService.java @@ -1,37 +1,40 @@ package com.snsIntegrationFeedService.post.service; +import java.util.List; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + import com.snsIntegrationFeedService.common.error.CustomErrorCode; import com.snsIntegrationFeedService.common.exception.CustomException; import com.snsIntegrationFeedService.post.dto.PostDetailResponseDto; import com.snsIntegrationFeedService.post.entity.Post; import com.snsIntegrationFeedService.post.repository.PostRepository; -import java.util.List; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; @Slf4j @RequiredArgsConstructor @Service public class PostService { - private final PostRepository postRepository; + private final PostRepository postRepository; - @Transactional - public PostDetailResponseDto getPostDetail(String postId) { - // 예외 처리 - Post post = postRepository.findByPostId(postId).orElseThrow( - () -> new CustomException(CustomErrorCode.POST_ID_NOT_FOUND) - ); + @Transactional + public PostDetailResponseDto getPostDetail(String postId) { + // 예외 처리 + Post post = postRepository.findByPostId(postId).orElseThrow( + () -> new CustomException(CustomErrorCode.POST_ID_NOT_FOUND) + ); - // 해시 태그명 가져오기 - List hashTags = post.getPostHashtagList().stream() - .map(postHashtag -> postHashtag.getHashtag().getName()) - .toList(); + // 해시 태그명 가져오기 + List hashTags = post.getPostHashtagList().stream() + .map(postHashtag -> postHashtag.getHashtag().getName()) + .toList(); - // 조회수 증가 - post.view(); - return PostDetailResponseDto.from(post, hashTags); - } + // 조회수 증가 + post.view(); + return PostDetailResponseDto.from(post, hashTags); + } }