feat: 수강신청 프로젝트 생성
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package com.study.course_registration.seeder;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.study.course_registration.entity.Lesson;
|
||||
import com.study.course_registration.entity.Professor;
|
||||
import com.study.course_registration.entity.Subject;
|
||||
import com.study.course_registration.entity.User;
|
||||
import com.study.course_registration.entity.UserLesson;
|
||||
import com.study.course_registration.enums.UserRole;
|
||||
import com.study.course_registration.repository.LessonRepository;
|
||||
import com.study.course_registration.repository.UserRepository;
|
||||
import com.study.course_registration.repository.SubjectRepository;
|
||||
import com.study.course_registration.repository.ProfessorRepository;
|
||||
import com.study.course_registration.repository.UserLessonRepository;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
|
||||
@Component
|
||||
@Profile ("local")
|
||||
public class DataSeeder implements CommandLineRunner {
|
||||
|
||||
private static final ZoneId KST = ZoneId.of("Asia/Seoul");
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final LessonRepository lessonRepository;
|
||||
private final SubjectRepository subjectRepository;
|
||||
private final ProfessorRepository professorRepository;
|
||||
private final UserLessonRepository userLessonRepository;
|
||||
|
||||
public DataSeeder(UserRepository userRepository, LessonRepository lessonRepository, SubjectRepository subjectRepository, ProfessorRepository professorRepository, UserLessonRepository userLessonRepository) {
|
||||
this.userRepository = userRepository;
|
||||
this.lessonRepository = lessonRepository;
|
||||
this.subjectRepository = subjectRepository;
|
||||
this.professorRepository = professorRepository;
|
||||
this.userLessonRepository = userLessonRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void run(String... args) throws Exception {
|
||||
if(userRepository.count() > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<Professor> professors = professorRepository.saveAll(List.of(
|
||||
new Professor("김영한"),
|
||||
new Professor("최태영"),
|
||||
new Professor("조재한")
|
||||
));
|
||||
|
||||
List<Subject> subjects = subjectRepository.saveAll(List.of(
|
||||
new Subject("자바 프로그래밍", "CS201", "자바 프로그래밍 기초"),
|
||||
new Subject("자료구조", "CS202", "자료구조 및 알고리즘"),
|
||||
new Subject("데이터베이스", "CS203", "데이터베이스 시스템")
|
||||
));
|
||||
|
||||
Instant base = Instant.now().truncatedTo(ChronoUnit.HOURS);
|
||||
List<Lesson> lessons = lessonRepository.saveAll(List.of(
|
||||
new Lesson("자바 프로그래밍", subjects.get(0), professors.get(0), kst(3, 2, 9), kst(3, 2, 11)),
|
||||
new Lesson("자료구조", subjects.get(1), professors.get(1), kst(3, 2, 13), kst(3, 2, 17)),
|
||||
new Lesson("데이터베이스", subjects.get(2), professors.get(2), kst(3, 3, 10), kst(3, 3, 14))
|
||||
));
|
||||
|
||||
List<User> users = userRepository.saveAll(List.of(
|
||||
new User("학생1", 1L, UserRole.STUDENT),
|
||||
new User("학생2", 2L, UserRole.STUDENT),
|
||||
new User("대학원생", 3L, UserRole.POSTGRADUATE)
|
||||
));
|
||||
|
||||
List<UserLesson> userLessons = userLessonRepository.saveAll(List.of(
|
||||
new UserLesson(users.get(0), lessons.get(0)),
|
||||
new UserLesson(users.get(0), lessons.get(1)),
|
||||
new UserLesson(users.get(1), lessons.get(1)),
|
||||
new UserLesson(users.get(2), lessons.get(2))
|
||||
));
|
||||
}
|
||||
|
||||
private Instant kst(int month, int day, int hour) {
|
||||
return ZonedDateTime.of(2026, month, day, hour, 0, 0, 0, KST).toInstant();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user