#14 simple blog : add response class
This commit is contained in:
@@ -2,6 +2,7 @@ package com.example.simpleblog.controller;
|
|||||||
|
|
||||||
import com.example.simpleblog.domain.Post;
|
import com.example.simpleblog.domain.Post;
|
||||||
import com.example.simpleblog.request.PostCreate;
|
import com.example.simpleblog.request.PostCreate;
|
||||||
|
import com.example.simpleblog.response.PostResponse;
|
||||||
import com.example.simpleblog.service.PostService;
|
import com.example.simpleblog.service.PostService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -22,7 +23,7 @@ public class PostController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/posts/{postId}")
|
@GetMapping("/posts/{postId}")
|
||||||
public Post getPost(@PathVariable(name = "postId") Long id) {
|
public PostResponse getPost(@PathVariable(name = "postId") Long id) {
|
||||||
return postService.getPost(id);
|
return postService.getPost(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.example.simpleblog.response;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 서비스 정책에 맞는 클래스
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public class PostResponse {
|
||||||
|
|
||||||
|
private final Long id;
|
||||||
|
private final String title;
|
||||||
|
private final String content;
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
|
||||||
|
public PostResponse(Long id, String title, String content) {
|
||||||
|
this.id = id;
|
||||||
|
this.title = title.substring(0 ,Math.min(title.length(), 10));
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package com.example.simpleblog.service;
|
|||||||
import com.example.simpleblog.domain.Post;
|
import com.example.simpleblog.domain.Post;
|
||||||
import com.example.simpleblog.repository.PostRepository;
|
import com.example.simpleblog.repository.PostRepository;
|
||||||
import com.example.simpleblog.request.PostCreate;
|
import com.example.simpleblog.request.PostCreate;
|
||||||
|
import com.example.simpleblog.response.PostResponse;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -24,8 +25,14 @@ public class PostService {
|
|||||||
postRepository.save(post);
|
postRepository.save(post);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Post getPost(Long id) {
|
public PostResponse getPost(Long id) {
|
||||||
return postRepository.findById(id)
|
Post postEntity = postRepository.findById(id)
|
||||||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 글입니다."));
|
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 글입니다."));
|
||||||
|
|
||||||
|
return PostResponse.builder()
|
||||||
|
.id(postEntity.getId())
|
||||||
|
.title(postEntity.getTitle())
|
||||||
|
.content(postEntity.getContent())
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,19 +114,21 @@ class PostControllerTest {
|
|||||||
void get_post_by_id_success() throws Exception {
|
void get_post_by_id_success() throws Exception {
|
||||||
// given
|
// given
|
||||||
Post post = Post.builder()
|
Post post = Post.builder()
|
||||||
.title("글 제목입니다.")
|
.title("123456789012345")
|
||||||
.content("글 내용입니다.")
|
.content("글 내용입니다.")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
postRepository.save(post);
|
postRepository.save(post);
|
||||||
|
|
||||||
|
// json 응답에서 title 값 길이를 최대 10글자
|
||||||
|
|
||||||
// expected
|
// expected
|
||||||
mockMvc.perform(get("/posts/{postId}", post.getId())
|
mockMvc.perform(get("/posts/{postId}", post.getId())
|
||||||
.contentType(APPLICATION_JSON)
|
.contentType(APPLICATION_JSON)
|
||||||
)
|
)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$.id").value(post.getId()))
|
.andExpect(jsonPath("$.id").value(post.getId()))
|
||||||
.andExpect(jsonPath("$.title").value("글 제목입니다."))
|
.andExpect(jsonPath("$.title").value("1234567890"))
|
||||||
.andExpect(jsonPath("$.content").value("글 내용입니다."))
|
.andExpect(jsonPath("$.content").value("글 내용입니다."))
|
||||||
.andDo(print())
|
.andDo(print())
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.example.simpleblog.service;
|
|||||||
import com.example.simpleblog.domain.Post;
|
import com.example.simpleblog.domain.Post;
|
||||||
import com.example.simpleblog.repository.PostRepository;
|
import com.example.simpleblog.repository.PostRepository;
|
||||||
import com.example.simpleblog.request.PostCreate;
|
import com.example.simpleblog.request.PostCreate;
|
||||||
|
import com.example.simpleblog.response.PostResponse;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -59,11 +60,11 @@ class PostServiceTest {
|
|||||||
Long postId = 1L;
|
Long postId = 1L;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
Post postEntity = postService.getPost(postId);
|
PostResponse response = postService.getPost(postId);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertNotNull(post);
|
assertNotNull(post);
|
||||||
assertEquals("글 제목입니다.", postEntity.getTitle());
|
assertEquals("글 제목입니다.", response.getTitle());
|
||||||
assertEquals("글 내용입니다.", postEntity.getContent());
|
assertEquals("글 내용입니다.", response.getContent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user