diff options
Diffstat (limited to 'src/main/java/com/stileeducation/markr/service/StudentService.java')
| -rw-r--r-- | src/main/java/com/stileeducation/markr/service/StudentService.java | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/main/java/com/stileeducation/markr/service/StudentService.java b/src/main/java/com/stileeducation/markr/service/StudentService.java new file mode 100644 index 0000000..4af4d63 --- /dev/null +++ b/src/main/java/com/stileeducation/markr/service/StudentService.java @@ -0,0 +1,28 @@ +package com.stileeducation.markr.service; + +import com.stileeducation.markr.entity.Student; +import com.stileeducation.markr.repository.StudentRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +public class StudentService { + + @Autowired + private StudentRepository studentRepository; + + public Student findOrCreateStudent(String firstName, String lastName, String studentNumber) { + Optional<Student> optionalStudent = studentRepository.findByStudentNumber(studentNumber); + if (optionalStudent.isPresent()) { + return optionalStudent.get(); + } else { + Student student = new Student(); + student.setFirstName(firstName); + student.setLastName(lastName); + student.setStudentNumber(studentNumber); + return studentRepository.save(student); + } + } +} |
