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
10 changes: 9 additions & 1 deletion backend/src/main/java/moadong/club/entity/ClubApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ public class ClubApplication {
@Builder.Default
LocalDateTime createdAt = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDateTime();

public void updateDetail(String memo, ApplicationStatus status) {
public void updateMemo(String memo) {
this.memo = memo;
}

public void updateStatus(ApplicationStatus status) {
this.status = status;
}

public void updateAnswers(List<ClubQuestionAnswer> answers) {
this.answers.clear();
this.answers.addAll(answers);
}
}
23 changes: 20 additions & 3 deletions backend/src/main/java/moadong/club/service/ClubApplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public ClubApplyInfoResponse getClubApplyInfo(String clubId, CustomUserDetails u
throw new RestApiException(ErrorCode.USER_UNAUTHORIZED);
}

ClubQuestion clubApplication = clubQuestionRepository.findByClubId(clubId)
.orElseThrow(() -> new RestApiException(ErrorCode.APPLICATION_NOT_FOUND));
List<ClubApplication> submittedApplications = clubApplicationRepository.findAllByQuestionId(clubId);

List<ClubApplicantsResult> applications = new ArrayList<>();
Expand All @@ -107,7 +109,8 @@ public ClubApplyInfoResponse getClubApplyInfo(String clubId, CustomUserDetails u
int accepted = 0;

for (ClubApplication app : submittedApplications) {
applications.add(ClubApplicantsResult.of(app, cipher));
ClubApplication sortedApp = sortApplicationAnswers(clubApplication, app);
applications.add(ClubApplicantsResult.of(sortedApp, cipher));

switch (app.getStatus()) {
case SUBMITTED -> reviewRequired++;
Expand All @@ -125,6 +128,19 @@ public ClubApplyInfoResponse getClubApplyInfo(String clubId, CustomUserDetails u
.build();
}

private ClubApplication sortApplicationAnswers(ClubQuestion application, ClubApplication app) {
Map<Long, ClubQuestionAnswer> answerMap = app.getAnswers().stream()
.collect(Collectors.toMap(ClubQuestionAnswer::getId, answer -> answer));

List<ClubQuestionAnswer> sortedAnswers = application.getQuestions().stream()
.map(question -> answerMap.get(question.getId()))
.filter(Objects::nonNull)
.collect(Collectors.toList());

app.updateAnswers(sortedAnswers);
return app;
}
Comment on lines +131 to +142
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

NPE 방지 및 중복 키 안전성 개선

  • app.getAnswers() 또는 application.getQuestions()가 null이면 NPE가 발생합니다. 기존 데이터 호환성을 고려하면 null-safe가 필요합니다.
  • Collectors.toMap은 키가 중복되면 IllegalStateException을 던집니다. 데이터 이상치가 들어와도 안전하게 동작하도록 머지 전략을 지정하는 것을 권장합니다.
-    private ClubApplication sortApplicationAnswers(ClubQuestion application, ClubApplication app) {
-        Map<Long, ClubQuestionAnswer> answerMap = app.getAnswers().stream()
-                .collect(Collectors.toMap(ClubQuestionAnswer::getId, answer -> answer));
-
-        List<ClubQuestionAnswer> sortedAnswers = application.getQuestions().stream()
-                .map(question -> answerMap.get(question.getId()))
-                .filter(Objects::nonNull)
-                .collect(Collectors.toList());
-
-        app.updateAnswers(sortedAnswers);
-        return app;
-    }
+    private ClubApplication sortApplicationAnswers(ClubQuestion clubQuestion, ClubApplication app) {
+        Map<Long, ClubQuestionAnswer> answerMap = Optional.ofNullable(app.getAnswers())
+                .orElseGet(Collections::emptyList)
+                .stream()
+                .collect(Collectors.toMap(
+                        ClubQuestionAnswer::getId,
+                        Function.identity(),
+                        (a, b) -> a // 중복 키 발생 시 첫 번째 값 유지
+                ));
+
+        List<ClubQuestionAnswer> sortedAnswers = Optional.ofNullable(clubQuestion.getQuestions())
+                .orElseGet(Collections::emptyList)
+                .stream()
+                .map(question -> answerMap.get(question.getId()))
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList());
+
+        app.updateAnswers(sortedAnswers);
+        return app;
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private ClubApplication sortApplicationAnswers(ClubQuestion application, ClubApplication app) {
Map<Long, ClubQuestionAnswer> answerMap = app.getAnswers().stream()
.collect(Collectors.toMap(ClubQuestionAnswer::getId, answer -> answer));
List<ClubQuestionAnswer> sortedAnswers = application.getQuestions().stream()
.map(question -> answerMap.get(question.getId()))
.filter(Objects::nonNull)
.collect(Collectors.toList());
app.updateAnswers(sortedAnswers);
return app;
}
private ClubApplication sortApplicationAnswers(ClubQuestion clubQuestion, ClubApplication app) {
Map<Long, ClubQuestionAnswer> answerMap = Optional.ofNullable(app.getAnswers())
.orElseGet(Collections::emptyList)
.stream()
.collect(Collectors.toMap(
ClubQuestionAnswer::getId,
Function.identity(),
(a, b) -> a // 중복 키 발생 시 첫 번째 값 유지
));
List<ClubQuestionAnswer> sortedAnswers = Optional.ofNullable(clubQuestion.getQuestions())
.orElseGet(Collections::emptyList)
.stream()
.map(question -> answerMap.get(question.getId()))
.filter(Objects::nonNull)
.collect(Collectors.toList());
app.updateAnswers(sortedAnswers);
return app;
}
🤖 Prompt for AI Agents
In backend/src/main/java/moadong/club/service/ClubApplyService.java around lines
131 to 142, the method assumes app.getAnswers() and application.getQuestions()
are non-null and uses Collectors.toMap without a merge function; make it
null-safe by treating null answer/question lists as empty lists (e.g., use
Collections.emptyList() or Stream.ofNullable) and build the map with a merge
strategy to avoid IllegalStateException on duplicate keys (e.g., (existing,
incoming) -> existing or incoming). Then map questions to answers using the safe
collections, filter nulls, update the app with the resulting list, and return
it.


@Transactional
public void editApplicantDetail(String clubId, String appId, ClubApplicantEditRequest request, CustomUserDetails user) {
Club club = clubRepository.findById(clubId)
Expand All @@ -137,7 +153,8 @@ public void editApplicantDetail(String clubId, String appId, ClubApplicantEditRe
ClubApplication application = clubApplicationRepository.findByIdAndQuestionId(appId, clubId)
.orElseThrow(() -> new RestApiException(ErrorCode.APPLICANT_NOT_FOUND));

application.updateDetail(request.memo(), request.status());
application.updateMemo(request.memo());
application.updateStatus(request.status());

clubApplicationRepository.save(application);
}
Expand Down Expand Up @@ -287,4 +304,4 @@ private ClubQuestion getClubQuestion(String clubId, CustomUserDetails user) {
.clubId(club.getId())
.build());
}
}
}