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 @@ -56,11 +56,7 @@ public ResponseEntity<?> editClubApplicationForm(@PathVariable String applicatio
@Operation(summary = "클럽의 모든 지원서 양식 목록 불러오기", description = "클럽의 모든 지원서 양식들을 학기별로 분류하여 불러옵니다")
@PreAuthorize("isAuthenticated()")
@SecurityRequirement(name = "BearerAuth")
public ResponseEntity<?> getClubApplications(@CurrentUser CustomUserDetails user,
@RequestParam(defaultValue = "agg") String mode) { //agg면 aggregation사용, server면, 서비스에서 그룹 및 정렬
if ("server".equalsIgnoreCase(mode)) {
return Response.ok(clubApplyAdminService.getGroupedClubApplicationForms(user));
}
public ResponseEntity<?> getClubApplications(@CurrentUser CustomUserDetails user) {
return Response.ok(clubApplyAdminService.getClubApplicationForms(user));
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import moadong.club.entity.ClubApplicationForm;
import moadong.club.enums.ApplicationFormStatus;
import moadong.club.payload.dto.ClubActiveFormSlim;
import moadong.club.payload.dto.ClubApplicationFormSlim;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
Expand All @@ -19,12 +17,6 @@ public interface ClubApplicationFormsRepository extends MongoRepository<ClubAppl
Optional<ClubApplicationForm> findByClubIdAndId(String clubId, String id);
List<ClubApplicationForm> findByClubId(String clubId);

@Query(
value = "{'clubId': ?0}",
fields = "{'_id': 1, 'title': 1, 'editedAt': 1, 'semesterYear': 1, 'semesterTerm': 1, 'status': 1}"
) //필드 5개만 가져옴
List<ClubApplicationFormSlim> findClubApplicationFormsByClubId(String clubId, Sort sort);

@Query(
value = "{'clubId': ?0, 'status': 'ACTIVE'}",
fields = "{'_id': 1, 'title': 1}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
import moadong.club.repository.ClubApplicantsRepository;
import moadong.club.repository.ClubApplicationFormsRepository;
import moadong.club.repository.ClubApplicationFormsRepositoryCustom;
import moadong.club.repository.ClubRepository;
import moadong.global.exception.ErrorCode;
import moadong.global.exception.RestApiException;
import moadong.global.util.AESCipher;
import moadong.user.payload.CustomUserDetails;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -29,7 +27,6 @@
import org.springframework.transaction.support.TransactionSynchronization;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
Expand All @@ -40,7 +37,6 @@
@AllArgsConstructor
@Slf4j
public class ClubApplyAdminService {
private final ClubRepository clubRepository;
private final ClubApplicationFormsRepository clubApplicationFormsRepository;
private final ClubApplicantsRepository clubApplicantsRepository;
private final AESCipher cipher;
Expand Down Expand Up @@ -83,7 +79,7 @@ private void validateSemester(Integer semesterYear, SemesterTerm semesterTerm) {
boolean allowed = items.stream()
.anyMatch(it -> it.year() == semesterYear && it.term() == semesterTerm);
if (!allowed) {
throw new IllegalArgumentException("Invalid semester selection");
throw new RestApiException(ErrorCode.APPLICATION_SEMESTER_INVALID);
}

}
Expand Down Expand Up @@ -126,67 +122,6 @@ public ClubApplicationFormsResponse getClubApplicationForms(CustomUserDetails us
.build();
}

public ClubApplicationFormsResponse getGroupedClubApplicationForms(CustomUserDetails user) {
Sort sort = Sort.by(Sort.Direction.DESC, "editedAt")
.and(Sort.by(Sort.Direction.DESC, "id"));
List<ClubApplicationFormSlim> questionSlims = clubApplicationFormsRepository.findClubApplicationFormsByClubId(user.getClubId(), sort);

Map<SemesterKey, List<ClubApplicationFormsResultItem>> grouped = new LinkedHashMap<>();
for (ClubApplicationFormSlim s : questionSlims) {
Integer year = s.getSemesterYear();
SemesterTerm term = s.getSemesterTerm();
LocalDateTime editedAt = s.getEditedAt();

grouped.computeIfAbsent(new SemesterKey(year, term), k -> new ArrayList<>())
.add(new ClubApplicationFormsResultItem(
s.getId(),
s.getTitle(),
editedAt,
s.getStatus()

));
}

//그룹 정렬 -> 연도 DESC, 학기순 DESC(SECOND > FIRST)
Comparator<Map.Entry<SemesterKey, List<ClubApplicationFormsResultItem>>> groupComparator =
Comparator.comparing(
(Map.Entry<SemesterKey, List<ClubApplicationFormsResultItem>> e) -> e.getKey().year(),
Comparator.nullsLast(Comparator.reverseOrder()))
.thenComparing(
e -> termRank(e.getKey().term()),
Comparator.nullsLast(Comparator.reverseOrder()))
.thenComparing(
e -> termName(e.getKey().term()),
Comparator.nullsLast(Comparator.reverseOrder()));

return ClubApplicationFormsResponse.builder()
.forms(grouped.entrySet().stream()
.sorted(groupComparator)
.map(e -> new ClubApplicationFormsResult(
e.getKey().year(),
e.getKey().term(),
e.getValue()
))
.collect(Collectors.toList()))
.build();
}

private static int termRank(SemesterTerm term) {
if (term == null) return -1;
return switch (term) {
case SECOND -> 2;
case FIRST -> 1;
default -> 0;
};
}

private static String termName(SemesterTerm term) {
return term == null ? "" : term.name();
}

private record SemesterKey(Integer year, SemesterTerm term) {
}

public ClubApplyInfoResponse getClubApplyInfo(String applicationFormId, CustomUserDetails user) {
ClubApplicationForm applicationForm = clubApplicationFormsRepository.findByClubIdAndId(user.getClubId(), applicationFormId)
.orElseThrow(() -> new RestApiException(ErrorCode.APPLICATION_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum ErrorCode {
QUESTION_NOT_FOUND(HttpStatus.NOT_FOUND, "800-4", "존재하지 않은 질문입니다."),
REQUIRED_QUESTION_MISSING(HttpStatus.BAD_REQUEST, "800-5", "필수 응답 질문이 누락되었습니다."),
ACTIVE_APPLICATION_NOT_FOUND(HttpStatus.NOT_FOUND, "800-6", "활성화된 지원서 양식이 존재하지 않습니다."),
APPLICATION_SEMESTER_INVALID(HttpStatus.BAD_REQUEST, "800-7", "올바르지 않은 학기입니다."),

AES_CIPHER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "900-1", "암호화 중 오류가 발생했습니다."),
APPLICANT_NOT_FOUND(HttpStatus.NOT_FOUND, "900-2", "지원서가 존재하지 않습니다."),
Expand Down
Loading