-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathStudentService.java
More file actions
41 lines (33 loc) · 1.26 KB
/
StudentService.java
File metadata and controls
41 lines (33 loc) · 1.26 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
package com.example.demo.student;
import com.example.demo.student.exception.BadRequestException;
import com.example.demo.student.exception.StudentNotFoundException;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.List;
@AllArgsConstructor
@Service
public class StudentService {
private final StudentRepository studentRepository;
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
public void addStudent(Student student) {
Boolean existsEmail = studentRepository
.selectExistsEmail(student.getEmail());
if (existsEmail) {
throw new BadRequestException(
"Email " + student.getEmail() + " taken");
}
studentRepository.save(student);
}
public void deleteStudent(Long studentId) {
if(!studentRepository.existsById(studentId)) {
throw new StudentNotFoundException(
"Student with id " + studentId + " does not exists");
}
studentRepository.deleteById(studentId);
}
}