forked from NeilAlishev/SpringCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeopleService.java
More file actions
53 lines (42 loc) · 1.31 KB
/
PeopleService.java
File metadata and controls
53 lines (42 loc) · 1.31 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
45
46
47
48
49
50
51
52
53
package ru.alishev.springcourse.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.alishev.springcourse.models.Person;
import ru.alishev.springcourse.repositories.PeopleRepository;
import java.util.Date;
import java.util.List;
import java.util.Optional;
/**
* @author Neil Alishev
*/
@Service
@Transactional(readOnly = true)
public class PeopleService {
private final PeopleRepository peopleRepository;
@Autowired
public PeopleService(PeopleRepository peopleRepository) {
this.peopleRepository = peopleRepository;
}
public List<Person> findAll() {
return peopleRepository.findAll();
}
public Person findOne(int id) {
Optional<Person> foundPerson = peopleRepository.findById(id);
return foundPerson.orElse(null);
}
@Transactional
public void save(Person person) {
person.setCreatedAt(new Date());
peopleRepository.save(person);
}
@Transactional
public void update(int id, Person updatedPerson) {
updatedPerson.setId(id);
peopleRepository.save(updatedPerson);
}
@Transactional
public void delete(int id) {
peopleRepository.deleteById(id);
}
}