Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ public boolean isEqualsById(Long formId) {
public void updateEndDate(LocalDate endDate) {
this.endDate = endDate;
}

public boolean isLargerSectionThan(int sectionSize) {
return this.sections.size() > sectionSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -52,6 +53,9 @@
@Slf4j
public class FacadeCentralFormServiceImpl implements FacadeCentralFormService {

private static final int SINGLE_SECTION_SIZE = 1;
private static final String COMMON_SECTION = "공통";

private final FormService formService;
private final FormFieldService formFieldService;
private final ClubService clubService;
Expand All @@ -68,8 +72,7 @@ public void createForm(CreateFormCommand createFormCommand) {

Form form = createFormCommand.toEntity(club);
Form savedForm = formService.create(form);

List<FormField> formFields = toCreateFormFields(savedForm,
List<FormField> formFields = buildFormFieldBySection(savedForm,
createFormCommand.formFieldCommands());
formFieldService.createAll(formFields);
}
Expand Down Expand Up @@ -254,12 +257,41 @@ private void validateEndDate(LocalDate startDate, LocalDate endDate) {
private List<FormField> toUpdateFormFields(Form originform,
List<UpdateFormFieldCommand> updateFormFieldCommands) {
return updateFormFieldCommands.stream()
.map(formFieldCommand -> formFieldCommand.toEntity(originform)).toList();
.map(formFieldCommand -> formFieldCommand.toEntity(originform))
.toList();
}

private List<FormField> buildFormFieldBySection(Form savedForm,
List<CreateFormFieldCommand> createFormFieldCommand) {
if (savedForm.isLargerSectionThan(SINGLE_SECTION_SIZE)) {
return toCreateFormFieldsForMultipleSections(savedForm, createFormFieldCommand);
} else {
return toCreateFormFieldsForSingleSection(savedForm, createFormFieldCommand);
}
}

private List<FormField> toCreateFormFields(Form savedForm,
List<CreateFormFieldCommand> createFormFieldCommands) {
private List<FormField> toCreateFormFieldsForSingleSection(Form savedForm,
List<CreateFormFieldCommand> createFormFieldCommands) {
return createFormFieldCommands.stream()
.map(formFieldCommand -> formFieldCommand.toEntity(savedForm)).toList();
.map(formFieldCommand -> formFieldCommand.toEntity(savedForm))
.toList();
Comment on lines +264 to +260
Copy link
Copy Markdown
Collaborator

@5uhwann 5uhwann Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4)

Suggested change
.map(formFieldCommand -> formFieldCommand.toEntity(savedForm))
.toList();
.map(formFieldCommand -> formFieldCommand.toEntity(savedForm))
.flatMap(formField -> formField.generateFormFieldsBySection(form))
.toList();

createForm() 메서드의 스트림과 합쳐도 괜찮을거 같습니다!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헙 그러네요 수정하였습니다!

}

private List<FormField> toCreateFormFieldsForMultipleSections(Form savedForm,
List<CreateFormFieldCommand> createFormFieldCommands) {
return createFormFieldCommands.stream()
.flatMap(formFieldCommand -> createFormFieldsBySection(savedForm, formFieldCommand))
.toList();
Comment on lines +271 to +260
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3)
뎁스가 3인데,
flatMap -> if -> stream
가독성이 떨어져서 최대한 리팩토링 해주시면 좋을 것 같아요.

그리고 "공통"도 상수처리 하면 좋을 것 같네요

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private 메서드 두 개로 나누어보았습니다. 한 번 더 나눌까 생각했지만 그러면 너무 많은 메서드 때문에 오히려 가독성이 떨어질 것 같기도 해서... 어떻게 생각하시는지 말씀해주실 수 있을까요?

상수처리 완료했습니다!

}

private Stream<FormField> createFormFieldsBySection(Form savedForm,
CreateFormFieldCommand formFieldCommand) {
if (formFieldCommand.section().equals(COMMON_SECTION)) {
return savedForm.getSections().stream()
.filter(section -> !section.equals(COMMON_SECTION))
.map(section -> formFieldCommand.toEntityWithSection(savedForm, section));
}
return Stream.of(formFieldCommand.toEntityWithSection(savedForm, formFieldCommand.section()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public FormField toEntity(Form savedForm) {
.form(savedForm)
.build();
}

public FormField toEntityWithSection(Form savedForm, String section) {
return FormField.builder()
.question(question)
.fieldType(type)
.options(options)
.required(required)
.fieldOrder(order)
.section(section)
.form(savedForm)
.build();
}

}

public Form toEntity(Club club) {
Expand Down