#14 simple blog : edit post
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.example.simpleblog.controller;
|
||||
|
||||
import com.example.simpleblog.request.PostCreate;
|
||||
import com.example.simpleblog.request.PostEdit;
|
||||
import com.example.simpleblog.request.PostSearch;
|
||||
import com.example.simpleblog.response.PostResponse;
|
||||
import com.example.simpleblog.service.PostService;
|
||||
@@ -32,4 +33,10 @@ public class PostController {
|
||||
public List<PostResponse> getPostList(PostSearch postSearch) {
|
||||
return postService.getPostList(postSearch);
|
||||
}
|
||||
|
||||
@PatchMapping("/posts/{postId}")
|
||||
public void editPost(@PathVariable Long postId,
|
||||
@RequestBody @Valid PostEdit postEdit) {
|
||||
postService.edit(postId, postEdit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,4 +26,15 @@ public class Post {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public PostEditor.PostEditorBuilder toEditor() {
|
||||
return PostEditor.builder()
|
||||
.title(title)
|
||||
.content(content);
|
||||
}
|
||||
|
||||
public void edit(PostEditor postEditor) {
|
||||
this.title = postEditor.getTitle() == null ? this.title : postEditor.getTitle();
|
||||
this.content = postEditor.getContent() == null ? this.content : postEditor.getContent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.simpleblog.domain;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class PostEditor {
|
||||
|
||||
private final String title;
|
||||
private final String content;
|
||||
|
||||
@Builder
|
||||
public PostEditor(String title, String content) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.simpleblog.request;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Getter
|
||||
public class PostEdit {
|
||||
|
||||
@NotBlank(message = "제목을 입력해주세요.")
|
||||
private final String title;
|
||||
|
||||
@NotBlank(message = "내용을 입력해주세요.")
|
||||
private final String content;
|
||||
|
||||
@Builder
|
||||
public PostEdit(String title, String content) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
package com.example.simpleblog.service;
|
||||
|
||||
import com.example.simpleblog.domain.Post;
|
||||
import com.example.simpleblog.domain.PostEditor;
|
||||
import com.example.simpleblog.repository.PostRepository;
|
||||
import com.example.simpleblog.request.PostCreate;
|
||||
import com.example.simpleblog.request.PostEdit;
|
||||
import com.example.simpleblog.request.PostSearch;
|
||||
import com.example.simpleblog.response.PostResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -46,4 +49,19 @@ public class PostService {
|
||||
.map(PostResponse::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void edit(Long id, PostEdit postEdit) {
|
||||
Post postEntity = postRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 글입니다."));
|
||||
|
||||
PostEditor.PostEditorBuilder postEditorBuilder = postEntity.toEditor();
|
||||
|
||||
PostEditor postEditor = postEditorBuilder
|
||||
.title(postEdit.getTitle())
|
||||
.content(postEdit.getContent())
|
||||
.build();
|
||||
|
||||
postEntity.edit(postEditor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.example.simpleblog.controller;
|
||||
import com.example.simpleblog.domain.Post;
|
||||
import com.example.simpleblog.repository.PostRepository;
|
||||
import com.example.simpleblog.request.PostCreate;
|
||||
import com.example.simpleblog.request.PostEdit;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
@@ -19,8 +20,7 @@ import java.util.stream.IntStream;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
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.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@@ -195,4 +195,29 @@ class PostControllerTest {
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("글 제목 수정")
|
||||
void update_post_title_test() throws Exception {
|
||||
// given
|
||||
Post post = postRepository.save(Post.builder()
|
||||
.title("제목")
|
||||
.content("내용")
|
||||
.build());
|
||||
|
||||
PostEdit postEdit = PostEdit.builder()
|
||||
.title("제목 수정")
|
||||
.content("내용")
|
||||
.build();
|
||||
|
||||
// expected
|
||||
mockMvc.perform(patch("/posts/{postId}", post.getId())
|
||||
.contentType(APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(postEdit))
|
||||
)
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print())
|
||||
;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.example.simpleblog.service;
|
||||
import com.example.simpleblog.domain.Post;
|
||||
import com.example.simpleblog.repository.PostRepository;
|
||||
import com.example.simpleblog.request.PostCreate;
|
||||
import com.example.simpleblog.request.PostEdit;
|
||||
import com.example.simpleblog.request.PostSearch;
|
||||
import com.example.simpleblog.response.PostResponse;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -99,4 +100,55 @@ class PostServiceTest {
|
||||
assertEquals("제목 26", posts.get(4).getTitle());
|
||||
assertEquals("내용 26", posts.get(4).getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("글 제목 수정")
|
||||
void update_post_title_test() {
|
||||
// given
|
||||
|
||||
Post post = postRepository.save(Post.builder()
|
||||
.title("제목")
|
||||
.content("내용")
|
||||
.build());
|
||||
|
||||
PostEdit postEdit = PostEdit.builder()
|
||||
.title("제목 수정")
|
||||
.build();
|
||||
|
||||
|
||||
// when
|
||||
postService.edit(post.getId(), postEdit);
|
||||
|
||||
// then
|
||||
Post changedPost = postRepository.findById(post.getId())
|
||||
.orElseThrow(() -> new RuntimeException("글이 존재하지 않습니다. id=" + post.getId()));
|
||||
|
||||
assertEquals("제목 수정", changedPost.getTitle());
|
||||
assertEquals("내용", changedPost.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("글 내용 수정")
|
||||
void update_post_content_test() {
|
||||
// given
|
||||
Post post = postRepository.save(Post.builder()
|
||||
.title("제목")
|
||||
.content("내용")
|
||||
.build());
|
||||
|
||||
PostEdit postEdit = PostEdit.builder()
|
||||
.content("내용 수정")
|
||||
.build();
|
||||
|
||||
|
||||
// when
|
||||
postService.edit(post.getId(), postEdit);
|
||||
|
||||
// then
|
||||
Post changedPost = postRepository.findById(post.getId())
|
||||
.orElseThrow(() -> new RuntimeException("글이 존재하지 않습니다. id=" + post.getId()));
|
||||
|
||||
assertEquals("제목", changedPost.getTitle());
|
||||
assertEquals("내용 수정", changedPost.getContent());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user