Editor 질문
613
작성한 질문수 2
아래글들을 참고하여 @NotBlank @NotNull 등을 제거하고 다음과 같이 PostEditor를 만들었습니다
@Getter
public class PostEditor {
private String title;
private String content;
@Builder
public PostEditor(String title, String content) {
//this.title = (title != null) ? title : this.title;
//this.content = (content != null) ? content : this.content;
if(title!=null){
this.title = title;
}
if(content!=null){
this.content = content;
}
}
}테스트를 위해
@Test
@DisplayName("글 제목 수정")
void test5(){
//given
Post post = Post.builder()
.title("호들맨")
.content("반포자이")
.build();
postRepository.save(post);
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());
}
하지만 content가 null 값으로 됩니다. ㅜ
service 코드는 강의 내용과 같습니다 무엇이 문제일까요
답변 1
1
안녕하세요. 호돌맨입니다.
질문을 남겨주셔서 감사합니다.
우선 죄송합니다. 제 설명이 잘못되었습니다.
@Builder 생성자는 build()가 될 때 호출됩니다. editorBuilder.content(null)를 호출하며 PostEditor.content에 값이 들어가는 게 아니라 내부 빌더 클래스인 PostEditor.PostEditorBuilder.content에 값이 들어가지게 됩니다. 따라서 @Builder의 build() 생성자에 PostEditor.PostEditorBuilder에서 넘어온 content 값이 null 이면 if문을 통해 막더라도 멤버변수 기본값인 null로 들어가는 게 맞는것으로 보입니다. (예전에는 분명 됐던것 같은데... ㅠㅠ)
따라서 해당 내용은 두 가지 방법으로 수정할 수 있습니다.
1. 빌더에 값 넘길때 체크
@Transactional
public void edit(Long id, PostEdit postEdit) {
...
PostEditor postEditor = editorBuilder.title(postEdit.getTitle() != null ? postEdit.getTitle() : post.getTitle())
.content(postEdit.getContent() != null ? postEdit.getContent() : post.getContent())
.build();
post.edit(postEditor);
}하지만 코드가 장황해지는 문제가 있습니다.
2. 근본적인 문제 PostEditor 수정
package com.hodolog.api.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;
}
public static PostEditor.PostEditorBuilder builder() {
return new PostEditor.PostEditorBuilder();
}
public static class PostEditorBuilder {
private String title;
private String content;
PostEditorBuilder() {
}
public PostEditor.PostEditorBuilder title(final String title) {
if (content != null) { // 여기에서 null 체크
this.title = title;
}
return this;
}
public PostEditor.PostEditorBuilder content(final String content) {
if (content != null) { // 여기에서 null 체크
this.content = content;
}
return this;
}
public PostEditor build() {
return new PostEditor(this.title, this.content);
}
public String toString() {
return "PostEditor.PostEditorBuilder(title=" + this.title + ", content=" + this.content + ")";
}
}
}
정말 죄송합니다. 그리고 감사합니다. 해당 내용은 보충 영상으로 올리도록 하겠습니다.
Deprecated 관련 사항들
0
128
2
깃헙 collaboator 초대 관련
0
104
1
강의 듣다가 도커 이미지 생성시 각각도 가능하나 그렇게 사용하는데가 많은지 모르겠다라는 말을 듣고 남김니다
0
171
2
logout 후에 login 페이지 이동은 어디서 시켜주는건가요?
0
243
1
다중 데이터를 삭제 할 때
0
292
2
querydsl Q class 이슈
0
433
2
Windows WSL Vue 설정
2
255
1
Dip, @transactional
0
199
1
[vite] http proxy error: /auth/login
0
1067
2
로그인 하고 나서 GET요청으로 메인페이지 요청
0
246
2
GitHub Collaborator 초대 관련
0
283
2
Window에서 Vue.js 설정
0
334
2
collaboator로 초대받을 수 있을까요??
0
298
2
SecurityMockContext 로부터 유저 정보를 가져오기
0
276
1
섹션9 프론트의 코드를 보고싶습니다,,,
0
427
1
Spring Security - defaultSuccessUrl 질문
0
639
1
강의 화면이 나오지 않습니다. 음성과 자막만 나와요
0
313
1
JPAQueryFactory(em)의 객체 생성자 오류에 대해서 질문이 있습니다ㅜㅜ
0
702
2
ExceptionHandler가 AccessDeniedHandler(Http403Handler)를 먹어버리는 현상
0
1186
2
섹션10 언제 나오나요?
0
489
1
CommentService에서 Repository를 호출하지 않는데도
0
346
1
Editor....를 활용한 패턴에 질문있습니다.
0
500
1
섹션9 vue
0
460
2
Post에 edit 메서드 삼항연산자 질문
0
480
2





