ConsommationController.java
3.96 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.controller;
import etunicorn.RestrictedTo;
import etunicorn.entity.Consommation;
import etunicorn.generated.model.UpdateAcheteRequest;
import etunicorn.generated.model.UpdateConsommationByIdRequest;
import etunicorn.generated.model.UpdateConsommationRequest;
import etunicorn.repository.ConsommationRepository;
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;
/**
* 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;
@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 consommationId, @PathVariable String idConsommation, @Valid @RequestBody UpdateConsommationByIdRequest updateConsommationByIdRequest) {
Consommation consommation = consommationRepository.findById(consommationId.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 consommationId, @PathVariable String idConsommation) {
Consommation consommation = consommationRepository.findById(consommationId.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 consommationId, @PathVariable String idConsommation) {
Consommation consommation = consommationRepository.findById(consommationId.intValue());
if (consommation == null) {
return generateError(HttpStatus.NOT_FOUND, "Consommation inconnue");
}
consommationRepository.delete(consommation);
return null;
}
@Override
@RestrictedTo("CONSO_ACHAT")
public ResponseEntity<?> updateAchete(@PathVariable Long consommationId, @PathVariable String idConsommation, @Valid @RequestBody UpdateAcheteRequest updateAcheteRequest) {
return null;
}
}