From a3c3ae445d9f18396fc3f90dcfb32b063bf9e6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=EA=B4=80=EA=B7=9C?= Date: Thu, 28 Nov 2024 16:18:28 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20timetableLecture=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20(#1096)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 인덱스 에러 수정 * fix: 인덱스 에러 수정 * fix: 정규 강의 분할 반환 * fix: 롤백 * test: 테스트 수정 * fix: 로직 수정 * fix: 롤백 * fix: deleteTimetableLectures 검증 오류 수정 * fix: 강의 장소가 없는 강의 정보가 두개 이상 들어왔을 때 오류 해결 * fix: 빈 문자열 에러 수정 * chore: 스웨거 설명 수정 * chore: todo 작성 * fix: class_place "null" 저장 수정 * chore: 미사용 import 삭제 --- .../controller/TimetableApiV2.java | 4 ++-- .../TimetableLectureCreateRequest.java | 14 +++++++---- .../response/TimetableLectureResponse.java | 24 +++++++++++++------ .../service/TimetableLectureService.java | 4 +++- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/java/in/koreatech/koin/domain/timetableV2/controller/TimetableApiV2.java b/src/main/java/in/koreatech/koin/domain/timetableV2/controller/TimetableApiV2.java index 99e63a9af..4f52486ba 100644 --- a/src/main/java/in/koreatech/koin/domain/timetableV2/controller/TimetableApiV2.java +++ b/src/main/java/in/koreatech/koin/domain/timetableV2/controller/TimetableApiV2.java @@ -123,8 +123,8 @@ ResponseEntity deleteAllTimetablesFrame( ) @Operation(summary = "시간표에 강의 정보 추가", description = """ - lecture_id가 있는 경우 class_title, class_time, professor은 null, grades는 '0'으로 입력해야합니다.\n - lecture_id가 없는 경우 class_title, class_time, professor, grades을 선택적으로 입력합니다. + lecture_id가 있는 경우 class_infos, professor은 null, grades는 '0'으로 입력해야합니다.\n + lecture_id가 없는 경우 class_infos, professor, grades을 선택적으로 입력합니다. """) @SecurityRequirement(name = "Jwt Authentication") @PostMapping("/v2/timetables/lecture") diff --git a/src/main/java/in/koreatech/koin/domain/timetableV2/dto/request/TimetableLectureCreateRequest.java b/src/main/java/in/koreatech/koin/domain/timetableV2/dto/request/TimetableLectureCreateRequest.java index 194d29379..a7c7f7067 100644 --- a/src/main/java/in/koreatech/koin/domain/timetableV2/dto/request/TimetableLectureCreateRequest.java +++ b/src/main/java/in/koreatech/koin/domain/timetableV2/dto/request/TimetableLectureCreateRequest.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; +import java.util.Objects; import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; @@ -113,9 +113,15 @@ private String getClassTimeToString() { private String getClassPlaceToString() { if (classInfos != null) { - return classInfos.stream() - .map(c -> c.classPlace) - .collect(Collectors.joining(", ")); + StringBuilder classPlaceSegment = new StringBuilder(); + for (int i = 0; i < classInfos.size(); i++) { + if (i > 0) classPlaceSegment.append(", "); + if (Objects.equals(classInfos.get(i).classPlace,null)) { + classPlaceSegment.append(""); + } + else classPlaceSegment.append(classInfos.get(i).classPlace); + } + return classPlaceSegment.toString(); } return null; } diff --git a/src/main/java/in/koreatech/koin/domain/timetableV2/dto/response/TimetableLectureResponse.java b/src/main/java/in/koreatech/koin/domain/timetableV2/dto/response/TimetableLectureResponse.java index bfc9c46a4..e55bbe476 100644 --- a/src/main/java/in/koreatech/koin/domain/timetableV2/dto/response/TimetableLectureResponse.java +++ b/src/main/java/in/koreatech/koin/domain/timetableV2/dto/response/TimetableLectureResponse.java @@ -15,7 +15,6 @@ import in.koreatech.koin.domain.timetableV2.model.TimetableFrame; import in.koreatech.koin.domain.timetableV2.model.TimetableLecture; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.Size; @JsonNaming(value = SnakeCaseStrategy.class) public record TimetableLectureResponse( @@ -81,12 +80,13 @@ public record ClassInfo( String classPlace ) { public static List of(String classTime, String classPlace) { - // 정규 강의인 경우 강의 장소가 없기 때문에 바로 반환 + // 강의 장소가 없는 경우 강의 시간과 매핑을 못하기 때문에 바로 반환 if (classPlace == null) { return List.of(new ClassInfo(parseClassTimes(classTime), null)); } // 구분자를 바탕으로 강의 시간과 강의 장소 분리 + // TODO. StringBuilder으로 리펙토링 String[] classPlaceSegment = classPlace.split(",\\s*"); String[] classTimeSegment = classTime.substring(1, classTime.length() - 1).trim().split(",\\s*"); @@ -98,24 +98,34 @@ public static List of(String classTime, String classPlace) { int parseInt = Integer.parseInt(segment); if (parseInt == -1) { if (!currentTimes.isEmpty()) { - classInfos.add(new ClassInfo(new ArrayList<>(currentTimes), classPlaceSegment[index++])); + if (classPlaceSegment.length < index + 1) { + classInfos.add(new ClassInfo(new ArrayList<>(currentTimes), "")); + } else { + classInfos.add( + new ClassInfo(new ArrayList<>(currentTimes), classPlaceSegment[index++])); + } currentTimes.clear(); } - } - else { + } else { currentTimes.add(parseInt); } } if (!currentTimes.isEmpty()) { - classInfos.add(new ClassInfo(new ArrayList<>(currentTimes), classPlaceSegment[index])); + if (classPlaceSegment.length < index + 1) { + classInfos.add(new ClassInfo(new ArrayList<>(currentTimes), "")); + } else { + classInfos.add( + new ClassInfo(new ArrayList<>(currentTimes), classPlaceSegment[index++])); + } } return classInfos; } private static List parseClassTimes(String classTime) { - if (classTime == null) return null; + if (classTime == null) + return null; String classTimeWithoutBrackets = classTime.substring(INITIAL_BRACE_INDEX, classTime.length() - 1); return Arrays.stream(classTimeWithoutBrackets.split(SEPARATOR)) diff --git a/src/main/java/in/koreatech/koin/domain/timetableV2/service/TimetableLectureService.java b/src/main/java/in/koreatech/koin/domain/timetableV2/service/TimetableLectureService.java index e75f02ede..0214237e8 100644 --- a/src/main/java/in/koreatech/koin/domain/timetableV2/service/TimetableLectureService.java +++ b/src/main/java/in/koreatech/koin/domain/timetableV2/service/TimetableLectureService.java @@ -5,6 +5,7 @@ import static in.koreatech.koin.domain.timetableV2.validation.TimetableFrameValidate.validateUserAuthorization; import java.util.List; +import java.util.Objects; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,6 +19,7 @@ import in.koreatech.koin.domain.timetableV2.repository.TimetableLectureRepositoryV2; import in.koreatech.koin.domain.timetableV2.factory.TimetableLectureCreator; import in.koreatech.koin.domain.timetableV2.factory.TimetableLectureUpdater; +import in.koreatech.koin.global.auth.exception.AuthorizationException; import lombok.RequiredArgsConstructor; @Service @@ -70,7 +72,7 @@ private TimetableLectureResponse getTimetableLectureResponse(Integer userId, Tim public void deleteTimetableLectures(List request, Integer userId) { request.stream() .map(timetableLectureRepositoryV2::getById) - .peek(lecture -> validateUserAuthorization(lecture.getTimetableFrame().getId(), userId)) + .peek(lecture -> validateUserAuthorization(lecture.getTimetableFrame().getUser().getId(), userId)) .forEach(lecture -> timetableLectureRepositoryV2.deleteById(lecture.getId())); }