PersonneController.java
3.49 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package etunicorn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
* Created by geoffrey on 28/01/17.
*/
@RestController
public class PersonneController implements etunicorn.generated.PersonneController {
@Autowired
private PersonneRepository personneRepository;
@Autowired
private RoleRepository roleRepository;
@Override
public ResponseEntity<?> getPersonne() {
return new ResponseEntity<List>((List) this.personneRepository.findAll(), HttpStatus.OK);
}
private ResponseEntity<?> mergePersonne(Personne personne, String carte, Date naissance, String login, String role) {
if (carte != null) {
personne.setCarte(carte);
}
if (naissance != null) {
personne.setNaissance(naissance);
}
if (login != null) {
personne.setLogin(login);
}
if (role != null) {
Role roleObj = roleRepository.findByNom(role);
personne.setRole(roleObj);
if (roleObj == null) {
return new ResponseEntity<Object>("Rôle inconnu", HttpStatus.NOT_FOUND);
}
}
try {
this.personneRepository.save(personne);
} catch (DataIntegrityViolationException e) {
return new ResponseEntity<Object>(HttpStatus.CONFLICT);
}
return new ResponseEntity<Object>(personne, HttpStatus.CREATED);
}
@Override
public ResponseEntity<?> updatePersonne(@RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) {
Personne personne = new Personne();
return mergePersonne(personne, carte, naissance, login, role);
}
@Override
public ResponseEntity<?> getPersonneById(@PathVariable BigDecimal idPersonne) {
Personne personne = personneRepository.findById(idPersonne.intValueExact());
if (personne == null) {
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Object>(personne, HttpStatus.OK);
}
@Override
public ResponseEntity<?> updatePersonneById(@PathVariable BigDecimal idPersonne, @RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) {
Personne personne = personneRepository.findById(idPersonne.intValueExact());
if (personne == null) {
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
return mergePersonne(personne, carte, naissance, login, role);
}
@Override
public ResponseEntity<?> deletePersonneById(@PathVariable BigDecimal idPersonne) {
Personne personne = personneRepository.findById(idPersonne.intValueExact());
if (personne == null) {
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
personneRepository.delete(personne);
return new ResponseEntity<Object>(personne, HttpStatus.NO_CONTENT);
}
}