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 @@ -57,7 +57,6 @@ public List<HashTagDto> findManyByCategory(Long CategoryId) {
}

public List<HashTagResponse> findManyByPost(Long postId) {
//postService.findOne(postId);

return hashTagRepository.findAllByPostId(postId).stream().map(HashTagResponse::from).toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -41,4 +43,11 @@ public ResponseEntity<?> findPosts(

return ResponseEntity.ok(postListResponse);
}

@GetMapping("/post/{postId}")
@Operation(summary = "상세조회")
public ResponseEntity<PostDto> findPost(@PathVariable("postId") Long postId) {
PostDto postDto = postService.findOne(postId);
return ResponseEntity.ok(postDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,14 @@ public static PostDto of(Post post, List<HashTagResponse> 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;

return postDto;

}

private static String setContent(String content) {
if (content != null && content.length() > 350) {
return content.substring(0, 350);
}
return content;
}

private static List<CategorySimpleDto> setCategoryHier(Category category) {
List<CategorySimpleDto> categoryHier = new ArrayList<>();
if (category == null) {
Expand All @@ -58,5 +51,4 @@ private static List<CategorySimpleDto> setCategoryHier(Category category) {
categoryHier.add(categoryDto);
return categoryHier;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class PostListResponse {
private int currentPage;
private int totalPages;
private long totalCount;
private List<PostDto> posts;
private List<PostSimpleDto> posts;

public static PostListResponse from(Page<PostDto> dtoPage) {
public static PostListResponse from(Page<PostSimpleDto> dtoPage) {
PostListResponse postListResponse = new PostListResponse();
postListResponse.currentPage = dtoPage.getNumber();
postListResponse.totalPages = dtoPage.getTotalPages();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CategorySimpleDto> categoryHier;
private Long postId;
private String title;
private String author;
private LocalDateTime createdAt;
private String content;
private List<HashTagResponse> hashTags;

public static PostSimpleDto of(Post post, List<HashTagResponse> 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<CategorySimpleDto> setCategoryHier(Category category) {
List<CategorySimpleDto> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<HashTagResponse> hashTagResponses = hashTagService.findManyByPost(post.getId());

return PostDto.of(post, hashTagResponses);
}

//todo: fetch join (양방향 연관관계)
Expand All @@ -71,7 +77,7 @@ public PostListResponse findPosts(Long categoryId, Long hashTagId, int pageNum,

Page<Post> posts = postRepository.findManyByCategoryIds(categoryIds, postIds, pageable);

Page<PostDto> dtoPage = getPostDtos(posts);
Page<PostSimpleDto> dtoPage = getPostDtos(posts);

return PostListResponse.from(dtoPage);
}
Expand All @@ -89,10 +95,10 @@ private List<Long> extractCategoryIds(Category category) {
}


private Page<PostDto> getPostDtos(Page<Post> posts) {
Page<PostDto> dtoPage = posts.map(post -> {
private Page<PostSimpleDto> getPostDtos(Page<Post> posts) {
Page<PostSimpleDto> dtoPage = posts.map(post -> {
List<HashTagResponse> hashTagDtos = hashTagService.findManyByPost(post.getId());
return PostDto.of(post, hashTagDtos);
return PostSimpleDto.of(post, hashTagDtos);
});
return dtoPage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +45,9 @@ class PostServiceTest {
@Autowired
PostService postService;

@Autowired
EntityManager em;

@Test
public void 게시글_작성_통합_테스트() throws Exception {
//given
Expand Down Expand Up @@ -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());

}


}
Loading