Skip to content

Commit 7b026c2

Browse files
committed
Adding Application execution to call implemented functionalities
1 parent cde62d3 commit 7b026c2

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

src/main/java/com/example/demo/Application.java

+58-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.boot.autoconfigure.SpringBootApplication;
77
import org.springframework.context.annotation.Bean;
88

9+
import java.time.LocalDateTime;
910
import java.util.List;
1011

1112
@SpringBootApplication
@@ -15,15 +16,13 @@ public static void main(String[] args) {
1516
SpringApplication.run(Application.class, args);
1617
}
1718

18-
/*Code running after the application started*/
1919
@Bean
20-
CommandLineRunner commandLineRunner(StudentRepository studentRepository) {
21-
return args -> generateRandomStudents(studentRepository);
22-
}
20+
CommandLineRunner commandLineRunner(
21+
StudentRepository studentRepository,
22+
StudentIdCardRepository studentIdCardRepository) {
23+
return args -> {
24+
Faker faker = new Faker();
2325

24-
private void generateRandomStudents(StudentRepository studentRepository) {
25-
Faker faker = new Faker();
26-
for (int i = 0; i < 20; i++) {
2726
String firstName = faker.name().firstName();
2827
String lastName = faker.name().lastName();
2928
String email = String.format("%s.%[email protected]", firstName, lastName);
@@ -32,8 +31,57 @@ private void generateRandomStudents(StudentRepository studentRepository) {
3231
lastName,
3332
email,
3433
faker.number().numberBetween(17, 55));
34+
35+
student.addBook(
36+
new Book("Clean Code", LocalDateTime.now().minusDays(4)));
37+
38+
student.addBook(
39+
new Book("Think and Grow Rich", LocalDateTime.now()));
40+
41+
student.addBook(
42+
new Book("Spring Data JPA", LocalDateTime.now().minusYears(1)));
43+
44+
StudentIdCard studentIdCard = new StudentIdCard(
45+
"123456789",
46+
student);
47+
48+
student.setStudentIdCard(studentIdCard);
49+
50+
student.addEnrollment(new Enrollment(
51+
new EnrollmentId(1L, 1L),
52+
student,
53+
new Course("Computer Science", "IT"),
54+
LocalDateTime.now()
55+
));
56+
57+
student.addEnrollment(new Enrollment(
58+
new EnrollmentId(1L, 2L),
59+
student,
60+
new Course("Amigoscode Spring Data JPA", "IT"),
61+
LocalDateTime.now().minusDays(18)
62+
));
63+
64+
student.addEnrollment(new Enrollment(
65+
new EnrollmentId(1L, 2L),
66+
student,
67+
new Course("Amigoscode Spring Data JPA", "IT"),
68+
LocalDateTime.now().minusDays(18)
69+
));
70+
71+
72+
3573
studentRepository.save(student);
36-
}
37-
}
3874

39-
}
75+
studentRepository.findById(1L)
76+
.ifPresent(s -> {
77+
System.out.println("fetch book lazy...");
78+
List<Book> books = student.getBooks();
79+
books.forEach(book -> {
80+
System.out.println(
81+
s.getFirstName() + " borrowed " + book.getBookName());
82+
});
83+
});
84+
85+
};
86+
}
87+
}

0 commit comments

Comments
 (0)