-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.java
More file actions
118 lines (111 loc) · 4.36 KB
/
Main.java
File metadata and controls
118 lines (111 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.booleanuk;
import com.booleanuk.cohorts.models.*;
import com.booleanuk.cohorts.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
@SpringBootApplication
public class Main implements CommandLineRunner {
@Autowired
private RoleRepository roleRepository;
@Autowired
private CohortRepository cohortRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private ProfileRepository profileRepository;
@Autowired
private PostRepository postRepository;
@Autowired
PasswordEncoder encoder;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Override
public void run(String... args) {
Role teacherRole;
if (!this.roleRepository.existsByName(ERole.ROLE_TEACHER)) {
teacherRole = this.roleRepository.save(new Role(ERole.ROLE_TEACHER));
} else {
teacherRole = this.roleRepository.findByName(ERole.ROLE_TEACHER).orElse(null);
}
Set<Role> teacherRoles = new HashSet<>();
teacherRoles.add(teacherRole);
Role studentRole;
if (!this.roleRepository.existsByName(ERole.ROLE_STUDENT)) {
studentRole = this.roleRepository.save(new Role(ERole.ROLE_STUDENT));
} else {
studentRole = this.roleRepository.findByName(ERole.ROLE_STUDENT).orElse(null);
}
Set<Role> studentRoles = new HashSet<>();
studentRoles.add(studentRole);
if (!this.roleRepository.existsByName(ERole.ROLE_ADMIN)) {
this.roleRepository.save(new Role(ERole.ROLE_ADMIN));
}
// Create a cohort.
Cohort cohort;
if (!this.cohortRepository.existsById(1)) {
cohort = this.cohortRepository.save(new Cohort());
} else {
cohort = this.cohortRepository.findById(1).orElse(null);
}
// Create some users
User studentUser;
if (!this.userRepository.existsById(1)) {
studentUser = new User("student@test.com", this.encoder.encode("Testpassword1!"), cohort);
studentUser.setRoles(studentRoles);
studentUser = this.userRepository.save(studentUser);
} else {
studentUser = this.userRepository.findById(1).orElse(null);
}
Profile studentProfile;
if (!this.profileRepository.existsById(1)) {
studentProfile = this.profileRepository.save(new Profile(studentUser,
"Joe",
"Bloggs",
"Hello world!",
"student1",
"11111111",
"Backend Development",
LocalDate.of(2025, 9, 8
), LocalDate.of(2026, 9, 8)
));
} else {
studentProfile = this.profileRepository.findById(1).orElse(null);
}
User teacherUser;
if (!this.userRepository.existsById(2)) {
teacherUser = new User("dave@email.com", this.encoder.encode("password"));
teacherUser.setRoles(teacherRoles);
teacherUser = this.userRepository.save(teacherUser);
} else {
teacherUser = this.userRepository.findById(2).orElse(null);
}
Profile teacherProfile;
if (!this.profileRepository.existsById(2)) {
teacherProfile = this.profileRepository.save(new Profile(teacherUser,
"Rick",
"Sanchez",
"Hello there!",
"teacher1",
"88888888",
"Everything",
LocalDate.of(1962, 9, 8),
LocalDate.of(2062, 9, 8)
));
} else {
teacherProfile = this.profileRepository.findById(2).orElse(null);
}
if (!this.postRepository.existsById(1)) {
this.postRepository.save(new Post(studentUser, "My first post!"));
}
if (!this.postRepository.existsById(2)) {
this.postRepository.save(new Post(teacherUser, "Hello, students!"));
}
}
}