VariableController.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package fr.triplea.demovote.web.controller;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.http.ResponseEntity;
  5. //import org.springframework.security.access.prepost.PreAuthorize;
  6. import org.springframework.web.bind.annotation.CrossOrigin;
  7. import org.springframework.web.bind.annotation.DeleteMapping;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.PutMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import fr.triplea.demovote.persistence.dao.VariableRepository;
  17. import fr.triplea.demovote.persistence.model.Variable;
  18. @CrossOrigin(origins = "http://localhost:4200")
  19. @RestController
  20. @RequestMapping("/demovote-api/v1/variable")
  21. public class VariableController
  22. {
  23. @Autowired
  24. private VariableRepository variableRepository;
  25. @GetMapping(value = "/list")
  26. //@PreAuthorize("hasRole('LISTE_VARIABLES')")
  27. public List<Variable> getList(@RequestParam(required = false) String type)
  28. {
  29. if (type != null) { return variableRepository.findByType(type); }
  30. return variableRepository.findAll();
  31. }
  32. @GetMapping(value = "/form/{id}")
  33. //@PreAuthorize("hasRole('LISTE_VARIABLES')")
  34. public ResponseEntity<Variable> getForm(@PathVariable int id)
  35. {
  36. Variable v = variableRepository.findById(id);
  37. if (v != null) { return ResponseEntity.ok(v); }
  38. return ResponseEntity.notFound().build();
  39. }
  40. @PostMapping(value = "/create")
  41. //@PreAuthorize("hasRole('LISTE_VARIABLES')")
  42. public Variable create(@RequestBody(required = true) Variable variable)
  43. {
  44. return variableRepository.save(variable);
  45. }
  46. @PutMapping(value = "/update/{id}")
  47. //@PreAuthorize("hasRole('LISTE_VARIABLES')")
  48. public ResponseEntity<Variable> update(@PathVariable int id, @RequestBody(required = true) Variable variable)
  49. {
  50. Variable found = variableRepository.findById(id);
  51. if (found != null)
  52. {
  53. found.setType(variable.getType());
  54. found.setCode(variable.getCode());
  55. found.setValeur(variable.getValeur());
  56. found.setNotes(variable.getNotes());
  57. Variable updated = variableRepository.save(found);
  58. return ResponseEntity.ok(updated);
  59. }
  60. return ResponseEntity.notFound().build();
  61. }
  62. @DeleteMapping(value = "/delete/{id}")
  63. //@PreAuthorize("hasRole('LISTE_VARIABLES')")
  64. public ResponseEntity<Object> deleteVariable(@PathVariable int id)
  65. {
  66. variableRepository.deleteById(id);
  67. return ResponseEntity.ok().build();
  68. }
  69. }