Skip to content

Commit

Permalink
fix: timetableLecture 오류 수정 (#1096)
Browse files Browse the repository at this point in the history
* fix: 인덱스 에러 수정

* fix: 인덱스 에러 수정

* fix: 정규 강의 분할 반환

* fix: 롤백

* test: 테스트 수정

* fix: 로직 수정

* fix: 롤백

* fix: deleteTimetableLectures 검증 오류 수정

* fix: 강의 장소가 없는 강의 정보가 두개 이상 들어왔을 때 오류 해결

* fix: 빈 문자열 에러 수정

* chore: 스웨거 설명 수정

* chore: todo 작성

* fix: class_place "null" 저장 수정

* chore: 미사용 import 삭제
  • Loading branch information
Soundbar91 authored Nov 28, 2024
1 parent aaa47ae commit a3c3ae4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ ResponseEntity<Void> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -81,12 +80,13 @@ public record ClassInfo(
String classPlace
) {
public static List<ClassInfo> 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*");

Expand All @@ -98,24 +98,34 @@ public static List<ClassInfo> 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<Integer> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -70,7 +72,7 @@ private TimetableLectureResponse getTimetableLectureResponse(Integer userId, Tim
public void deleteTimetableLectures(List<Integer> 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()));
}

Expand Down

0 comments on commit a3c3ae4

Please sign in to comment.