ConsommationController.java
4.69 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package etunicorn.controller;
import etunicorn.RestrictedTo;
import etunicorn.entity.Consommation;
import etunicorn.entity.Personne;
import etunicorn.generated.model.UpdateAcheteRequest;
import etunicorn.generated.model.UpdateConsommationByIdRequest;
import etunicorn.generated.model.UpdateConsommationRequest;
import etunicorn.repository.ConsommationRepository;
import etunicorn.service.PersonneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.LinkedHashMap;
/**
* etunicorn-server
* Copyright © 2017 Le Club Info Polytech Lille
* Tous droits réservés
*/
@RestController
public class ConsommationController extends BaseController implements etunicorn.generated.ConsommationController {
@Autowired
ConsommationRepository consommationRepository;
@Autowired
PersonneService personneService;
@Override
@RestrictedTo("CONSO_LIST")
public ResponseEntity<?> getConsommation() {
return new ResponseEntity<Object>(this.consommationRepository.findAll(), HttpStatus.OK);
}
private ResponseEntity<?> mergeConsommation(Consommation consommation, UpdateConsommationRequest updateConsommationRequest) {
if (updateConsommationRequest.getNom() != null) {
consommation.setNom(updateConsommationRequest.getNom());
}
if (updateConsommationRequest.getPrix() != null) {
consommation.setPrix(updateConsommationRequest.getPrix());
}
consommationRepository.save(consommation);
return new ResponseEntity(consommation, HttpStatus.CREATED);
}
@Override
@RestrictedTo("CONSO_ADD")
public ResponseEntity<?> updateConsommation(@Valid @RequestBody UpdateConsommationRequest updateConsommationRequest) {
Consommation consommation = new Consommation();
return mergeConsommation(consommation, updateConsommationRequest);
}
@Override
@RestrictedTo("CONSO_EDIT")
public ResponseEntity<?> updateConsommationById(@PathVariable Long idConsommation, @Valid @RequestBody UpdateConsommationByIdRequest updateConsommationByIdRequest) {
Consommation consommation = consommationRepository.findById(idConsommation.intValue());
if (consommation == null) {
return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
}
UpdateConsommationRequest updateConsommationRequest = new UpdateConsommationRequest();
updateConsommationRequest.setNom(updateConsommationByIdRequest.getNom());
updateConsommationRequest.setPrix(updateConsommationByIdRequest.getPrix());
return mergeConsommation(consommation, updateConsommationRequest);
}
@Override
@RestrictedTo("CONSO_GET")
public ResponseEntity<?> getConsommationById(@PathVariable Long idConsommation) {
Consommation consommation = consommationRepository.findById(idConsommation.intValue());
if (consommation == null) {
return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
}
return new ResponseEntity(consommation, HttpStatus.OK);
}
@Override
@RestrictedTo("CONSO_REMOVE")
public ResponseEntity<?> deleteConsommationById(@PathVariable Long idConsommation) {
Consommation consommation = consommationRepository.findById(idConsommation.intValue());
if (consommation == null) {
return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
}
consommationRepository.delete(consommation);
return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
@Override
@RestrictedTo("CONSO_ACHAT")
public ResponseEntity<?> updateAchete(@PathVariable Long idConsommation, @Valid @RequestBody UpdateAcheteRequest updateAcheteRequest) {
Consommation consommation = consommationRepository.findById(idConsommation.intValue());
if (consommation == null) {
return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
}
if (!(updateAcheteRequest.getParticipant() instanceof LinkedHashMap)) {
return generateError(HttpStatus.BAD_REQUEST, "Participant doit être un objet");
}
Personne personne = personneService.getFromObject((LinkedHashMap<String, Object>) updateAcheteRequest.getParticipant());
if (personne == null) {
return generateError(HttpStatus.NOT_FOUND, "Personne inconnue");
}
// TODO To be continued...
return null;
}
}