diff --git a/BACK/spring-app/src/main/java/com/starchive/springapp/hashtag/service/HashTagService.java b/BACK/spring-app/src/main/java/com/starchive/springapp/hashtag/service/HashTagService.java index b511497..7f41943 100644 --- a/BACK/spring-app/src/main/java/com/starchive/springapp/hashtag/service/HashTagService.java +++ b/BACK/spring-app/src/main/java/com/starchive/springapp/hashtag/service/HashTagService.java @@ -57,7 +57,6 @@ public List findManyByCategory(Long CategoryId) { } public List findManyByPost(Long postId) { - //postService.findOne(postId); return hashTagRepository.findAllByPostId(postId).stream().map(HashTagResponse::from).toList(); } diff --git a/BACK/spring-app/src/main/java/com/starchive/springapp/post/controller/PostController.java b/BACK/spring-app/src/main/java/com/starchive/springapp/post/controller/PostController.java index d75aaef..8dad10b 100644 --- a/BACK/spring-app/src/main/java/com/starchive/springapp/post/controller/PostController.java +++ b/BACK/spring-app/src/main/java/com/starchive/springapp/post/controller/PostController.java @@ -1,6 +1,7 @@ package com.starchive.springapp.post.controller; import com.starchive.springapp.post.dto.PostCreateRequest; +import com.starchive.springapp.post.dto.PostDto; import com.starchive.springapp.post.dto.PostListResponse; import com.starchive.springapp.post.service.PostService; import io.swagger.v3.oas.annotations.Operation; @@ -9,6 +10,7 @@ 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.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; @@ -41,4 +43,11 @@ public ResponseEntity findPosts( return ResponseEntity.ok(postListResponse); } + + @GetMapping("/post/{postId}") + @Operation(summary = "상세조회") + public ResponseEntity findPost(@PathVariable("postId") Long postId) { + PostDto postDto = postService.findOne(postId); + return ResponseEntity.ok(postDto); + } } diff --git a/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostDto.java b/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostDto.java index e120f8c..e6c156d 100644 --- a/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostDto.java +++ b/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostDto.java @@ -28,7 +28,7 @@ public static PostDto of(Post post, List hashTagDtos) { postDto.title = post.getTitle(); postDto.author = post.getAuthor(); postDto.createdAt = post.getCreateAt(); - postDto.content = setContent(post.getContent()); + postDto.content = post.getContent(); postDto.hashTags = hashTagDtos; @@ -36,13 +36,6 @@ public static PostDto of(Post post, List hashTagDtos) { } - private static String setContent(String content) { - if (content != null && content.length() > 350) { - return content.substring(0, 350); - } - return content; - } - private static List setCategoryHier(Category category) { List categoryHier = new ArrayList<>(); if (category == null) { @@ -58,5 +51,4 @@ private static List setCategoryHier(Category category) { categoryHier.add(categoryDto); return categoryHier; } - } diff --git a/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostListResponse.java b/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostListResponse.java index f89aea9..5892427 100644 --- a/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostListResponse.java +++ b/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostListResponse.java @@ -9,9 +9,9 @@ public class PostListResponse { private int currentPage; private int totalPages; private long totalCount; - private List posts; + private List posts; - public static PostListResponse from(Page dtoPage) { + public static PostListResponse from(Page dtoPage) { PostListResponse postListResponse = new PostListResponse(); postListResponse.currentPage = dtoPage.getNumber(); postListResponse.totalPages = dtoPage.getTotalPages(); diff --git a/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostSimpleDto.java b/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostSimpleDto.java new file mode 100644 index 0000000..6c151de --- /dev/null +++ b/BACK/spring-app/src/main/java/com/starchive/springapp/post/dto/PostSimpleDto.java @@ -0,0 +1,62 @@ +package com.starchive.springapp.post.dto; + +import com.starchive.springapp.category.domain.Category; +import com.starchive.springapp.category.dto.CategorySimpleDto; +import com.starchive.springapp.hashtag.dto.HashTagResponse; +import com.starchive.springapp.post.domain.Post; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import lombok.Data; + +@Data +public class PostSimpleDto { + private List categoryHier; + private Long postId; + private String title; + private String author; + private LocalDateTime createdAt; + private String content; + private List hashTags; + + public static PostSimpleDto of(Post post, List hashTagDtos) { + PostSimpleDto postDto = new PostSimpleDto(); + + postDto.categoryHier = setCategoryHier(post.getCategory()); + + postDto.postId = post.getId(); + postDto.title = post.getTitle(); + postDto.author = post.getAuthor(); + postDto.createdAt = post.getCreateAt(); + postDto.content = setContent(post.getContent()); + + postDto.hashTags = hashTagDtos; + + return postDto; + + } + + private static String setContent(String content) { + if (content != null && content.length() > 350) { + return content.substring(0, 350); + } + return content; + } + + private static List setCategoryHier(Category category) { + List categoryHier = new ArrayList<>(); + if (category == null) { + return categoryHier; + } + if (category.getParent() != null) { + Category parent = category.getParent(); + CategorySimpleDto parentCategoryDto = CategorySimpleDto.from(parent); + categoryHier.add(parentCategoryDto); + } + + CategorySimpleDto categoryDto = CategorySimpleDto.from(category); + categoryHier.add(categoryDto); + return categoryHier; + } + +} diff --git a/BACK/spring-app/src/main/java/com/starchive/springapp/post/service/PostService.java b/BACK/spring-app/src/main/java/com/starchive/springapp/post/service/PostService.java index 3257bfc..dfdb05c 100644 --- a/BACK/spring-app/src/main/java/com/starchive/springapp/post/service/PostService.java +++ b/BACK/spring-app/src/main/java/com/starchive/springapp/post/service/PostService.java @@ -9,6 +9,8 @@ import com.starchive.springapp.post.dto.PostCreateRequest; import com.starchive.springapp.post.dto.PostDto; import com.starchive.springapp.post.dto.PostListResponse; +import com.starchive.springapp.post.dto.PostSimpleDto; +import com.starchive.springapp.post.exception.PostNotFoundException; import com.starchive.springapp.post.repository.PostRepository; import com.starchive.springapp.posthashtag.domain.PostHashTag; import com.starchive.springapp.posthashtag.service.PostHashTagService; @@ -43,8 +45,12 @@ public void createPost(PostCreateRequest request) { } - public Post findOne(Long postId) { - return postRepository.findById(postId).orElseThrow(); + public PostDto findOne(Long postId) { + Post post = postRepository.findById(postId).orElseThrow(PostNotFoundException::new); + + List hashTagResponses = hashTagService.findManyByPost(post.getId()); + + return PostDto.of(post, hashTagResponses); } //todo: fetch join (양방향 연관관계) @@ -71,7 +77,7 @@ public PostListResponse findPosts(Long categoryId, Long hashTagId, int pageNum, Page posts = postRepository.findManyByCategoryIds(categoryIds, postIds, pageable); - Page dtoPage = getPostDtos(posts); + Page dtoPage = getPostDtos(posts); return PostListResponse.from(dtoPage); } @@ -89,10 +95,10 @@ private List extractCategoryIds(Category category) { } - private Page getPostDtos(Page posts) { - Page dtoPage = posts.map(post -> { + private Page getPostDtos(Page posts) { + Page dtoPage = posts.map(post -> { List hashTagDtos = hashTagService.findManyByPost(post.getId()); - return PostDto.of(post, hashTagDtos); + return PostSimpleDto.of(post, hashTagDtos); }); return dtoPage; } diff --git a/BACK/spring-app/src/test/java/com/starchive/springapp/post/service/PostServiceTest.java b/BACK/spring-app/src/test/java/com/starchive/springapp/post/service/PostServiceTest.java index f140145..a9bf70e 100644 --- a/BACK/spring-app/src/test/java/com/starchive/springapp/post/service/PostServiceTest.java +++ b/BACK/spring-app/src/test/java/com/starchive/springapp/post/service/PostServiceTest.java @@ -10,10 +10,12 @@ import com.starchive.springapp.image.repository.PostImageRepository; import com.starchive.springapp.post.domain.Post; import com.starchive.springapp.post.dto.PostCreateRequest; +import com.starchive.springapp.post.dto.PostDto; import com.starchive.springapp.post.dto.PostListResponse; import com.starchive.springapp.post.repository.PostRepository; import com.starchive.springapp.posthashtag.domain.PostHashTag; import com.starchive.springapp.posthashtag.repository.PostHashTagRepository; +import jakarta.persistence.EntityManager; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -43,6 +45,9 @@ class PostServiceTest { @Autowired PostService postService; + @Autowired + EntityManager em; + @Test public void 게시글_작성_통합_테스트() throws Exception { //given @@ -99,4 +104,29 @@ class PostServiceTest { assertThat(response.getPosts()).extracting("title").containsExactlyInAnyOrder("title1", "title2"); assertThat(response.getPosts().get(0).getHashTags()).extracting("name").containsExactly("tag1"); } + + @Test + public void 게시글_상세조회_통합_테스트() throws Exception { + //given + Category category = new Category("카테고리", null); + categoryRepository.save(category); + + Post post1 = new Post(null, "title1", "content", "author1", "123", LocalDateTime.now(), category); + postRepository.save(post1); + HashTag hashTag = new HashTag("tag1"); + hashTagRepository.save(hashTag); + + postHashTagRepository.save(new PostHashTag(post1, hashTag)); + + em.flush(); + em.clear(); + //when + PostDto findOne = postService.findOne(post1.getId()); + + //then + assertThat(findOne.getTitle()).isEqualTo(post1.getTitle()); + + } + + } \ No newline at end of file