// controller
@GetMapping("/")
public ResponseEntity<?> getApplication(@RequestParam(required = true) String stepId,
@RequestParam(required = true) String applicantId) {
try {
GetApplicationResponse response = applicationService.findApplicationByApplicantId(stepId, applicantId);
return ResponseEntity.status(HttpStatus.OK).body(response);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
// service
@Override
@Transactional
public GetApplicationResponse findApplicationByApplicantId(String stepId, String applicantId) {
//0. 해당 접근자가, 회장인지 동아리원인지 판단.
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetail userDetails = (CustomUserDetail) authentication.getPrincipal();
User user = userRepository.findById(userDetails.getId())
.orElseThrow(() -> new NoSuchElementException("User not found"));
Club club = stepRepository.findById(stepId)
.orElseThrow(() -> new NoSuchElementException("Step not found"))
.getRecruitment().getClub();
UserClubRole userClubRole = userClubRoleRepository.findByClubAndUser(club, user)
.orElseThrow(() -> new NoSuchElementException("User is not in Club"));
//1. stepId(해당 전형 하위 step)와 applicantId(지원자id)로 지원서 찾기
Applicant applicant = applicantRepository.findById(applicantId)
.orElseThrow(() -> new NoSuchElementException("Applicant not found"));
Step step = stepRepository.findById(stepId)
.orElseThrow(() -> new NoSuchElementException("Step not found"));
//굳이 step과 applicant로 찾을 필요없이, applicant하나로 조회가능하다. 이부분 수정함
Application application = applicationRepository.findByApplicantAndStep(applicant, step)
.orElseThrow(() -> new NoSuchElementException("Application not found"));
//2-1. 회장에게만, 필수 응답값(지원자정보) DTO생성
RequiredFieldDto requiredFieldAnswerDto = applicant.toRequiredFieldDto();
//2-2. 질문 및 답변값 DTO 생성
List<GetApplicationResponse.QuestionAnswerDto> questionAnswerDtos = new ArrayList<>();
List<Answer> answers = answerRepository.findAllByApplication(application);
if (answers.isEmpty())
throw new IllegalArgumentException("Answers cannot be empty");
for (Answer answer : answers) {
Question question = questionRepository.findById(answer.getQuestion().getId())
.orElseThrow(() -> new NoSuchElementException("Question not found"));
//동아리원 접근 제한 응답 확인
if (userClubRole.getClubRole() == ClubRole.MEMBER && !question.isAccessible())
continue;
String answerText = getAnswerText(question, answer);
GetApplicationResponse.QuestionAnswerDto questionAnswerDto = GetApplicationResponse.QuestionAnswerDto.builder()
.questionId(question.getId())
.questionOrder(question.getQuestionOrder())
.questionText(question.getQuestionText())
.questionType(question.getQuestionType())
.answer(answerText)
.build();
questionAnswerDtos.add(questionAnswerDto);
}
//2-1. 질문 순서에 맞게 정렬
SortUtils.sortList(questionAnswerDtos, Comparator.comparing(GetApplicationResponse.QuestionAnswerDto::questionOrder));
//3. 전체 지원서 반환값 생성
return GetApplicationResponse.builder()
.applicantId(applicant.getId())
.requiredFieldAnswerDto(userClubRole.getClubRole() == ClubRole.PRESIDENT ? requiredFieldAnswerDto : null)
.questionAnswerDtos(questionAnswerDtos)
.build();
}
// controller
@GetMapping("/")
public ResponseEntity<?> getApplication(@RequestParam(required = true) String stepId,
@RequestParam(required = true) List<String> applicantIdList) {
try {
List<GetApplicationResponse> response = applicationService.findApplicationByApplicantId(stepId, applicantIdList);
return ResponseEntity.status(HttpStatus.OK).body(response);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
@Override
@Transactional
public List<GetApplicationResponse> findApplicationByApplicantId(String stepId, List<String> applicantId) {
//0. 해당 접근자가, 회장인지 동아리원인지 판단.
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetail userDetails = (CustomUserDetail) authentication.getPrincipal();
User user = userRepository.findById(userDetails.getId())
.orElseThrow(() -> new NoSuchElementException("User not found"));
Club club = stepRepository.findById(stepId)
.orElseThrow(() -> new NoSuchElementException("Step not found"))
.getRecruitment().getClub();
UserClubRole userClubRole = userClubRoleRepository.findByClubAndUser(club, user)
.orElseThrow(() -> new NoSuchElementException("User is not in Club"));
//1. stepId(해당 전형 하위 step)와 applicantId(지원자id)로 지원서 찾기
List<Applicant> applicants = applicantRepository.findAllById(applicantId);
if (applicants.isEmpty())
throw new IllegalArgumentException("Applicant not found");
List<Application> applications = applicationRepository.findAllByApplicantIn(applicants);
if (applications.isEmpty())
throw new IllegalArgumentException("application not found");
List<GetApplicationResponse> getApplicationResponses = new ArrayList<>();
for (Application application : applications) {
//2. 질문 및 답변값 DTO 생성
List<GetApplicationResponse.QuestionAnswerDto> questionAnswerDtos = new ArrayList<>();
List<Answer> answers = answerRepository.findAllByApplication(application);
if (answers.isEmpty())
throw new IllegalArgumentException("Answers cannot be empty");
for (Answer answer : answers) {
Question question = questionRepository.findById(answer.getQuestion().getId())
.orElseThrow(() -> new NoSuchElementException("Question not found"));
//동아리원 접근 제한 응답 확인
if (userClubRole.getClubRole() == ClubRole.MEMBER && !question.isAccessible())
continue;
String answerText = getAnswerText(question, answer);
GetApplicationResponse.QuestionAnswerDto questionAnswerDto = GetApplicationResponse.QuestionAnswerDto.builder()
.questionId(question.getId())
.questionOrder(question.getQuestionOrder())
.questionText(question.getQuestionText())
.questionType(question.getQuestionType())
.answer(answerText)
.build();
questionAnswerDtos.add(questionAnswerDto);
}
//2-1. 질문 순서에 맞게 정렬
SortUtils.sortList(questionAnswerDtos, Comparator.comparing(GetApplicationResponse.QuestionAnswerDto::questionOrder));
//2-2. 회장에게만, 필수 응답값(지원자정보) DTO생성
RequiredFieldDto requiredFieldAnswerDto;
if (userClubRole.getClubRole() == ClubRole.PRESIDENT)
requiredFieldAnswerDto = application.getApplicant().toFullRequiredFieldDto();
else
requiredFieldAnswerDto = application.getApplicant().toNameOnlyRequiredFieldDto();
//3. 전체 지원서 반환값 생성
GetApplicationResponse getApplicationResponse = GetApplicationResponse.builder()
.applicantId(application.getApplicant().getId())
.requiredFieldAnswerDto(requiredFieldAnswerDto)
.questionAnswerDtos(questionAnswerDtos)
.build();
getApplicationResponses.add(getApplicationResponse);
}
return getApplicationResponses;
}
stepid와 applicantId로 조회하는 findByStepAndApplicant jpa메소드를 findAllByApplicantIn 메소드로 수정
RequiredFieldDto requiredFieldAnswerDto; 를 기존에는 회장만 받아볼 수 있도록 하였지만, 동아리원도 지원자 이름이 필요하므로, toRequiredFieldAnswerDto메소드를 회장이 받아볼때는 toFullRequiredFieldAnswerDto로, 동아리원은 toNameOnlyRequiredFieldAnswerDto 메소드로 생성하는 것으로 수정
public RequiredFieldDto toFullRequiredFieldDto() {
return RequiredFieldDto.builder()
.name(this.name)
.email(this.email)
.phone(this.phone)
.studentId(this.studentId)
.image(this.imageUrl)
.build();
}
public RequiredFieldDto toNameOnlyRequiredFieldDto(){
return RequiredFieldDto.builder()
.name(this.name)
.build();
}