Skip to content

Commit

Permalink
feat: 학과 수정 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
김원경 committed Dec 27, 2024
1 parent 6e44aa9 commit dea52f0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import in.koreatech.koin.domain.graduation.repository.StandardGraduationRequirementsRepository;
import in.koreatech.koin.domain.graduation.repository.StudentCourseCalculationRepository;
import in.koreatech.koin.domain.student.exception.DepartmentNotFoundException;
import in.koreatech.koin.domain.student.model.Department;
import in.koreatech.koin.domain.student.model.Student;
import in.koreatech.koin.domain.student.repository.StudentRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -54,4 +55,32 @@ public void createStudentCourseCalculation(Integer userId) {
.build();
detectGraduationCalculationRepository.save(detectGraduationCalculation);
}

@Transactional
public void resetStudentCourseCalculation(Student student, Department newDepartment) {
// 기존 학생 졸업요건 계산 정보 삭제
studentCourseCalculationRepository.findByUserId(student.getUser().getId())
.ifPresent(studentCourseCalculation -> {
studentCourseCalculationRepository.deleteAllByUserId(student.getUser().getId());
});
// 학번에 맞는 이수요건 정보 조회
List<StandardGraduationRequirements> requirementsList =
standardGraduationRequirementsRepository.findAllByDepartmentAndYear(
newDepartment, student.getStudentNumber().substring(0, 4));
// 학생 졸업요건 계산 정보 초기화
requirementsList.forEach(requirement ->
studentCourseCalculationRepository.save(
StudentCourseCalculation.builder()
.completedGrades(0)
.user(student.getUser())
.standardGraduationRequirements(requirement)
.build()
)
);
// DetectGraduationCalculation 정보 업데이트
detectGraduationCalculationRepository.findByUserId(student.getUser().getId())
.ifPresent(detectGraduationCalculation -> {
detectGraduationCalculation.updatedIsChanged(true);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import in.koreatech.koin.domain.graduation.repository.DetectGraduationCalculationRepository;
import in.koreatech.koin.domain.graduation.repository.StandardGraduationRequirementsRepository;
import in.koreatech.koin.domain.graduation.repository.StudentCourseCalculationRepository;
import in.koreatech.koin.domain.graduation.service.GraduationService;
import in.koreatech.koin.domain.student.model.Department;
import in.koreatech.koin.domain.student.model.Student;
import in.koreatech.koin.domain.student.model.StudentEmailRequestEvent;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class StudentService {
private final StudentCourseCalculationRepository studentCourseCalculationRepository;
private final StandardGraduationRequirementsRepository standardGraduationRequirementsRepository;
private final DetectGraduationCalculationRepository detectGraduationCalculationRepository;
private final GraduationService graduationService;
private final PasswordEncoder passwordEncoder;
private final ApplicationEventPublisher eventPublisher;
private final Clock clock;
Expand Down Expand Up @@ -102,34 +104,11 @@ public StudentUpdateResponse updateStudent(Integer userId, StudentUpdateRequest
Student student = studentRepository.getById(userId);
User user = student.getUser();

// 학부(학과) 변경 시 학생의 졸업 요건 계산 정보 초기화
Department oldDepartment = student.getDepartment();
Department newDepartment = departmentRepository.getByName(request.major());
// 학부(학과) 변경 시 학생의 졸업 요건 계산 정보 초기화
if (isChangedDepartment(oldDepartment, newDepartment) && student.getStudentNumber() != null) {
// 기존 학생 졸업요건 계산 정보 삭제
studentCourseCalculationRepository.findByUserId(userId)
.ifPresent(studentCourseCalculation -> {
studentCourseCalculationRepository.deleteAllByUserId(userId);
});
// 학번에 맞는 이수요건 정보 조회
List<StandardGraduationRequirements> requirementsList =
standardGraduationRequirementsRepository.findAllByDepartmentAndYear(
newDepartment, student.getStudentNumber().substring(0, 4));
// 학생 졸업요건 계산 정보 초기화
requirementsList.forEach(requirement ->
studentCourseCalculationRepository.save(
StudentCourseCalculation.builder()
.completedGrades(0)
.user(student.getUser())
.standardGraduationRequirements(requirement)
.build()
)
);
// DetectGraduationCalculation 정보 업데이트
detectGraduationCalculationRepository.findByUserId(userId)
.ifPresent(detectGraduationCalculation -> {
detectGraduationCalculation.updatedIsChanged(true);
});
graduationService.resetStudentCourseCalculation(student, newDepartment);
}
user.update(request.nickname(), request.name(), request.phoneNumber(), request.gender());
user.updateStudentPassword(passwordEncoder, request.password());
Expand Down

0 comments on commit dea52f0

Please sign in to comment.