Skip to content

Commit

Permalink
fix: LectureSchedule 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
wonslee committed Feb 8, 2024
1 parent 8261506 commit a6d30a6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static LectureWithScheduleResponse of(
) {
final String professorName = ResponseFieldManipulationUtils.resolveLiteralNull(lectureSchedule.getLecture().getProfessor());
return LectureWithScheduleResponse.builder()
.id(lectureSchedule.getLecture().getId())
.id(lectureSchedule.getId())
.name(lectureSchedule.getLecture().getName())
.professorName(professorName)
.type(lectureSchedule.getLecture().getType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Slice<LectureSchedule> findCurrentSemesterLectureSchedules(
final Integer grade
);

List<LectureSchedule> findAllLectureSchedulesByLectureSemesterContains(String semester);

Optional<Lecture> findByExtraUniqueKey(String lectureName, String ProfessorName, String majorType);

List<String> findAllMajorType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,25 @@ public Slice<LectureSchedule> findCurrentSemesterLectureSchedules(
) {
JPAQuery<LectureSchedule> query = queryFactory.selectFrom(lectureSchedule)
.join(lectureSchedule.lecture).fetchJoin()
.where(gtLectureScheduleLectureCursorId(cursorId))
.where(gtLectureScheduleCursorId(cursorId))
.where(containsLectureScheduleLectureKeyword(keyword))
.where(eqLectureScheduleLectureMajorType(majorType))
.where(eqLectureScheduleLectureGrade(grade))
.where(lectureSchedule.lecture.semester.endsWith(currentSemester))
.orderBy(lectureSchedule.lecture.id.asc())
.orderBy(lectureSchedule.id.asc())
.limit(SlicePaginationUtils.increaseSliceLimit(limit));

return SlicePaginationUtils.buildSlice(query.fetch(), limit);
}

@Override
public List<LectureSchedule> findAllLectureSchedulesByLectureSemesterContains(String semester) {
return queryFactory.selectFrom(lectureSchedule)
.join(lectureSchedule.lecture).fetchJoin()
.where(lectureSchedule.lecture.semester.contains(semester))
.fetch();
}


@Override
public Optional<Lecture> findByExtraUniqueKey(String lectureName, String professorName, String majorType) {
Expand Down Expand Up @@ -208,11 +216,11 @@ public List<String> findAllMajorType() {
.fetch();
}

private BooleanExpression gtLectureScheduleLectureCursorId(Long cursorId) {
private BooleanExpression gtLectureScheduleCursorId(Long cursorId) {
if (Objects.isNull(cursorId)) {
return null;
}
return lectureSchedule.lecture.id.gt(cursorId);
return lectureSchedule.id.gt(cursorId);
}

private BooleanExpression containsLectureScheduleLectureKeyword(String keyword) {
Expand Down
115 changes: 76 additions & 39 deletions src/main/java/usw/suwiki/domain/lecture/service/LectureService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import usw.suwiki.domain.lecture.controller.dto.LectureAndCountResponseForm;
import usw.suwiki.domain.lecture.controller.dto.LectureDetailResponseDto;
Expand Down Expand Up @@ -105,48 +106,13 @@ public Lecture findLectureById(Long id) {


@Transactional
public void bulkSaveJsonLectures(String filePath) {
public void bulkApplyLectureJsonFile(String filePath) {
JSONArray jsonArray = resolveJsonArrayFromJsonFile(filePath);
bulkSaveLectures(jsonArray);
}

private void bulkSaveLectures(JSONArray jsonArray) {
for (Object o : jsonArray) {
JSONObject jsonObject = (JSONObject) o;
JSONLectureVO jsonLectureVO = JSONLectureVO.from(jsonObject);

Optional<Lecture> optionalLecture = lectureRepository.findByExtraUniqueKey(
jsonLectureVO.getLectureName(),
jsonLectureVO.getProfessor(),
jsonLectureVO.getMajorType()
);

if (optionalLecture.isPresent()) {
Lecture lecture = optionalLecture.get();
lecture.addSemester(jsonLectureVO.getSelectedSemester());

boolean anyMatch = lecture.getScheduleList().stream()
.anyMatch(jsonLectureVO::isLectureAndPlaceScheduleEqual);
if (!anyMatch) { // 없던 스케줄일 경우 : 추가
LectureSchedule.builder()
.lecture(lecture)
.placeSchedule(jsonLectureVO.getPlaceSchedule())
.build();
lectureRepository.save(lecture);
}
} else {
Lecture newLecture = jsonLectureVO.toEntity();
LectureSchedule.builder()
.lecture(newLecture)
.placeSchedule(jsonLectureVO.getPlaceSchedule())
.build();

lectureRepository.save(newLecture);
}
}

List<JSONLectureVO> jsonLectureVOList = convertJSONArrayToVOList(jsonArray);
bulkApplyJsonLectureList(jsonLectureVOList);
}


private static JSONArray resolveJsonArrayFromJsonFile(String filePath) {
try {
Reader reader = new FileReader(filePath);
Expand All @@ -160,6 +126,77 @@ private static JSONArray resolveJsonArrayFromJsonFile(String filePath) {
}
}

private static List<JSONLectureVO> convertJSONArrayToVOList(JSONArray jsonArray) {
List<JSONLectureVO> jsonLectureVOList = new ArrayList<>();
for (Object rawObject : jsonArray) {
JSONLectureVO jsonLectureVO = JSONLectureVO.from((JSONObject) rawObject);
jsonLectureVOList.add(jsonLectureVO);
}
return jsonLectureVOList;
}


@Transactional(propagation = Propagation.MANDATORY)
public void bulkApplyJsonLectureList(List<JSONLectureVO> jsonLectureVOList) {
List<LectureSchedule> currentSemeterLectureScheduleList = lectureRepository
.findAllLectureSchedulesByLectureSemesterContains("2024-1");

List<LectureSchedule> deletedLectureScheduleList = resolveDeletedLectureScheduleList(
jsonLectureVOList,
currentSemeterLectureScheduleList
);

jsonLectureVOList.forEach(vo -> applyJsonLecture(vo, deletedLectureScheduleList));
}

private void applyJsonLecture(
JSONLectureVO jsonLectureVO,
List<LectureSchedule> deletedLectureScheduleList
) {
Optional<Lecture> optionalLecture = lectureRepository.findByExtraUniqueKey(
jsonLectureVO.getLectureName(),
jsonLectureVO.getProfessor(),
jsonLectureVO.getMajorType()
);

if (optionalLecture.isPresent()) {
Lecture lecture = optionalLecture.get();
lecture.addSemester(jsonLectureVO.getSelectedSemester());

boolean isThereNewSchedule = lecture.getScheduleList().stream()
.noneMatch(jsonLectureVO::isLectureAndPlaceScheduleEqual);
if (isThereNewSchedule) {
LectureSchedule schedule = LectureSchedule.builder()
.lecture(lecture)
.placeSchedule(jsonLectureVO.getPlaceSchedule())
.build();
lectureScheduleRepository.save(schedule);
}

deletedLectureScheduleList.stream()
.filter(it -> it.getLecture().getId().equals(lecture.getId()))
.forEach(lecture::removeSchedule);
} else {
Lecture newLecture = jsonLectureVO.toEntity();
LectureSchedule.builder()
.lecture(newLecture)
.placeSchedule(jsonLectureVO.getPlaceSchedule())
.build();

lectureRepository.save(newLecture);
}
}

private static List<LectureSchedule> resolveDeletedLectureScheduleList(
List<JSONLectureVO> jsonLectureVOList,
List<LectureSchedule> currentSemeterLectureScheduleList
) {
// 기존의 스케줄이 삭제된 케이스 필터링 : O(N^2) 비교
return currentSemeterLectureScheduleList.stream()
.filter(it -> jsonLectureVOList.stream().noneMatch(vo -> vo.isLectureAndPlaceScheduleEqual(it)))
.toList();
}

private LectureAndCountResponseForm readLectureByKeywordAndOption(String keyword, LectureFindOption option) {
LecturesAndCountDao lectureInfo = lectureCRUDService.loadLectureByKeywordAndOption(keyword, option);
return createLectureResponseForm(lectureInfo);
Expand Down
29 changes: 1 addition & 28 deletions src/main/java/usw/suwiki/global/util/loadjson/JSONLectureVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,10 @@ public Lecture toEntity() {
}

public boolean isLectureAndPlaceScheduleEqual(LectureSchedule lectureSchedule) {
boolean b = lectureSchedule.getPlaceSchedule().contains(placeSchedule)
return lectureSchedule.getPlaceSchedule().contains(placeSchedule)
&& lectureSchedule.getLecture().getName().equals(lectureName)
&& lectureSchedule.getLecture().getProfessor().equals(professor)
&& lectureSchedule.getLecture().getMajorType().equals(majorType);

if (b) {
System.out.println("lectureSchedule.getId() = " + lectureSchedule.getId());
System.out.println("lectureSchedule.getPlaceSchedule() = " + lectureSchedule.getPlaceSchedule());
System.out.println("placeSchedule = " + placeSchedule);
System.out.println(
"lectureSchedule.getPlaceSchedule().equals(placeSchedule) = " + lectureSchedule.getPlaceSchedule()
.equals(placeSchedule));
System.out.println("lectureSchedule.getLecture().getName() = " + lectureSchedule.getLecture().getName());
System.out.println("lectureName = " + lectureName);
System.out.println(
"lectureSchedule.getLecture().getName().equals(lectureName) = " + lectureSchedule.getLecture()
.getName().equals(lectureName));
System.out.println(
"lectureSchedule.getLecture().getProfessor() = " + lectureSchedule.getLecture().getProfessor());
System.out.println("professor = " + professor);
System.out.println(
"lectureSchedule.getLecture().getProfessor().equals(professor) = " + lectureSchedule.getLecture()
.getProfessor().equals(professor));
System.out.println(
"lectureSchedule.getLecture().getMajorType() = " + lectureSchedule.getLecture().getMajorType());
System.out.println("majorType = " + majorType);
System.out.println(
"lectureSchedule.getLecture().getMajorType().equals(majorType) = " + lectureSchedule.getLecture()
.getMajorType().equals(majorType));
}
return b;
}

}

0 comments on commit a6d30a6

Please sign in to comment.