Skip to content

Commit c6ef036

Browse files
committed
feat: 기수관리를 위한 엔티티, 서비스, 레포지토리 생성 및 대응하는 에러코드 추가 (DASOMBE-16)
1 parent d884a28 commit c6ef036

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public enum ErrorCode {
3737
FILE_ENCODE_FAIL(400, "C028", "파일 인코딩에 실패하였습니다."),
3838
RECRUITMENT_NOT_ACTIVE(400, "C029", "모집 기간이 아닙니다."),
3939
NOT_FOUND_PARTICIPANT(400, "C030", "참가자를 찾을 수 없습니다."),
40-
EXECUTIVE_NOT_FOUND(400, "C031", "임원진을 찾을 수 없습니다."),
41-
;
40+
GENERATION_NOT_FOUND(400, "C031", "저장된 기수를 찾을 수 없습니다."),
41+
INVALID_GENERATION_FORMAT(400, "C032", "유효하지 않은 기수 형식입니다. (예: '1기')");
4242

4343
private final int status;
4444
private final String code;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dmu.dasom.api.global.generation.entity;
2+
3+
import jakarta.persistence.*;
4+
import lombok.*;
5+
import org.hibernate.annotations.DynamicUpdate;
6+
7+
@AllArgsConstructor
8+
@Builder
9+
@DynamicUpdate
10+
@Entity
11+
@Getter
12+
@NoArgsConstructor
13+
public class Generation {
14+
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.IDENTITY)
17+
private Long id; // 기본키
18+
19+
@Column(nullable = false, unique = true, length = 10)
20+
private String generation; // 기수 (예: "1기", "2기")
21+
22+
23+
public void updateGeneration(String generation) {
24+
this.generation = generation;
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dmu.dasom.api.global.generation.repository;
2+
3+
import dmu.dasom.api.global.generation.entity.Generation;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
import java.util.Optional;
7+
8+
public interface GenerationRepository extends JpaRepository<Generation, Long> {
9+
10+
// 가장 최근 기수 1개 조회
11+
Optional<Generation> findFirstByOrderByIdDesc();
12+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dmu.dasom.api.global.generation.service;
2+
3+
import dmu.dasom.api.domain.common.exception.CustomException;
4+
import dmu.dasom.api.domain.common.exception.ErrorCode;
5+
import dmu.dasom.api.global.generation.entity.Generation;
6+
import dmu.dasom.api.global.generation.repository.GenerationRepository;
7+
import lombok.RequiredArgsConstructor;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
import java.util.regex.Pattern;
13+
14+
@Slf4j
15+
@Service
16+
@RequiredArgsConstructor
17+
@Transactional(readOnly = true)
18+
public class GenerationService {
19+
20+
private final GenerationRepository generationRepository;
21+
22+
// 기본값 해당 변수로 관리
23+
private static final String DEFAULT_GENERATION = "34기";
24+
25+
// 정규식: 숫자 + "기" 형식 (예: "1기", "12기")
26+
private static final Pattern GENERATION_PATTERN = Pattern.compile("^[0-9]+기$");
27+
28+
//현재 저장된 기수 조회
29+
public String getCurrentGeneration() {
30+
try {
31+
return generationRepository.findFirstByOrderByIdDesc()
32+
.map(Generation::getGeneration)
33+
.orElseGet(() -> {
34+
log.warn("저장된 기수 없음, 기본값 사용: {}", DEFAULT_GENERATION);
35+
return DEFAULT_GENERATION;
36+
});
37+
} catch (Exception e) {
38+
throw new CustomException(ErrorCode.INTERNAL_SERVER_ERROR);
39+
}
40+
}
41+
42+
//새로운 기수 저장 또는 기존 기수 수정 (유효성 검사 포함)
43+
@Transactional
44+
public void saveOrUpdateGeneration(String generationValue) {
45+
if (!GENERATION_PATTERN.matcher(generationValue).matches()) {
46+
throw new CustomException(ErrorCode.INVALID_GENERATION_FORMAT);
47+
}
48+
49+
try {
50+
Generation generation = generationRepository.findFirstByOrderByIdDesc()
51+
.orElseGet(() -> Generation.builder().build());
52+
53+
generation.updateGeneration(generationValue);
54+
generationRepository.save(generation);
55+
56+
} catch (Exception e) {
57+
throw new CustomException(ErrorCode.WRITE_FAIL);
58+
}
59+
}
60+
61+
}

0 commit comments

Comments
 (0)