Skip to content

Commit a6f1e91

Browse files
committed
feat: 지원자 저장 로직 학번 검증 추가
- 이미 지원한 지원자가 다시 지원 시 덮어쓰기 하거나 저장하지 않는 방식으로 구현 - 에러코드 추가
1 parent 8a45d7b commit a6f1e91

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/main/java/dmu/dasom/api/domain/applicant/repository/ApplicantRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import org.springframework.data.jpa.repository.JpaRepository;
77
import org.springframework.data.jpa.repository.Query;
88

9+
import java.util.Optional;
10+
911
public interface ApplicantRepository extends JpaRepository<Applicant, Long> {
1012

1113
@Query("SELECT a FROM Applicant a ORDER BY a.id DESC")
1214
Page<Applicant> findAllWithPageRequest(final Pageable pageable);
1315

16+
Optional<Applicant> findByStudentNo(final String studentNo);
17+
1418
}

src/main/java/dmu/dasom/api/domain/applicant/service/ApplicantServiceImpl.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
import org.springframework.data.domain.Page;
1414
import org.springframework.data.domain.PageRequest;
1515
import org.springframework.stereotype.Service;
16+
import org.springframework.transaction.annotation.Transactional;
17+
18+
import java.util.Optional;
1619

1720
@RequiredArgsConstructor
1821
@Service
22+
@Transactional
1923
public class ApplicantServiceImpl implements ApplicantService {
2024

2125
private final static int DEFAULT_PAGE_SIZE = 20;
@@ -25,6 +29,20 @@ public class ApplicantServiceImpl implements ApplicantService {
2529
// 지원자 저장
2630
@Override
2731
public void apply(final ApplicantCreateRequestDto request) {
32+
final Optional<Applicant> applicant = findByStudentNo(request.getStudentNo());
33+
34+
// 이미 지원한 학번이 존재할 경우
35+
if (applicant.isPresent()) {
36+
// 덮어쓰기 확인 여부가 false일 경우 예외 발생
37+
if (!request.getIsOverwriteConfirmed())
38+
throw new CustomException(ErrorCode.DUPLICATED_STUDENT_NO);
39+
40+
// 기존 지원자 정보 갱신 수행
41+
applicant.get().overwrite(request);
42+
return;
43+
}
44+
45+
// 새로운 지원자일 경우 저장
2846
applicantRepository.save(request.toEntity());
2947
}
3048

@@ -61,4 +79,9 @@ private Applicant findById(final Long id) {
6179
.orElseThrow(() -> new CustomException(ErrorCode.EMPTY_RESULT));
6280
}
6381

82+
// 학번으로 지원자 존재 여부 확인
83+
private Optional<Applicant> findByStudentNo(final String studentNo) {
84+
return applicantRepository.findByStudentNo(studentNo);
85+
}
86+
6487
}

src/main/java/dmu/dasom/api/domain/common/exception/ErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public enum ErrorCode {
1818
INTERNAL_SERVER_ERROR(500, "C009", "서버에 문제가 발생하였습니다."),
1919
NOT_FOUND(404, "C010", "해당 리소스를 찾을 수 없습니다."),
2020
WRITE_FAIL(400, "C011", "데이터를 쓰는데 실패하였습니다."),
21-
EMPTY_RESULT(400, "C012", "조회 결과가 없습니다.")
21+
EMPTY_RESULT(400, "C012", "조회 결과가 없습니다."),
22+
DUPLICATED_STUDENT_NO(400, "C013", "이미 등록된 학번입니다."),
2223
;
2324

2425
private final int status;

0 commit comments

Comments
 (0)