• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

Editor 질문

22.09.16 20:34 작성 조회수 436

0

아래글들을 참고하여 @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 + ")";
        }
    }
}

 

정말 죄송합니다. 그리고 감사합니다. 해당 내용은 보충 영상으로 올리도록 하겠습니다.