Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -123,4 +123,8 @@ public void updateFormFields(List<FormField> updatedFormFields) {
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 @@ -13,6 +13,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import java.util.List;
import java.util.stream.Stream;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -22,6 +23,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class FormField extends BaseEntity {
private static final String COMMON_SECTION = "공통";
private static final int SINGLE_SECTION_SIZE = 1;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -95,4 +98,32 @@ public void update(FormField updatedField) {
this.section = updatedField.getSection();
this.options = updatedField.getOptions();
}

public Stream<FormField> generateFormFieldsBySection(Form form) {
if (form.isLargerSectionThan(SINGLE_SECTION_SIZE)) {
return expandCommonSectionFormField(form);
}
return Stream.of(this);
}

private Stream<FormField> expandCommonSectionFormField(Form form) {
if (this.section.equals(COMMON_SECTION)) {
return form.getSections().stream()
.filter(section -> !section.equals(COMMON_SECTION))
.map(this::copyWithSection);
}
return Stream.of(this);
}

private FormField copyWithSection(String newSection) {
return FormField.builder()
.question(this.question)
.fieldType(this.fieldType)
.required(this.required)
.fieldOrder(this.fieldOrder)
.section(newSection)
.options(this.options)
.form(this.form)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void createForm(CreateFormCommand createFormCommand) {

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

List<FormField> formFields = toCreateFormFields(savedForm,
createFormCommand.formFieldCommands());
formFieldService.createAll(formFields);
Expand Down Expand Up @@ -249,12 +248,15 @@ 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> toCreateFormFields(Form savedForm,
List<CreateFormFieldCommand> createFormFieldCommands) {
return createFormFieldCommands.stream()
.map(formFieldCommand -> formFieldCommand.toEntity(savedForm)).toList();
.map(formFieldCommand -> formFieldCommand.toEntity(savedForm))
.flatMap(formField -> formField.generateFormFieldsBySection(savedForm))
.toList();
}
}
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