Skip to content

Commit

Permalink
Merge pull request #27 from team-crews/fix/#26-update-recruitment
Browse files Browse the repository at this point in the history
모집 공고 update 기능 오류 해결
  • Loading branch information
jongmee authored Aug 17, 2024
2 parents 82df04e + e777d78 commit 6da252b
Show file tree
Hide file tree
Showing 22 changed files with 171 additions and 87 deletions.
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

>description
## To improve 💬
## Issue 💬

개선사항
#
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public RecruitmentDetailsResponse saveRecruitment(Long publisherId, RecruitmentS
.orElseThrow(() -> new CrewsException(ErrorCode.USER_NOT_FOUND));
String code = UUID.randomUUID().toString();
Recruitment recruitment = request.toRecruitment(code, publisher);
recruitmentRepository.findByPublisher(publisherId)
.ifPresent(existingRecruitment -> recruitment.setByExistingId(existingRecruitment.getId()));
recruitmentRepository.save(recruitment);
return RecruitmentDetailsResponse.from(recruitment);
return RecruitmentDetailsResponse.from(recruitmentRepository.save(recruitment));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class Choice {
@Column(name = "content", nullable = false)
private String content;

public Choice(String content) {
public Choice(Long id, String content) {
this.id = id;
this.content = content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class NarrativeQuestion {
@Column(name = "word_limit", nullable = false)
private Integer wordLimit;

public NarrativeQuestion(String content, Boolean necessity, Integer order, Integer wordLimit) {
public NarrativeQuestion(Long id, String content, Boolean necessity, Integer order, Integer wordLimit) {
this.id = id;
this.content = content;
this.necessity = necessity;
this.order = order;
Expand All @@ -52,4 +53,8 @@ public NarrativeQuestion(String content, Boolean necessity, Integer order, Integ
public void updateSection(final Section section) {
this.section = section;
}

public void setByExistingId(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ public class Recruitment {
@Column(name = "created_date", updatable = false, nullable = false)
private LocalDateTime createdDate;

public Recruitment(String code, String title, String description, LocalDateTime closingDate,
public Recruitment(Long id, String code, String title, String description, LocalDateTime closingDate,
Administrator publisher, List<Section> sections) {
validateClosingDate(closingDate);
this.id = id;
this.code = code;
this.title = title;
this.description = description;
Expand Down Expand Up @@ -100,10 +101,6 @@ public void updateClosingDate(LocalDateTime closingDate) {
this.closingDate = closingDate;
}

public void setByExistingId(Long id) {
this.id = id;
}

public boolean isAnnounced() {
return this.progress == Progress.ANNOUNCED;
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/server/crews/recruitment/domain/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
@Entity
@Table(name = "section")
Expand All @@ -45,15 +44,16 @@ public class Section {
@OneToMany(mappedBy = "section", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SelectiveQuestion> selectiveQuestions;

public Section(
String name, String description,
List<NarrativeQuestion> narrativeQuestions, List<SelectiveQuestion> selectiveQuestions) {
public Section(Long id, String name, String description, List<NarrativeQuestion> narrativeQuestions,
List<SelectiveQuestion> selectiveQuestions) {
this.id = id;
this.name = name;
this.description = description;
replaceQuestions(narrativeQuestions, selectiveQuestions);
}

public void replaceQuestions(List<NarrativeQuestion> narrativeQuestions, List<SelectiveQuestion> selectiveQuestions) {
public void replaceQuestions(List<NarrativeQuestion> narrativeQuestions,
List<SelectiveQuestion> selectiveQuestions) {
narrativeQuestions.forEach(narrativeQuestion -> narrativeQuestion.updateSection(this));
this.narrativeQuestions = new ArrayList<>(narrativeQuestions);
selectiveQuestions.forEach(selectiveQuestion -> selectiveQuestion.updateSection(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
@Entity
@Table(name = "selective_question")
Expand Down Expand Up @@ -51,10 +50,10 @@ public class SelectiveQuestion {
@Column(name = "maximum_selection", nullable = false)
private Integer maximumSelection;

public SelectiveQuestion(
List<Choice> choices, String content, Boolean necessity,
Integer order, Integer minimumSelection, Integer maximumSelection) {
public SelectiveQuestion(Long id, List<Choice> choices, String content, Boolean necessity, Integer order,
Integer minimumSelection, Integer maximumSelection) {
choices.forEach(choice -> choice.updateSelectiveQuestion(this));
this.id = id;
this.choices = new ArrayList<>(choices);
this.content = content;
this.necessity = necessity;
Expand All @@ -66,4 +65,8 @@ public SelectiveQuestion(
public void updateSection(final Section section) {
this.section = section;
}

public void setByExistingId(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.server.crews.recruitment.dto.request;

import com.server.crews.recruitment.domain.Choice;

public record ChoiceSaveRequest(Long id, String content) {

public Choice toEntity() {
return new Choice(id, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;

public record QuestionSaveRequest(
Long id,
@NotBlank(message = "질문 타입은 공백일 수 없습니다.")
@QuestionTypeFormat
String type,
Expand All @@ -21,20 +22,22 @@ public record QuestionSaveRequest(
Integer wordLimit,
Integer minimumSelection,
Integer maximumSelection,
List<String> choices
List<ChoiceSaveRequest> choices
) {

public SelectiveQuestion createSelectiveQuestion() {
return new SelectiveQuestion(createsChoices(), content, necessity, order, minimumSelection, maximumSelection);
return new SelectiveQuestion(id, createsChoices(), content, necessity, order, minimumSelection,
maximumSelection);
}

private List<Choice> createsChoices() {
return choices.stream()
.map(Choice::new)
.map(ChoiceSaveRequest::toEntity)
.toList();
}

public NarrativeQuestion createNarrativeQuestion() {
return new NarrativeQuestion(content, necessity, order, wordLimit);
return new NarrativeQuestion(id, content, necessity, order, wordLimit);
}

public boolean isSelective() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@
import java.util.List;

public record RecruitmentSaveRequest(
Long id,
@NotBlank(message = "모집공고 제목은 공백일 수 없습니다.")
String title,
String description,
@Valid
List<SectionsSaveRequest> sections,
List<SectionSaveRequest> sections,
@NotNull(message = "모집 마감일은 null일 수 없습니다.")
@DateTimeFormat
String closingDate
) {
public Recruitment toRecruitment(String code, Administrator publisher) {
LocalDateTime closingDateTime = LocalDateTime.parse(closingDate);
return new Recruitment(code, title, description, closingDateTime, publisher, toSections());
return new Recruitment(id, code, title, description, closingDateTime, publisher, toSections());
}

public List<Section> toSections() {
if (sections == null) {
return List.of();
}
return sections.stream()
.map(SectionsSaveRequest::toEntity)
.map(SectionSaveRequest::toEntity)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
import jakarta.validation.constraints.NotBlank;
import java.util.List;

public record SectionsSaveRequest(
public record SectionSaveRequest(
Long id,
@NotBlank(message = "섹션 이름은 공백일 수 없습니다.")
String name,
String description,
@Valid
List<QuestionSaveRequest> questions
) {

public Section toEntity() {
return new Section(name, description, narrativeQuestions(), selectiveQuestions());
return new Section(id, name, description, narrativeQuestions(), selectiveQuestions());
}

private List<NarrativeQuestion> narrativeQuestions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
import lombok.Builder;

@Builder
public record NarrativeQuestionResponse(String content, Boolean necessity, Integer order, Integer wordLimit) {
public record NarrativeQuestionResponse(
Long id,
String content,
Boolean necessity,
Integer order,
Integer wordLimit
) {

public static NarrativeQuestionResponse from(final NarrativeQuestion question) {
return NarrativeQuestionResponse.builder()
.id(question.getId())
.content(question.getContent())
.necessity(question.getNecessity())
.order(question.getOrder())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.server.crews.recruitment.domain.NarrativeQuestion;
import com.server.crews.recruitment.domain.Section;
import com.server.crews.recruitment.domain.SelectiveQuestion;
import lombok.Builder;

import java.util.List;
import lombok.Builder;

@Builder
public record SectionResponse(
String name, String description,
Long id,
String name,
String description,
List<NarrativeQuestionResponse> narrativeQuestions,
List<SelectiveQuestionResponse> selectiveQuestions) {

Expand All @@ -20,20 +21,23 @@ public static SectionResponse of(Section section) {
public static SectionResponse of(
Section section, List<NarrativeQuestion> narrativeQuestions, List<SelectiveQuestion> selectiveQuestions) {
return SectionResponse.builder()
.id(section.getId())
.name(section.getName())
.description(section.getDescription())
.narrativeQuestions(narrativeQuestionResponses(narrativeQuestions))
.selectiveQuestions(selectiveQuestionResponses(selectiveQuestions))
.build();
}

private static List<NarrativeQuestionResponse> narrativeQuestionResponses(List<NarrativeQuestion> narrativeQuestions) {
private static List<NarrativeQuestionResponse> narrativeQuestionResponses(
List<NarrativeQuestion> narrativeQuestions) {
return narrativeQuestions.stream()
.map(NarrativeQuestionResponse::from)
.toList();
}

private static List<SelectiveQuestionResponse> selectiveQuestionResponses(List<SelectiveQuestion> selectiveQuestions) {
private static List<SelectiveQuestionResponse> selectiveQuestionResponses(
List<SelectiveQuestion> selectiveQuestions) {
return selectiveQuestions.stream()
.map(SelectiveQuestionResponse::from)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import com.server.crews.recruitment.domain.Choice;
import com.server.crews.recruitment.domain.SelectiveQuestion;
import lombok.Builder;

import java.util.List;
import lombok.Builder;

@Builder
public record SelectiveQuestionResponse(
List<ChoiceResponse> choices, String content,
Boolean necessity, Integer order,
Integer minimumSelection, Integer maximumSelection) {
Long id,
List<ChoiceResponse> choices,
String content,
Boolean necessity,
Integer order,
Integer minimumSelection,
Integer maximumSelection
) {

public static SelectiveQuestionResponse from(final SelectiveQuestion question) {
return SelectiveQuestionResponse.builder()
.id(question.getId())
.choices(choiceResponses(question.getChoices()))
.content(question.getContent())
.necessity(question.getNecessity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public interface RecruitmentRepository extends JpaRepository<Recruitment, Long>,
""")
Optional<Recruitment> findByPublisher(@Param("publisherId") Long publisherId);

@Query("""
SELECT r FROM Recruitment r
JOIN FETCH r.sections
WHERE r.publisher.id = :publisherId
""")
Optional<Recruitment> findWithSectionsByPublisher(@Param("publisherId") Long publisherId);

@Query("""
SELECT r FROM Recruitment r
join fetch r.publisher p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public TestRecruitment(ServiceTestEnviron environ) {
}

public TestRecruitment create(String code, String clubName, Administrator publisher) {
Recruitment recruitment = new Recruitment(code, clubName + " 99기 모집", DEFAULT_DESCRIPTION,
Recruitment recruitment = new Recruitment(null, code, clubName + " 99기 모집", DEFAULT_DESCRIPTION,
DEFAULT_CLOSING_DATE, publisher, List.of());
this.recruitment = environ.recruitmentRepository().save(recruitment);
return this;
}

public TestRecruitment addSection(String name, List<NarrativeQuestion> narrativeQuestions,
List<SelectiveQuestion> selectiveQuestions) {
Section section = new Section(name, DEFAULT_DESCRIPTION, narrativeQuestions, selectiveQuestions);
Section section = new Section(null, name, DEFAULT_DESCRIPTION, narrativeQuestions, selectiveQuestions);
section.updateRecruitment(this.recruitment);
Section savedSection = environ.sectionRepository().save(section);
List<NarrativeQuestion> savedNarrativeQuestions = environ.narrativeQuestionRepository().saveAll(narrativeQuestions);
Expand Down
Loading

0 comments on commit 6da252b

Please sign in to comment.