#14 simple blog : use builder
This commit is contained in:
@@ -17,7 +17,10 @@ public class ExceptionController {
|
||||
public ErrorResponse invalidRequestHandler(MethodArgumentNotValidException e) {
|
||||
|
||||
// if (e.hasErrors()) {
|
||||
ErrorResponse response = new ErrorResponse("400", "잘못된 요청입니다.");
|
||||
ErrorResponse response = ErrorResponse.builder()
|
||||
.code("400")
|
||||
.message("잘못된 요청입니다.")
|
||||
.build();
|
||||
|
||||
e.getFieldErrors().forEach(fieldError -> {
|
||||
response.addValidation(fieldError.getField(), fieldError.getDefaultMessage());
|
||||
|
||||
@@ -18,8 +18,7 @@ public class PostController {
|
||||
private final PostService postService;
|
||||
|
||||
@PostMapping("/posts")
|
||||
public PostCreate post(@RequestBody @Valid PostCreate request) {
|
||||
public void post(@RequestBody @Valid PostCreate request) {
|
||||
postService.write(request);
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.example.simpleblog.domain;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@@ -20,6 +21,7 @@ public class Post {
|
||||
@Lob
|
||||
private String content;
|
||||
|
||||
@Builder
|
||||
public Post(String title, String content) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
package com.example.simpleblog.request;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class PostCreate {
|
||||
|
||||
@NotBlank(message = "제목을 입력해주세요.")
|
||||
private String title;
|
||||
private final String title;
|
||||
|
||||
@NotBlank(message = "내용을 입력해주세요.")
|
||||
private String content;
|
||||
private final String content;
|
||||
|
||||
/**
|
||||
* Builder
|
||||
* - 가독성
|
||||
* - 필요한 값만 받아서 생성 가능
|
||||
* - 객체의 불변성
|
||||
*/
|
||||
@Builder
|
||||
public PostCreate(String title, String content) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.example.simpleblog.response;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -16,7 +16,6 @@ import java.util.Map;
|
||||
* }
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class ErrorResponse {
|
||||
|
||||
private final String code;
|
||||
@@ -24,6 +23,12 @@ public class ErrorResponse {
|
||||
|
||||
private final Map<String, String> validation = new HashMap<>();
|
||||
|
||||
@Builder
|
||||
public ErrorResponse(String code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void addValidation(String field, String errorMessage) {
|
||||
this.validation.put(field, errorMessage);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,11 @@ public class PostService {
|
||||
|
||||
public void write(PostCreate postCreate) {
|
||||
|
||||
Post post = new Post(postCreate.getTitle(), postCreate.getContent());
|
||||
Post post = Post.builder()
|
||||
.title(postCreate.getTitle())
|
||||
.content(postCreate.getContent())
|
||||
.build();
|
||||
|
||||
postRepository.save(post);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
@@ -39,28 +39,37 @@ class PostControllerTest {
|
||||
@Test
|
||||
@DisplayName("/posts 요청시 저장된 값을 출력한다.")
|
||||
void posts() throws Exception {
|
||||
PostCreate request = new PostCreate("글 제목입니다.", "글 내용입니다.");
|
||||
// given
|
||||
PostCreate request = PostCreate.builder()
|
||||
.title("글 제목입니다.")
|
||||
.content("글 내용입니다.")
|
||||
.build();
|
||||
|
||||
String jsonString = objectMapper.writeValueAsString(request);
|
||||
|
||||
// expected
|
||||
mockMvc.perform(post("/posts")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.contentType(APPLICATION_JSON)
|
||||
.content(jsonString)
|
||||
)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json(jsonString))
|
||||
.andExpect(content().string(""))
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("/posts 요청시 title값은 필수다.")
|
||||
void posts_validation() throws Exception {
|
||||
PostCreate request = new PostCreate("", "글 내용입니다.");
|
||||
// given
|
||||
PostCreate request = PostCreate.builder()
|
||||
.content("글 내용입니다.")
|
||||
.build();
|
||||
|
||||
String jsonString = objectMapper.writeValueAsString(request);
|
||||
|
||||
// expected
|
||||
mockMvc.perform(post("/posts")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.contentType(APPLICATION_JSON)
|
||||
.content(jsonString)
|
||||
)
|
||||
.andExpect(status().isBadRequest())
|
||||
@@ -74,12 +83,17 @@ class PostControllerTest {
|
||||
@Test
|
||||
@DisplayName("/posts 요청시 DB에 값이 저장된다.")
|
||||
void save_post() throws Exception {
|
||||
PostCreate request = new PostCreate("글 제목입니다.", "글 내용입니다.");
|
||||
// given
|
||||
PostCreate request = PostCreate.builder()
|
||||
.title("글 제목입니다.")
|
||||
.content("글 내용입니다.")
|
||||
.build();
|
||||
|
||||
String jsonString = objectMapper.writeValueAsString(request);
|
||||
|
||||
// when
|
||||
mockMvc.perform(post("/posts")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.contentType(APPLICATION_JSON)
|
||||
.content(jsonString)
|
||||
)
|
||||
.andExpect(status().isOk())
|
||||
|
||||
Reference in New Issue
Block a user