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
| @Component public class CourseRepository {
private final Map<Integer, Course> courses;
public CourseRepository() { this.courses = new HashMap<>();
Course course1 = Course.newBuilder().setId(1) .setCourseName("REST with Spring").addAllStudent(createTestStudents()).build(); Course course2 = Course.newBuilder().setId(2) .setCourseName("Learn Spring Security").addAllStudent(new ArrayList<>()).build();
courses.put(course1.getId(), course1); courses.put(course2.getId(), course2); }
public Course getCourse(int id) { return courses.get(id); }
private List<Student> createTestStudents() { PhoneNumber phone1 = createPhone("123456", PhoneType.MOBILE); Student student1 = createStudent(1, "John", "Doe", "john.doe@baeldung.com", List.of(phone1));
PhoneNumber phone2 = createPhone("234567", PhoneType.LANDLINE); Student student2 = createStudent(2, "Richard", "Roe", "richard.roe@baeldung.com", List.of(phone2));
PhoneNumber phone3_1 = createPhone("345678", PhoneType.MOBILE); PhoneNumber phone3_2 = createPhone("456789", PhoneType.LANDLINE); Student student3 = createStudent(3, "Jane", "Doe", "jane.doe@baeldung.com", Arrays.asList(phone3_1, phone3_2));
return Arrays.asList(student1, student2, student3); }
private Student createStudent(int id, String firstName, String lastName, String email, List<PhoneNumber> phones) { return Student.newBuilder().setId(id) .setFirstName(firstName).setLastName(lastName).setEmail(email).addAllPhone(phones).build(); }
private PhoneNumber createPhone(String number, PhoneType type) { return PhoneNumber.newBuilder().setNumber(number).setType(type).build(); } }
|