-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathPersonService.java
More file actions
44 lines (35 loc) · 1.15 KB
/
PersonService.java
File metadata and controls
44 lines (35 loc) · 1.15 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
package io.zipcoder.crudapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class PersonService {
@Autowired
private PersonRepository repository;
public Person create(Person person){
return repository.save(person);
}
public Person read(Integer id) {
return repository.findById(id).get();
}
public List<Person> readAll() {
Iterable<Person> personIterable = repository.findAll();
List<Person> result = new ArrayList<>();
personIterable.forEach(result::add);
return result;
}
public Person update(Integer id, Person newPersonData) {
Person personInDatabase = read(id);
personInDatabase.setFirstName(newPersonData.getFirstName());
personInDatabase.setLastName(newPersonData.getLastName());
return repository.save(personInDatabase);
}
public Person delete(Person person) {
repository.delete(person);
return person;
}
public Person delete(Integer id) {
return delete(read(id));
}
}