Files
course-registration/src/main/java/com/study/course_registration/seeder/DataSeeder.java
T

122 lines
7.4 KiB
Java

package com.study.course_registration.seeder;
import java.time.Clock;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.study.course_registration.entity.Lesson;
import com.study.course_registration.entity.LessonSchedule;
import com.study.course_registration.entity.Professor;
import com.study.course_registration.entity.Semester;
import com.study.course_registration.entity.Subject;
import com.study.course_registration.entity.User;
import com.study.course_registration.enums.UserRole;
import com.study.course_registration.repository.LessonRepository;
import com.study.course_registration.repository.LessonScheduleRepository;
import com.study.course_registration.repository.ProfessorRepository;
import com.study.course_registration.repository.SemesterRepository;
import com.study.course_registration.repository.SubjectRepository;
import com.study.course_registration.repository.UserRepository;
@Component
@ConditionalOnProperty(name = "app.seed.enabled", havingValue = "true")
public class DataSeeder implements CommandLineRunner {
public static final String SEMESTER_ID = "00000000-0000-0000-0000-000000000001";
public static final String STUDENT_GRADE_1_ID = "30000000-0000-0000-0000-000000000001";
public static final String STUDENT_GRADE_2_ID = "30000000-0000-0000-0000-000000000002";
public static final String POSTGRADUATE_ID = "30000000-0000-0000-0000-000000000003";
public static final String DATA_STRUCTURES_01_ID = "40000000-0000-0000-0000-000000000001";
public static final String DATA_STRUCTURES_02_ID = "40000000-0000-0000-0000-000000000002";
public static final String OVERLAP_OS_ID = "40000000-0000-0000-0000-000000000003";
public static final String CAPACITY_ONE_ID = "40000000-0000-0000-0000-000000000004";
public static final String MIN_GRADE_ID = "40000000-0000-0000-0000-000000000005";
public static final String POSTGRADUATE_ONLY_ID = "40000000-0000-0000-0000-000000000006";
public static final String BOUNDARY_LESSON_ID = "40000000-0000-0000-0000-000000000007";
private final Clock clock;
private final SemesterRepository semesterRepository;
private final ProfessorRepository professorRepository;
private final SubjectRepository subjectRepository;
private final LessonRepository lessonRepository;
private final LessonScheduleRepository lessonScheduleRepository;
private final UserRepository userRepository;
public DataSeeder(Clock clock,
SemesterRepository semesterRepository,
ProfessorRepository professorRepository,
SubjectRepository subjectRepository,
LessonRepository lessonRepository,
LessonScheduleRepository lessonScheduleRepository,
UserRepository userRepository) {
this.clock = clock;
this.semesterRepository = semesterRepository;
this.professorRepository = professorRepository;
this.subjectRepository = subjectRepository;
this.lessonRepository = lessonRepository;
this.lessonScheduleRepository = lessonScheduleRepository;
this.userRepository = userRepository;
}
@Override
@Transactional
public void run(String... args) {
if (semesterRepository.existsById(SEMESTER_ID)) {
return;
}
Instant now = clock.instant();
LocalDate today = LocalDate.now(clock);
Semester semester = semesterRepository.save(new Semester(
SEMESTER_ID, "2026-1", today.minusDays(30), today.plusDays(120),
now.minusSeconds(86_400), now.plusSeconds(7 * 86_400), 18));
List<Professor> professors = professorRepository.saveAll(List.of(
new Professor("10000000-0000-0000-0000-000000000001", "김영한"),
new Professor("10000000-0000-0000-0000-000000000002", "최태영"),
new Professor("10000000-0000-0000-0000-000000000003", "조재한")));
List<Subject> subjects = subjectRepository.saveAll(List.of(
new Subject("20000000-0000-0000-0000-000000000001", "자바 프로그래밍", "CS201", "자바 프로그래밍 기초", 3),
new Subject("20000000-0000-0000-0000-000000000002", "자료구조", "CS202", "자료구조 및 알고리즘", 3),
new Subject("20000000-0000-0000-0000-000000000003", "데이터베이스", "CS203", "데이터베이스 시스템", 3),
new Subject("20000000-0000-0000-0000-000000000004", "컴퓨터 네트워크", "CS204", "네트워크 기초", 3),
new Subject("20000000-0000-0000-0000-000000000005", "운영체제", "CS301", "운영체제 핵심", 3),
new Subject("20000000-0000-0000-0000-000000000006", "대학원 세미나", "CS401", "대학원 전용 세미나", 3)));
Lesson data01 = new Lesson(DATA_STRUCTURES_01_ID, "자료구조 01분반", subjects.get(1), professors.get(0), semester, 30, null, null);
Lesson data02 = new Lesson(DATA_STRUCTURES_02_ID, "자료구조 02분반", subjects.get(1), professors.get(1), semester, 30, null, null);
Lesson os = new Lesson(OVERLAP_OS_ID, "운영체제", subjects.get(4), professors.get(1), semester, 30, null, null);
Lesson capacityOne = new Lesson(CAPACITY_ONE_ID, "데이터베이스 소수정예", subjects.get(2), professors.get(2), semester, 1, null, null);
Lesson minGrade = new Lesson(MIN_GRADE_ID, "고급 네트워크", subjects.get(3), professors.get(2), semester, 30, 3L, null);
Lesson postgraduate = new Lesson(POSTGRADUATE_ONLY_ID, "대학원 세미나", subjects.get(5), professors.get(0), semester, 20, null, UserRole.POSTGRADUATE);
Lesson boundary = new Lesson(BOUNDARY_LESSON_ID, "자바 프로그래밍", subjects.get(0), professors.get(0), semester, 30, null, null);
lessonRepository.saveAll(List.of(data01, data02, os, capacityOne, minGrade, postgraduate, boundary));
lessonScheduleRepository.saveAll(List.of(
schedule("50000000-0000-0000-0000-000000000001", data01, DayOfWeek.MONDAY, 9, 11),
schedule("50000000-0000-0000-0000-000000000002", data02, DayOfWeek.TUESDAY, 9, 11),
schedule("50000000-0000-0000-0000-000000000003", os, DayOfWeek.MONDAY, 10, 12),
schedule("50000000-0000-0000-0000-000000000004", capacityOne, DayOfWeek.WEDNESDAY, 9, 11),
schedule("50000000-0000-0000-0000-000000000005", minGrade, DayOfWeek.THURSDAY, 9, 11),
schedule("50000000-0000-0000-0000-000000000006", postgraduate, DayOfWeek.FRIDAY, 9, 11),
schedule("50000000-0000-0000-0000-000000000007", boundary, DayOfWeek.MONDAY, 11, 13)));
userRepository.saveAll(List.of(
new User(STUDENT_GRADE_1_ID, "학생1", 1L, UserRole.STUDENT),
new User(STUDENT_GRADE_2_ID, "학생2", 2L, UserRole.STUDENT),
new User(POSTGRADUATE_ID, "대학원생", 3L, UserRole.POSTGRADUATE)));
}
private static LessonSchedule schedule(String id, Lesson lesson, DayOfWeek day, int startHour, int endHour) {
return new LessonSchedule(id, lesson, day, LocalTime.of(startHour, 0), LocalTime.of(endHour, 0));
}
}