Skip to content

Commit 1f999d4

Browse files
committed
[Refactor]: 응답 형태 ApiResponse -> ResponseEntity로 수정
1 parent ae56526 commit 1f999d4

File tree

5 files changed

+59
-71
lines changed

5 files changed

+59
-71
lines changed

back/src/main/java/com/back/domain/comment/controller/CommentController.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.back.domain.comment.dto.CommentResponse;
55
import com.back.domain.comment.enums.CommentSortType;
66
import com.back.domain.comment.service.CommentService;
7-
import com.back.global.common.ApiResponse;
87
import com.back.global.common.PageResponse;
98
import com.back.global.security.CustomUserDetails;
109
import io.swagger.v3.oas.annotations.Operation;
@@ -17,12 +16,10 @@
1716
import org.springframework.data.domain.Pageable;
1817
import org.springframework.data.domain.Sort;
1918
import org.springframework.http.HttpStatus;
19+
import org.springframework.http.ResponseEntity;
2020
import org.springframework.security.core.annotation.AuthenticationPrincipal;
2121
import org.springframework.web.bind.annotation.*;
2222

23-
/**
24-
* 댓글 관련 API 요청을 처리하는 컨트롤러.
25-
*/
2623
@Tag(name = "Comment", description = "댓글 관련 API")
2724
@RestController
2825
@RequestMapping("/api/v1/posts/{postId}/comments")
@@ -31,9 +28,10 @@ public class CommentController {
3128

3229
private final CommentService commentService;
3330

31+
// 댓글 생성
3432
@PostMapping
3533
@Operation(summary = "댓글 생성", description = "새 댓글을 생성합니다.")
36-
public ApiResponse<CommentResponse> createPost(
34+
public ResponseEntity<CommentResponse> createPost(
3735
@io.swagger.v3.oas.annotations.parameters.RequestBody(
3836
description = "생성할 댓글 정보",
3937
required = true
@@ -43,13 +41,13 @@ public ApiResponse<CommentResponse> createPost(
4341
@AuthenticationPrincipal CustomUserDetails cs
4442
) {
4543
CommentResponse response = commentService.createComment(cs.getUser().getId(), postId, request);
46-
return ApiResponse.success(response, "성공적으로 생성되었습니다.", HttpStatus.OK);
44+
return ResponseEntity.status(HttpStatus.CREATED).body(response);
4745
}
4846

49-
// 게시글 목록 조회
47+
// fixme 게시글 목록 조회 - 정렬 조건 최신순, 좋아요순 추가하였는데 변경될 수 있음
5048
@GetMapping
5149
@Operation(summary = "댓글 목록 조회", description = "게시글 목록을 조회합니다.")
52-
public ApiResponse<PageResponse<CommentResponse>> getPosts(
50+
public ResponseEntity<PageResponse<CommentResponse>> getPosts(
5351
@Parameter(description = "페이지 정보") Pageable pageable,
5452
@Parameter(description = "조회할 게시글 ID", required = true) @PathVariable("postId") Long postId,
5553
@Parameter(description = "정렬 조건 LATEST or LIKES") @RequestParam(defaultValue = "LATEST") CommentSortType sortType,
@@ -64,29 +62,29 @@ public ApiResponse<PageResponse<CommentResponse>> getPosts(
6462
);
6563

6664
Page<CommentResponse> responses = commentService.getComments(cs.getUser().getId(), postId, sortedPageable);
67-
return ApiResponse.success(PageResponse.of(responses), "성공적으로 조회되었습니다.", HttpStatus.OK);
65+
return ResponseEntity.ok(PageResponse.of(responses));
6866
}
6967

7068

7169
@PutMapping("/{commentId}")
7270
@Operation(summary = "댓글 수정", description = "자신의 댓글을 수정합니다.")
73-
public ApiResponse<Long> updateComment(
71+
public ResponseEntity<Long> updateComment(
7472
@Parameter(description = "수정할 댓글 ID", required = true) @PathVariable Long commentId,
7573
@io.swagger.v3.oas.annotations.parameters.RequestBody(
7674
description = "수정할 댓글 정보",
7775
required = true
7876
)
7977
@RequestBody @Valid CommentRequest request,
8078
@AuthenticationPrincipal CustomUserDetails cs) {
81-
return ApiResponse.success(commentService.updateComment(cs.getUser().getId(), commentId, request), "성공적으로 수정되었습니다.", HttpStatus.OK);
79+
return ResponseEntity.ok(commentService.updateComment(cs.getUser().getId(), commentId, request));
8280
}
8381

8482
@DeleteMapping("/{commentId}")
8583
@Operation(summary = "댓글 삭제", description = "자신의 댓글을 삭제합니다.")
86-
public ApiResponse<Void> deletePost(
84+
public ResponseEntity<Void> deletePost(
8785
@Parameter(description = "삭제할 댓글 ID", required = true) @PathVariable Long commentId,
8886
@AuthenticationPrincipal CustomUserDetails cs) {
8987
commentService.deleteComment(cs.getUser().getId(), commentId);
90-
return ApiResponse.success(null, "성공적으로 삭제되었습니다.", HttpStatus.OK);
88+
return ResponseEntity.ok(null);
9189
}
9290
}

back/src/main/java/com/back/domain/like/controller/LikeController.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.back.domain.like.controller;
22

33
import com.back.domain.like.service.LikeService;
4-
import com.back.global.common.ApiResponse;
54
import com.back.global.security.CustomUserDetails;
65
import lombok.RequiredArgsConstructor;
76
import org.springframework.http.HttpStatus;
@@ -19,14 +18,14 @@ public class LikeController {
1918
private final LikeService likeService;
2019

2120
@PostMapping("/{postId}/likes")
22-
public ApiResponse<Void> addLike(@PathVariable Long postId, @AuthenticationPrincipal CustomUserDetails cs) {
21+
public ResponseEntity<Void> addLike(@PathVariable Long postId, @AuthenticationPrincipal CustomUserDetails cs) {
2322
likeService.addLike(cs.getUser().getId(), postId);
24-
return ApiResponse.success(null, "좋아요 추가했습니다.", HttpStatus.OK);
23+
return ResponseEntity.status(HttpStatus.CREATED).body(null);
2524
}
2625

2726
@DeleteMapping("/{postId}/likes")
28-
public ApiResponse<Void> removeLike(@PathVariable Long postId, @AuthenticationPrincipal CustomUserDetails cs) {
27+
public ResponseEntity<Void> removeLike(@PathVariable Long postId, @AuthenticationPrincipal CustomUserDetails cs) {
2928
likeService.removeLike(postId, cs.getUser().getId());
30-
return ApiResponse.success(null, "좋아요 취소하였습니다.", HttpStatus.OK);
29+
return ResponseEntity.ok(null);
3130
}
3231
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.back.domain.post.controller;
22

3-
import com.back.domain.post.dto.PostRequest;
43
import com.back.domain.post.dto.PostDetailResponse;
4+
import com.back.domain.post.dto.PostRequest;
55
import com.back.domain.post.dto.PostSearchCondition;
66
import com.back.domain.post.dto.PostSummaryResponse;
77
import com.back.domain.post.service.PostService;
8-
import com.back.global.common.ApiResponse;
98
import com.back.global.common.PageResponse;
109
import com.back.global.security.CustomUserDetails;
1110
import io.swagger.v3.oas.annotations.Operation;
@@ -16,6 +15,7 @@
1615
import org.springframework.data.domain.Page;
1716
import org.springframework.data.domain.Pageable;
1817
import org.springframework.http.HttpStatus;
18+
import org.springframework.http.ResponseEntity;
1919
import org.springframework.security.core.annotation.AuthenticationPrincipal;
2020
import org.springframework.web.bind.annotation.*;
2121

@@ -33,7 +33,7 @@ public class PostController {
3333
// 게시글 생성
3434
@PostMapping
3535
@Operation(summary = "게시글 생성", description = "새 게시글을 생성합니다.")
36-
public ApiResponse<PostDetailResponse> createPost(
36+
public ResponseEntity<PostDetailResponse> createPost(
3737
@io.swagger.v3.oas.annotations.parameters.RequestBody(
3838
description = "생성할 게시글 정보",
3939
required = true
@@ -42,48 +42,48 @@ public ApiResponse<PostDetailResponse> createPost(
4242
@AuthenticationPrincipal CustomUserDetails cs
4343
) {
4444
PostDetailResponse response = postService.createPost(cs.getUser().getId(), request);
45-
return ApiResponse.success(response, "성공적으로 생성되었습니다.", HttpStatus.OK);
45+
return ResponseEntity.status(HttpStatus.CREATED).body(response);
4646
}
4747

4848
// 게시글 목록 조회
4949
@GetMapping
5050
@Operation(summary = "게시글 목록 조회", description = "게시글 목록을 조회합니다.")
51-
public ApiResponse<PageResponse<PostSummaryResponse>> getPosts(
51+
public ResponseEntity<PageResponse<PostSummaryResponse>> getPosts(
5252
@Parameter(description = "검색 조건") @ModelAttribute PostSearchCondition condition,
5353
@Parameter(description = "페이지 정보") Pageable pageable,
5454
@AuthenticationPrincipal CustomUserDetails cs) {
5555
Page<PostSummaryResponse> responses = postService.getPosts(cs.getUser().getId(), condition, pageable);
56-
return ApiResponse.success(PageResponse.of(responses), "성공적으로 조회되었습니다.", HttpStatus.OK);
56+
return ResponseEntity.ok(PageResponse.of(responses));
5757
}
5858

5959
// 게시글 단건 조회
6060
@GetMapping("/{postId}")
6161
@Operation(summary = "게시글 상세 조회", description = "게시글 ID로 게시글을 조회합니다.")
62-
public ApiResponse<PostDetailResponse> getPost(
62+
public ResponseEntity<PostDetailResponse> getPost(
6363
@Parameter(description = "조회할 게시글 ID", required = true) @PathVariable Long postId,
6464
@AuthenticationPrincipal CustomUserDetails cs) {
65-
return ApiResponse.success(postService.getPost(cs.getUser().getId(), postId), "성공적으로 조회되었습니다.", HttpStatus.OK);
65+
return ResponseEntity.ok(postService.getPost(cs.getUser().getId(), postId));
6666
}
6767

6868
@PutMapping("/{postId}")
6969
@Operation(summary = "게시글 수정", description = "게시글 ID로 게시글을 수정합니다.")
70-
public ApiResponse<Long> updatePost(
70+
public ResponseEntity<Long> updatePost(
7171
@Parameter(description = "수정할 게시글 ID", required = true) @PathVariable Long postId,
7272
@io.swagger.v3.oas.annotations.parameters.RequestBody(
7373
description = "수정할 게시글 정보",
7474
required = true
7575
)
7676
@RequestBody @Valid PostRequest request,
7777
@AuthenticationPrincipal CustomUserDetails cs) {
78-
return ApiResponse.success(postService.updatePost(cs.getUser().getId(), postId, request), "성공적으로 수정되었습니다.", HttpStatus.OK);
78+
return ResponseEntity.ok(postService.updatePost(cs.getUser().getId(), postId, request));
7979
}
8080

8181
@DeleteMapping("/{postId}")
8282
@Operation(summary = "게시글 삭제", description = "게시글 ID로 게시글을 삭제합니다.")
83-
public ApiResponse<Void> deletePost(
83+
public ResponseEntity<Void> deletePost(
8484
@Parameter(description = "삭제할 게시글 ID", required = true) @PathVariable Long postId,
8585
@AuthenticationPrincipal CustomUserDetails cs) {
8686
postService.deletePost(cs.getUser().getId(), postId);
87-
return ApiResponse.success(null, "성공적으로 삭제되었습니다.", HttpStatus.OK);
87+
return ResponseEntity.ok(null);
8888
}
8989
}

back/src/test/java/com/back/domain/comment/controller/CommentControllerTest.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ void success() throws Exception {
107107
mockMvc.perform(post("/api/v1/posts/{postId}/comments", testPost.getId())
108108
.contentType(MediaType.APPLICATION_JSON)
109109
.content(toJson(request)))
110-
.andExpect(status().isOk())
111-
.andExpect(jsonPath("$.data.content").value("테스트 댓글"))
112-
.andExpect(jsonPath("$.message").value("성공적으로 생성되었습니다."));
110+
.andExpect(status().isCreated())
111+
.andExpect(jsonPath("$.content").value("테스트 댓글"));
113112
}
114113
}
115114

@@ -127,10 +126,10 @@ void success() throws Exception {
127126
mockMvc.perform(get("/api/v1/posts/{postId}/comments", testPost.getId())
128127
.contentType(MediaType.APPLICATION_JSON))
129128
.andExpect(status().isOk())
130-
.andExpect(jsonPath("$.data.items").isArray())
131-
.andExpect(jsonPath("$.data.items.length()").value(3))
132-
.andExpect(jsonPath("$.data.totalElements").value(3))
133-
.andExpect(jsonPath("$.data.totalPages").value(1));
129+
.andExpect(jsonPath("$.items").isArray())
130+
.andExpect(jsonPath("$.items.length()").value(3))
131+
.andExpect(jsonPath("$.totalElements").value(3))
132+
.andExpect(jsonPath("$.totalPages").value(1));
134133
}
135134

136135
@Test
@@ -144,9 +143,9 @@ void successWithLatestSort() throws Exception {
144143
.param("sortType", "LATEST")
145144
.contentType(MediaType.APPLICATION_JSON))
146145
.andExpect(status().isOk())
147-
.andExpect(jsonPath("$.data.items[0].content").value("가장 최근 댓글"))
148-
.andExpect(jsonPath("$.data.items[1].content").value("중간 댓글"))
149-
.andExpect(jsonPath("$.data.items[2].content").value("오래된 댓글"));
146+
.andExpect(jsonPath("$.items[0].content").value("가장 최근 댓글"))
147+
.andExpect(jsonPath("$.items[1].content").value("중간 댓글"))
148+
.andExpect(jsonPath("$.items[2].content").value("오래된 댓글"));
150149
}
151150

152151
@Test
@@ -160,9 +159,9 @@ void successWithLikesSort() throws Exception {
160159
.param("sortType", "LIKES")
161160
.contentType(MediaType.APPLICATION_JSON))
162161
.andExpect(status().isOk())
163-
.andExpect(jsonPath("$.data.items[0].likeCount").value(10))
164-
.andExpect(jsonPath("$.data.items[1].likeCount").value(5))
165-
.andExpect(jsonPath("$.data.items[2].likeCount").value(2));
162+
.andExpect(jsonPath("$.items[0].likeCount").value(10))
163+
.andExpect(jsonPath("$.items[1].likeCount").value(5))
164+
.andExpect(jsonPath("$.items[2].likeCount").value(2));
166165
}
167166

168167
@Test
@@ -171,10 +170,10 @@ void successWithEmptyComments() throws Exception {
171170
mockMvc.perform(get("/api/v1/posts/{postId}/comments", testPost.getId())
172171
.contentType(MediaType.APPLICATION_JSON))
173172
.andExpect(status().isOk())
174-
.andExpect(jsonPath("$.data.items").isArray())
175-
.andExpect(jsonPath("$.data.items.length()").value(0))
176-
.andExpect(jsonPath("$.data.totalElements").value(0))
177-
.andExpect(jsonPath("$.data.totalPages").value(0));
173+
.andExpect(jsonPath("$.items").isArray())
174+
.andExpect(jsonPath("$.items.length()").value(0))
175+
.andExpect(jsonPath("$.totalElements").value(0))
176+
.andExpect(jsonPath("$.totalPages").value(0));
178177
}
179178

180179
@Test
@@ -202,8 +201,7 @@ void updateComment_Success() throws Exception {
202201
.contentType(MediaType.APPLICATION_JSON)
203202
.content(toJson(request)))
204203
.andExpect(status().isOk())
205-
.andExpect(jsonPath("$.message").value("성공적으로 수정되었습니다."))
206-
.andExpect(jsonPath("$.data").value(testComment.getId()));
204+
.andExpect(jsonPath("$").value(testComment.getId()));
207205

208206
Comment updatedComment = commentRepository.findById(testComment.getId()).orElseThrow();
209207
assertThat(updatedComment.getContent()).isEqualTo(request.content());
@@ -240,8 +238,7 @@ void deleteComment_Success() throws Exception {
240238
mockMvc.perform(delete("/api/v1/posts/{postId}/comments/{commentId}",
241239
testPost.getId(),
242240
testComment.getId()))
243-
.andExpect(status().isOk())
244-
.andExpect(jsonPath("$.message").value("성공적으로 삭제되었습니다."));
241+
.andExpect(status().isOk());
245242

246243
assertThat(commentRepository.findById(testComment.getId())).isEmpty();
247244
}

0 commit comments

Comments
 (0)