summaryrefslogtreecommitdiff
path: root/src/main/java/com/stileeducation/markr/service/StudentService.java
blob: 3ce28c012a02016b947b9c3c6f2e2c1fc015345e (plain)
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
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);
      student.setCreated(true);
      return studentRepository.save(student);
    }
  }
}