Skip to content

Commit 416b68b

Browse files
committed
PostSingleResponseDto 작성
1 parent c8a703c commit 416b68b

File tree

4 files changed

+107
-10
lines changed

4 files changed

+107
-10
lines changed

back/src/main/java/com/back/domain/post/controller/InformationPostController.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.back.domain.post.dto.PostAllResponse;
55
import com.back.domain.post.dto.PostCreateRequest;
66
import com.back.domain.post.dto.PostCreateResponse;
7+
import com.back.domain.post.dto.PostSingleResponse;
78
import com.back.domain.post.entity.Post;
89
import com.back.domain.post.rq.ApiResponse;
910
import com.back.domain.post.service.PostService;
@@ -38,8 +39,21 @@ public ResponseEntity<ApiResponse<PostCreateResponse>> createPost(
3839
@Operation(summary = "게시글 다건 조회")
3940
@GetMapping
4041
public ResponseEntity<ApiResponse<List<PostAllResponse>>> getAllPost() {
41-
List<PostAllResponse> posts = postService.getAllPosts();
42-
ApiResponse<List<PostAllResponse>> response = new ApiResponse<>("게시글 조회 성공", posts);
42+
List<PostAllResponse> postAllResponse = postService.getAllPostResponse();
43+
44+
45+
ApiResponse<List<PostAllResponse>> response = new ApiResponse<>("게시글 다건 조회 성공", postAllResponse);
46+
return ResponseEntity.ok(response);
47+
}
48+
49+
@Operation(summary = "게시글 단건 조회")
50+
@GetMapping("/{post_id}")
51+
public ResponseEntity<ApiResponse<PostSingleResponse>> getSinglePost(@PathVariable long post_id) {
52+
Post post = postService.findById(post_id);
53+
54+
PostSingleResponse postSingleResponse = new PostSingleResponse(post);
55+
56+
ApiResponse<PostSingleResponse> response = new ApiResponse<>("게시글 단건 조회 성공", postSingleResponse);
4357
return ResponseEntity.ok(response);
4458
}
4559
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.back.domain.post.dto;
2+
3+
import com.back.domain.post.entity.Post;
4+
import lombok.Data;
5+
6+
import java.time.LocalDateTime;
7+
8+
@Data
9+
public class PostSingleResponse {
10+
private Long id;
11+
private String title;
12+
private String authorName;
13+
private LocalDateTime createdAt;
14+
private int viewCount;
15+
private int like;
16+
17+
public PostSingleResponse(Post post) {
18+
this.id = post.getId();
19+
this.title = post.getTitle();
20+
this.authorName = post.getAuthorName();
21+
this.createdAt = post.getCreateDate();
22+
this.viewCount = post.getViewCount();
23+
this.like = post.getLiked();
24+
}
25+
}

back/src/main/java/com/back/domain/post/service/PostService.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@
99
import org.springframework.stereotype.Service;
1010

1111
import java.util.List;
12-
import java.util.stream.Collectors;
1312

1413
@Service
1514
@RequiredArgsConstructor
1615
public class PostService {
1716

1817
private final PostRepository postRepository;
1918

20-
public List<PostAllResponse> getAllPosts() {
19+
public List<Post> getAllPosts() {
2120
List<Post> posts = postRepository.findAll();
2221

23-
return posts.stream()
24-
.map(PostAllResponse::new)
25-
.collect(Collectors.toList());
22+
return posts;
2623
}
2724

2825

@@ -55,7 +52,13 @@ public Post createPost(PostCreateRequest postCreateRequest, String authorName) {
5552
return post;
5653
}
5754

58-
public Post findByid(Long id) {
59-
return postRepository.findById(id).get();
55+
public Post findById(Long id) {
56+
return postRepository.findById(id).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
57+
}
58+
59+
public List<PostAllResponse> getAllPostResponse() {
60+
return postRepository.findAll().stream()
61+
.map(PostAllResponse::new)
62+
.toList();
6063
}
6164
}

back/src/test/java/com/back/domain/post/controller/InformationPostControllerTest.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.List;
2525

26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2627
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2728
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
2829
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@@ -37,6 +38,9 @@ public class InformationPostControllerTest {
3738
@Autowired
3839
private PostService postService;
3940

41+
// @MockBean
42+
// private PostService postMockService;
43+
4044
@Autowired
4145
private MemberService memberService;
4246

@@ -84,7 +88,7 @@ void t1() throws Exception {
8488
.andDo(print());
8589

8690
// 실제 생성된 게시글 조회 (실제 DB에서)
87-
Post createdPost = postService.findByid(1L);
91+
Post createdPost = postService.findById(1L);
8892

8993
resultActions
9094
.andExpect(handler().handlerType(InformationPostController.class))
@@ -121,4 +125,55 @@ void t2() throws Exception {
121125
.andExpect(jsonPath("$.resultCode").value("400-2"))
122126
.andExpect(jsonPath("$.msg").value("유효하지 않은 PostType입니다."));
123127
}
128+
129+
@Test
130+
@DisplayName("게시글 다건조회")
131+
void t3() throws Exception {
132+
ResultActions resultActions = mvc
133+
.perform(
134+
get("/post/infor")
135+
)
136+
.andDo(print());
137+
138+
139+
resultActions
140+
.andExpect(handler().handlerType(InformationPostController.class))
141+
.andExpect(handler().methodName("getAllPost"))
142+
.andExpect(status().isOk())
143+
.andExpect(jsonPath("$.data").isArray())
144+
.andExpect(jsonPath("$.message").value("게시글 다건 조회 성공"))
145+
.andExpect(jsonPath("$.data").exists());
146+
}
147+
148+
// @Test
149+
// @DisplayName("게시글 단건조회")
150+
// void t4() throws Exception {
151+
// Post mockPost = new Post();
152+
//
153+
// // 리플렉션으로 ID 설정
154+
// Field idField = BaseEntity.class.getDeclaredField("id");
155+
// idField.setAccessible(true);
156+
// idField.set(mockPost, 1L);
157+
//
158+
// mockPost.setTitle("테스트 제목");
159+
// mockPost.setContent("테스트 내용");
160+
// mockPost.setAuthorName("테스트유저");
161+
// mockPost.setPostType(Post.PostType.INFORMATIONPOST);
162+
//
163+
// when(postService.findById(1L)).thenReturn(mockPost);
164+
//
165+
// ResultActions resultActions = mvc
166+
// .perform(
167+
// get("/post/infor/{post_id}", 1L)
168+
// )
169+
// .andDo(print());
170+
//
171+
//
172+
// resultActions
173+
// .andExpect(handler().handlerType(InformationPostController.class))
174+
// .andExpect(handler().methodName("getSinglePost"))
175+
// .andExpect(status().isOk())
176+
// .andExpect(jsonPath("$.message").value("게시글 단건 조회 성공"))
177+
// .andExpect(jsonPath("$.data").exists());
178+
// }
124179
}

0 commit comments

Comments
 (0)