ParticipantController.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package fr.triplea.demovote.web.controller;
  2. import java.math.BigDecimal;
  3. import java.time.LocalDateTime;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.UUID;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.security.access.prepost.PreAuthorize;
  11. import org.springframework.security.crypto.password.PasswordEncoder;
  12. import org.springframework.web.bind.annotation.CrossOrigin;
  13. import org.springframework.web.bind.annotation.DeleteMapping;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PathVariable;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.PutMapping;
  18. import org.springframework.web.bind.annotation.RequestBody;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import fr.triplea.demovote.dao.ParticipantRepository;
  23. import fr.triplea.demovote.dto.ParticipantList;
  24. import fr.triplea.demovote.dto.ParticipantOptionList;
  25. import fr.triplea.demovote.dto.ParticipantTransfer;
  26. import fr.triplea.demovote.model.Participant;
  27. import fr.triplea.demovote.model.ParticipantModePaiement;
  28. import fr.triplea.demovote.model.ParticipantStatut;
  29. @CrossOrigin(origins = "http://localhost:4200")
  30. @RestController
  31. @RequestMapping("/participant")
  32. public class ParticipantController
  33. {
  34. @Autowired
  35. private ParticipantRepository participantRepository;
  36. @Autowired
  37. private PasswordEncoder passwordEncoder;
  38. @GetMapping(value = "/list")
  39. @PreAuthorize("hasRole('ORGA')")
  40. public List<ParticipantList> getList(@RequestParam("nom") String filtreNom, @RequestParam("statut") int filtreStatut, @RequestParam("arrive") int filtreArrive, @RequestParam("tri") int tri)
  41. {
  42. if (filtreNom != null) { if (filtreNom.isBlank()) { filtreNom = null; } else { filtreNom = filtreNom.trim().toUpperCase(); } }
  43. if (tri == 1) { return participantRepository.getListOrderedByDateInscription(filtreNom, filtreStatut, filtreArrive); }
  44. return participantRepository.getListOrderedByNom(filtreNom, filtreStatut, filtreArrive);
  45. }
  46. @GetMapping(value = "/option-list")
  47. @PreAuthorize("hasRole('ORGA')")
  48. public List<ParticipantOptionList> getOptionList()
  49. {
  50. return participantRepository.getOptionList();
  51. }
  52. @GetMapping(value = "/form/{id}")
  53. @PreAuthorize("hasRole('ORGA')")
  54. public ResponseEntity<ParticipantTransfer> getForm(@PathVariable int id)
  55. {
  56. ParticipantTransfer p = participantRepository.searchById(id);
  57. if (p != null) { return ResponseEntity.ok(p); }
  58. return ResponseEntity.notFound().build();
  59. }
  60. @PostMapping(value = "/create")
  61. @PreAuthorize("hasRole('ORGA')")
  62. public ResponseEntity<Object> create(@RequestBody(required = true) ParticipantTransfer participant)
  63. {
  64. Participant found = participantRepository.findById(0);
  65. if (found == null)
  66. {
  67. if (!(participant.nom().isBlank()))
  68. {
  69. if (!(participant.pseudonyme().isBlank()))
  70. {
  71. found = new Participant();
  72. found.setRoles(found.getRoles());
  73. found.setEnabled(true);
  74. found.setNom(participant.nom());
  75. found.setPrenom(participant.prenom());
  76. found.setPseudonyme(participant.pseudonyme());
  77. final String mdp = participant.motDePasse();
  78. if (mdp != null) { if (!(mdp.isBlank())) { found.setMotDePasse(passwordEncoder.encode(mdp.trim())); } }
  79. found.setGroupe(participant.groupe());
  80. found.setDelaiDeconnexion(participant.delaiDeconnexion());
  81. found.setAdresse(participant.adresse());
  82. found.setCodePostal(participant.codePostal());
  83. found.setVille(participant.ville());
  84. found.setPays(participant.pays());
  85. found.setNumeroTelephone(participant.numeroTelephone());
  86. found.setEmail(participant.email());
  87. if (participant.statut().equals("PAYE_CHEQUE")) { found.setStatut(ParticipantStatut.PAYE_CHEQUE); }
  88. else if(participant.statut().equals("PAYE_ESPECES")) { found.setStatut(ParticipantStatut.PAYE_ESPECES); }
  89. else if(participant.statut().equals("VIREMENT_BANCAIRE")) { found.setStatut(ParticipantStatut.VIREMENT_BANCAIRE); }
  90. else if(participant.statut().equals("VIREMENT_PAYPAL")) { found.setStatut(ParticipantStatut.VIREMENT_PAYPAL); }
  91. else if(participant.statut().equals("ORGA")) { found.setStatut(ParticipantStatut.ORGA); }
  92. else if(participant.statut().equals("GUEST")) { found.setStatut(ParticipantStatut.GUEST); }
  93. else { found.setStatut(ParticipantStatut.EN_ATTENTE); }
  94. found.setWithMachine(participant.withMachine());
  95. found.setCommentaire(participant.commentaire());
  96. found.setHereDay1(participant.hereDay1());
  97. found.setHereDay2(participant.hereDay2());
  98. found.setHereDay3(participant.hereDay3());
  99. found.setSleepingOnSite(participant.sleepingOnSite());
  100. found.setUseAmigabus(participant.useAmigabus());
  101. if (participant.modePaiement().equals("CHEQUE")) { found.setModePaiement(ParticipantModePaiement.CHEQUE); }
  102. else if(participant.modePaiement().equals("VIREMENT")) { found.setModePaiement(ParticipantModePaiement.VIREMENT); }
  103. else if(participant.modePaiement().equals("PAYPAL")) { found.setModePaiement(ParticipantModePaiement.PAYPAL); }
  104. else if(participant.modePaiement().equals("ESPECES")) { found.setModePaiement(ParticipantModePaiement.ESPECES); }
  105. else { found.setModePaiement(ParticipantModePaiement.AUTRE); }
  106. try { found.setSommeRecue(new BigDecimal(participant.sommeRecue())); } catch (Exception e) { found.setSommeRecue(new BigDecimal("0.00")); }
  107. found.setDateInscription(LocalDateTime.now());
  108. found.setArrived(participant.arrived());
  109. // TODO: set roles
  110. Participant created = participantRepository.save(found);
  111. return ResponseEntity.ok(created);
  112. }
  113. }
  114. }
  115. return null;
  116. }
  117. @PutMapping(value = "/update/{id}")
  118. @PreAuthorize("hasRole('ORGA')")
  119. public ResponseEntity<Object> update(@PathVariable int id, @RequestBody(required = true) ParticipantTransfer participant)
  120. {
  121. Participant found = participantRepository.findById(id);
  122. if (found != null)
  123. {
  124. found.setRoles(found.getRoles());
  125. found.setEnabled(true);
  126. found.setNom(participant.nom());
  127. found.setPrenom(participant.prenom());
  128. found.setPseudonyme(participant.pseudonyme());
  129. final String mdp = participant.motDePasse();
  130. if (mdp != null) { if (!(mdp.isBlank())) { found.setMotDePasse(passwordEncoder.encode(mdp.trim())); } }
  131. found.setGroupe(participant.groupe());
  132. found.setDelaiDeconnexion(participant.delaiDeconnexion());
  133. found.setAdresse(participant.adresse());
  134. found.setCodePostal(participant.codePostal());
  135. found.setVille(participant.ville());
  136. found.setPays(participant.pays());
  137. found.setNumeroTelephone(participant.numeroTelephone());
  138. found.setEmail(participant.email());
  139. if (participant.statut().equals("PAYE_CHEQUE")) { found.setStatut(ParticipantStatut.PAYE_CHEQUE); }
  140. else if(participant.statut().equals("PAYE_ESPECES")) { found.setStatut(ParticipantStatut.PAYE_ESPECES); }
  141. else if(participant.statut().equals("VIREMENT_BANCAIRE")) { found.setStatut(ParticipantStatut.VIREMENT_BANCAIRE); }
  142. else if(participant.statut().equals("VIREMENT_PAYPAL")) { found.setStatut(ParticipantStatut.VIREMENT_PAYPAL); }
  143. else if(participant.statut().equals("ORGA")) { found.setStatut(ParticipantStatut.ORGA); }
  144. else if(participant.statut().equals("GUEST")) { found.setStatut(ParticipantStatut.GUEST); }
  145. else { found.setStatut(ParticipantStatut.EN_ATTENTE); }
  146. found.setWithMachine(participant.withMachine());
  147. found.setCommentaire(participant.commentaire());
  148. found.setHereDay1(participant.hereDay1());
  149. found.setHereDay2(participant.hereDay2());
  150. found.setHereDay3(participant.hereDay3());
  151. found.setSleepingOnSite(participant.sleepingOnSite());
  152. found.setUseAmigabus(participant.useAmigabus());
  153. if (participant.modePaiement().equals("CHEQUE")) { found.setModePaiement(ParticipantModePaiement.CHEQUE); }
  154. else if(participant.modePaiement().equals("VIREMENT")) { found.setModePaiement(ParticipantModePaiement.VIREMENT); }
  155. else if(participant.modePaiement().equals("PAYPAL")) { found.setModePaiement(ParticipantModePaiement.PAYPAL); }
  156. else if(participant.modePaiement().equals("ESPECES")) { found.setModePaiement(ParticipantModePaiement.ESPECES); }
  157. else { found.setModePaiement(ParticipantModePaiement.AUTRE); }
  158. try { found.setSommeRecue(new BigDecimal(participant.sommeRecue())); } catch (Exception e) { found.setSommeRecue(new BigDecimal("0.00")); }
  159. found.setArrived(participant.arrived());
  160. // TODO: modify password in session
  161. // TODO: modify roles
  162. Participant updated = participantRepository.save(found);
  163. return ResponseEntity.ok(updated);
  164. }
  165. return ResponseEntity.notFound().build();
  166. }
  167. @DeleteMapping(value = "/delete/{id}")
  168. @PreAuthorize("hasRole('ORGA')")
  169. public ResponseEntity<Map<String, Boolean>> disableParticipant(@PathVariable int id)
  170. {
  171. Participant found = participantRepository.getReferenceById(id);
  172. if (found != null)
  173. {
  174. found.setEnabled(false);
  175. found.setPseudonyme(found.getPseudonyme() + "_" + UUID.randomUUID().toString());
  176. participantRepository.saveAndFlush(found);
  177. Map<String, Boolean> response = new HashMap<>();
  178. response.put("deleted", Boolean.TRUE);
  179. return ResponseEntity.ok(response);
  180. }
  181. return ResponseEntity.notFound().build();
  182. }
  183. }