-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#14 #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Feat/#14 #22
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| NEIS_API_KEY=8ba18e6d2b1f42f9be762009bc3dfa90 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| 1 | ||
| /var/lib/postgresql/data | ||
| 1750235928 | ||
| 1751931356 | ||
| 5432 | ||
| /var/run/postgresql | ||
| * | ||
| 5 0 | ||
| 13 0 | ||
| ready |
47 changes: 47 additions & 0 deletions
47
src/main/java/hello/cluebackend/domain/timetable/presentation/TimetableController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package hello.cluebackend.domain.timetable.presentation; | ||
|
|
||
| import hello.cluebackend.domain.timetable.presentation.dto.request.TimetableRequestDto; | ||
| import hello.cluebackend.domain.timetable.presentation.dto.response.TimetableResponseDto; | ||
| import hello.cluebackend.domain.timetable.service.TimetableService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/timetable") | ||
| @RequiredArgsConstructor | ||
| public class TimetableController { | ||
| private final TimetableService timetableService; | ||
|
|
||
| @GetMapping("/test") | ||
| public String test(){ | ||
| return "Test"; | ||
| } | ||
|
|
||
| @GetMapping("/today") | ||
| public Mono<ResponseEntity<List<TimetableResponseDto>>> getTodayTimetable( | ||
| @RequestParam(required = true) String grade, | ||
| @RequestParam(required = true) String classNumber | ||
| ) { | ||
| TimetableRequestDto request = new TimetableRequestDto(grade, classNumber); | ||
| return timetableService.getTodayTimetable(request) | ||
| .map(ResponseEntity::ok) | ||
| .defaultIfEmpty(ResponseEntity.noContent().build()) | ||
| .onErrorResume(e -> Mono.just(ResponseEntity.internalServerError().build())); | ||
| } | ||
|
|
||
| @GetMapping("/weekly") | ||
| public Mono<ResponseEntity<List<TimetableResponseDto>>> getWeeklyTimetable( | ||
| @RequestParam(required = true) String grade, | ||
| @RequestParam(required = true) String classNumber | ||
| ) { | ||
| TimetableRequestDto request = new TimetableRequestDto(grade, classNumber); | ||
| return timetableService.getWeeklyTimetable(request) | ||
| .map(ResponseEntity::ok) | ||
| .defaultIfEmpty(ResponseEntity.noContent().build()) | ||
| .onErrorResume(e -> Mono.just(ResponseEntity.internalServerError().build())); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...java/hello/cluebackend/domain/timetable/presentation/dto/request/TimetableRequestDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package hello.cluebackend.domain.timetable.presentation.dto.request; | ||
|
|
||
| import hello.cluebackend.domain.timetable.service.TimetableService; | ||
| import jakarta.validation.constraints.Max; | ||
| import jakarta.validation.constraints.Min; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import lombok.Data; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import org.hibernate.annotations.processing.Pattern; | ||
|
|
||
| @Getter | ||
| public class TimetableRequestDto { | ||
| @NotBlank(message = "학년은 필수입니다.") | ||
| @Min(value = 1) | ||
| @Max(value = 3) | ||
| private String grade; | ||
|
|
||
| @NotBlank(message = "반은 필수입니다.") | ||
| @Min(value = 1) | ||
| @Max(value = 4) | ||
| private String classNumber; | ||
|
|
||
| public TimetableRequestDto(String grade, String classNumber) { | ||
| this.grade = grade; | ||
| this.classNumber = classNumber; | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...va/hello/cluebackend/domain/timetable/presentation/dto/response/TimetableResponseDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package hello.cluebackend.domain.timetable.presentation.dto.response; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| import java.time.DayOfWeek; | ||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.time.format.TextStyle; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
|
|
||
| @Getter @Setter | ||
| @NoArgsConstructor | ||
| public class TimetableResponseDto { | ||
|
|
||
| private String period; // 교시 | ||
| private String subject; // 과목 이름 | ||
| private String date; // 날짜 | ||
| private String dayOfWeek; // 요일 | ||
|
|
||
| public static TimetableResponseDto fromMap(Map<String, Object> map) { | ||
| TimetableResponseDto dto = new TimetableResponseDto(); | ||
|
|
||
| dto.period = (String) map.getOrDefault("PERIO", ""); | ||
| dto.subject = (String) map.getOrDefault("ITRT_CNTNT", ""); | ||
| dto.date = (String) map.getOrDefault("ALL_TI_YMD", ""); | ||
|
|
||
| if (!dto.date.isEmpty() && dto.date.length() == 8) { | ||
| try { | ||
| LocalDate localDate = LocalDate.parse(dto.date, DateTimeFormatter.ofPattern("yyyyMMdd")); | ||
| DayOfWeek dayOfWeek = localDate.getDayOfWeek(); | ||
| dto.dayOfWeek = dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.KOREAN); | ||
| } catch (Exception e) { | ||
| dto.dayOfWeek = ""; | ||
| } | ||
| }else{ | ||
| dto.dayOfWeek="error"; | ||
| } | ||
| return dto; | ||
| } | ||
| } |
176 changes: 176 additions & 0 deletions
176
src/main/java/hello/cluebackend/domain/timetable/service/TimetableService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| package hello.cluebackend.domain.timetable.service; | ||
|
|
||
| import hello.cluebackend.domain.timetable.presentation.dto.request.TimetableRequestDto; | ||
| import hello.cluebackend.domain.timetable.presentation.dto.response.TimetableResponseDto; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.core.ParameterizedTypeReference; | ||
| import org.springframework.http.HttpStatusCode; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
| import org.springframework.web.reactive.function.client.WebClientResponseException; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.util.retry.Retry; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.LocalDate; | ||
| import java.time.ZoneId; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class TimetableService { | ||
|
|
||
| // Constants | ||
| private static final String TIMETABLE_PATH = "/hisTimetable"; | ||
| private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); | ||
| private static final DateTimeFormatter YEAR_FORMATTER = DateTimeFormatter.ofPattern("yyyy"); | ||
| private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("MM"); | ||
| private static final ZoneId KOREA_TIMEZONE = ZoneId.of("Asia/Seoul"); | ||
| private static final int FIRST_SEMESTER_END_MONTH = 8; | ||
| private static final String FIRST_SEMESTER = "1"; | ||
| private static final String SECOND_SEMESTER = "2"; | ||
| private static final int API_TIMEOUT_SECONDS = 10; | ||
| private static final int RETRY_ATTEMPTS = 2; | ||
| private static final Duration RETRY_DELAY = Duration.ofSeconds(1); | ||
|
|
||
| @Value("${spring.neis.api.host}") | ||
| private String neisApiHost; | ||
|
|
||
| @Value("${spring.neis.api.key}") | ||
| private String neisApiKey; | ||
|
|
||
| @Value("${spring.neis.api.atpt-code:C10}") | ||
| private String atptCode; | ||
|
|
||
| @Value("${spring.neis.api.school-code:7150658}") | ||
| private String schoolCode; | ||
|
|
||
| private final WebClient webClient; | ||
|
|
||
| public Mono<List<TimetableResponseDto>> getTodayTimetable(TimetableRequestDto request) { | ||
| LocalDate today = LocalDate.now(KOREA_TIMEZONE); | ||
| String todayStr = today.format(DATE_FORMATTER); | ||
|
|
||
| log.info("오늘 시간표 조회 요청 - Grade: {}, Class: {}, Date: {}", | ||
| request.getGrade(), request.getClassNumber(), todayStr); | ||
|
|
||
| return fetchTimetable(request, todayStr, todayStr); | ||
| } | ||
|
|
||
| public Mono<List<TimetableResponseDto>> getWeeklyTimetable(TimetableRequestDto request) { | ||
| LocalDate now = LocalDate.now(KOREA_TIMEZONE); | ||
| String from = now.with(java.time.DayOfWeek.MONDAY).format(DATE_FORMATTER); | ||
| String to = now.with(java.time.DayOfWeek.FRIDAY).format(DATE_FORMATTER); | ||
|
|
||
| log.info("주간 시간표 조회 요청 - Grade: {}, Class: {}, Period: {} ~ {}", | ||
| request.getGrade(), request.getClassNumber(), from, to); | ||
|
|
||
| return fetchTimetable(request, from, to); | ||
| } | ||
|
|
||
| private Mono<List<TimetableResponseDto>> fetchTimetable(TimetableRequestDto request, String from, String to) { | ||
| LocalDate now = LocalDate.now(KOREA_TIMEZONE); | ||
| String year = now.format(YEAR_FORMATTER); | ||
| String semester = determineSemester(now); | ||
|
|
||
| log.debug("NEIS API 호출 파라미터 - Year: {}, Semester: {}, From: {}, To: {}", | ||
| year, semester, from, to); | ||
|
|
||
| return webClient.get() | ||
| .uri("https://" + neisApiHost + TIMETABLE_PATH + | ||
| "?KEY=" + neisApiKey + | ||
| "&Type=json" + | ||
| "&pIndex=1" + | ||
| "&pSize=100" + | ||
| "&ATPT_OFCDC_SC_CODE=" + atptCode + | ||
| "&SD_SCHUL_CODE=" + schoolCode + | ||
| "&AY=" + year + | ||
| "&SEM=" + semester + | ||
| "&GRADE=" + request.getGrade() + | ||
| "&CLASS_NM=" + request.getClassNumber() + | ||
| "&TI_FROM_YMD=" + from + | ||
| "&TI_TO_YMD=" + to) | ||
| // .uri(uriBuilder -> uriBuilder | ||
| // .scheme("https") | ||
| // .host(neisApiHost) | ||
| // .path(TIMETABLE_PATH) | ||
| // .queryParam("KEY", neisApiKey) | ||
| // .queryParam("Type", "json") | ||
| // .queryParam("pIndex", "1") | ||
| // .queryParam("pSize", "100") | ||
| // .queryParam("ATPT_OFCDC_SC_CODE", atptCode) | ||
| // .queryParam("SD_SCHUL_CODE", schoolCode) | ||
| // .queryParam("AY", year) | ||
| // .queryParam("SEM", semester) | ||
| // .queryParam("GRADE", request.getGrade()) | ||
| // .queryParam("CLASS_NM", request.getClassNumber()) | ||
| // .queryParam("TI_FROM_YMD", from) | ||
| // .queryParam("TI_TO_YMD", to) | ||
| // .build()) | ||
| .retrieve() | ||
| .onStatus(HttpStatusCode::is4xxClientError, | ||
| response -> response.bodyToMono(String.class) | ||
| .flatMap(body -> { | ||
| log.error("NEIS API 클라이언트 오류 - Status: {}, Body: {}", | ||
| response.statusCode(), body); | ||
| return Mono.error(new IllegalArgumentException("잘못된 요청: " + body)); | ||
| })) | ||
| .onStatus(HttpStatusCode::is5xxServerError, | ||
| response -> { | ||
| log.error("NEIS API 서버 오류 - Status: {}", response.statusCode()); | ||
| return Mono.error(new RuntimeException("NEIS 서버 오류 발생")); | ||
| }) | ||
| .bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {}) | ||
| .timeout(Duration.ofSeconds(API_TIMEOUT_SECONDS)) | ||
| .retryWhen(Retry.backoff(RETRY_ATTEMPTS, RETRY_DELAY) | ||
| .filter(throwable -> !(throwable instanceof WebClientResponseException.BadRequest)) | ||
| .onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> { | ||
| log.error("NEIS API 재시도 횟수 초과"); | ||
| return new RuntimeException("NEIS API 호출 실패 - 호출 시간 초과"); | ||
| })) | ||
| .map(this::parseTimetableResponse) | ||
| .doOnSuccess(result -> log.info("시간표 조회 성공 - 결과 개수: {}", result.size())) | ||
| .doOnError(error -> log.error("시간표 조회 실패", error)); | ||
| } | ||
|
|
||
| private String determineSemester(LocalDate date) { | ||
| int month = Integer.parseInt(date.format(MONTH_FORMATTER)); | ||
| return (month <= FIRST_SEMESTER_END_MONTH) ? FIRST_SEMESTER : SECOND_SEMESTER; | ||
| } | ||
|
|
||
| private List<TimetableResponseDto> parseTimetableResponse(Map<String, Object> response) { | ||
| try { | ||
| return Optional.ofNullable(response.get("hisTimetable")) | ||
| .filter(List.class::isInstance) | ||
| .map(obj -> (List<?>) obj) | ||
| .filter(list -> list.size() >= 2) | ||
| .map(list -> list.get(1)) | ||
| .filter(Map.class::isInstance) | ||
| .map(obj -> (Map<?, ?>) obj) | ||
| .map(map -> map.get("row")) | ||
| .filter(List.class::isInstance) | ||
| .map(obj -> (List<?>) obj) | ||
| .map(this::convertToTimetableResponseList) | ||
| .orElse(List.of()); | ||
| } catch (Exception e) { | ||
| log.error("시간표 파싱 오류", e); | ||
| return List.of(); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private List<TimetableResponseDto> convertToTimetableResponseList(List<?> rowList) { | ||
| return rowList.stream() | ||
| .filter(Map.class::isInstance) | ||
| .map(item -> (Map<String, Object>) item) | ||
| .map(TimetableResponseDto::fromMap) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추후 리팩토링 과정에서 하드 코딩된 url 전송을 queryParam으로 고칠 예정,
redis를 이용해서 데이터 캐싱하여, 외부 API 접근 횟수 줄일 예정