|
|
@@ -0,0 +1,209 @@
|
|
|
+package fr.jaquin.bdlg.planner.controller;
|
|
|
+
|
|
|
+import java.security.Principal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.PutMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.server.ResponseStatusException;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.Evenement;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.EvenementAlreadyExistException;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.EvenementData;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.EvenementDataLob;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.EvenementNotFoundException;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.PlanningView;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.repositories.EvenementLobRepository;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.repositories.EvenementRepository;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.repositories.MyUserRepository;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.repositories.PlanningViewRepository;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.repositories.QuestionnaireQuestionRepo;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.repositories.QuestionnaireRepo;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.repositories.RegistrationRepository;
|
|
|
+import fr.jaquin.bdlg.planner.security.MyUser;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api")
|
|
|
+public class ApiEvenementController {
|
|
|
+
|
|
|
+ private final EvenementRepository repository;
|
|
|
+
|
|
|
+ private final EvenementLobRepository repositoryLob;
|
|
|
+ private final PlanningViewRepository repositoryView;
|
|
|
+
|
|
|
+ private final MyUserRepository repositoryUser;
|
|
|
+
|
|
|
+ public ApiEvenementController(EvenementRepository repository,
|
|
|
+ EvenementLobRepository repositoryLob, PlanningViewRepository repositoryView,
|
|
|
+ MyUserRepository repositoryUser, RegistrationRepository repositoryVolunteer,
|
|
|
+ QuestionnaireRepo repositoryQuestionnaire,
|
|
|
+ QuestionnaireQuestionRepo repositoryQuestionnaireQuestion) {
|
|
|
+ this.repository = repository;
|
|
|
+ this.repositoryLob = repositoryLob;
|
|
|
+ this.repositoryUser = repositoryUser;
|
|
|
+ this.repositoryView = repositoryView;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/evenements")
|
|
|
+ public List<Evenement> getEvenements() {
|
|
|
+ return (List<Evenement>) repository.findByLastVersionIsTrue();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Single evenement data
|
|
|
+ @GetMapping("/evenements/{uuid}")
|
|
|
+ String one(@PathVariable String uuid) {
|
|
|
+ List<Evenement> allVersions = repository.findFirst1ByUuidOrderByLastModifiedDesc(uuid);
|
|
|
+ if (allVersions.size() < 1) {
|
|
|
+ throw new EvenementNotFoundException(uuid);
|
|
|
+ }
|
|
|
+ return repositoryLob.findById(allVersions.get(0).getId())
|
|
|
+ .orElseThrow(() -> new EvenementNotFoundException(uuid)).getJsonContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/evenements/history/{uuid}")
|
|
|
+ List<Evenement> history(@PathVariable String uuid) {
|
|
|
+ return repository.findByUuidOrderByLastModifiedDesc(uuid);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/evenements/history/{uuid}/content/{id}")
|
|
|
+ String historyContent(@PathVariable String uuid, @PathVariable Long id) {
|
|
|
+ Optional<EvenementDataLob> lob = repositoryLob.findById(id);
|
|
|
+ if (lob.isPresent() && lob.get().getEvenement().getUuid().equals(uuid)) {
|
|
|
+ return lob.get().getJsonContent();
|
|
|
+ } else {
|
|
|
+ throw new EvenementNotFoundException(uuid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("/evenements/history/{uuid}/content/{id}")
|
|
|
+ boolean deleteHistoryEvenement(@PathVariable String uuid, @PathVariable Long id) {
|
|
|
+ Optional<Evenement> evt = repository.findById(id);
|
|
|
+ if (!evt.isPresent()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (evt.get().getUuid().equals(uuid)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ repositoryLob.deleteById(id);
|
|
|
+ repository.deleteById(id);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/evenements")
|
|
|
+ Evenement newEvenement(Principal principal, @RequestBody EvenementData evt) {
|
|
|
+ if (repository.findFirst1ByUuidOrderByLastModifiedDesc(evt.getUuid()).size() > 0) {
|
|
|
+ throw new ResponseStatusException(HttpStatus.CONFLICT, "",
|
|
|
+ new EvenementAlreadyExistException(evt.getUuid()));
|
|
|
+ }
|
|
|
+ return saveEvenementData(evt, principal.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ @PutMapping("/evenements/{uuid}")
|
|
|
+ Evenement updateEvenement(Principal principal, @RequestBody EvenementData evt,
|
|
|
+ @PathVariable String uuid) {
|
|
|
+ List<Evenement> previous = repository.findFirst1ByUuidOrderByLastModifiedDesc(uuid);
|
|
|
+ if (previous.size() == 0) {
|
|
|
+ throw new ResponseStatusException(HttpStatus.NOT_FOUND, "",
|
|
|
+ new EvenementNotFoundException(uuid));
|
|
|
+ }
|
|
|
+ Evenement lastversioEvenement = previous.get(0);
|
|
|
+ if (lastversioEvenement.getContentHash().equals(evt.getContentHash())) {
|
|
|
+ return lastversioEvenement;
|
|
|
+ } else {
|
|
|
+ lastversioEvenement.setLastVersion(false);
|
|
|
+ repository.save(lastversioEvenement);
|
|
|
+ return saveEvenementData(evt, principal.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("/evenements/{uuid}")
|
|
|
+ boolean deleteEvenement(@PathVariable String uuid) {
|
|
|
+ List<Evenement> list = repository.findByUuid(uuid);
|
|
|
+ for (Evenement evenement : list) {
|
|
|
+ repositoryLob.deleteById(evenement.getId());
|
|
|
+ }
|
|
|
+ repository.deleteAll(list);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Evenement saveEvenementData(EvenementData evt, String username) {
|
|
|
+ MyUser customUser = repositoryUser.findByUsername(username);
|
|
|
+ Evenement newEvenement = new Evenement();
|
|
|
+ newEvenement.setName(evt.getName());
|
|
|
+ newEvenement.setUuid(evt.getUuid());
|
|
|
+ newEvenement.setLastModified(LocalDateTime.now());
|
|
|
+ newEvenement.setLastEditor(customUser);
|
|
|
+ newEvenement.setLastVersion(true);
|
|
|
+ newEvenement.setContentHash(evt.getContentHash());
|
|
|
+ newEvenement = repository.save(newEvenement);
|
|
|
+ EvenementDataLob blob = new EvenementDataLob();
|
|
|
+ blob.setEvenement(newEvenement);
|
|
|
+ blob.setJsonContent(evt.getContent());
|
|
|
+ repositoryLob.save(blob);
|
|
|
+ return newEvenement;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/evenements/{uuid}/views")
|
|
|
+ List<PlanningView> getPlanningViews(@PathVariable String uuid) {
|
|
|
+ return repositoryView.findByPlanningUuid(uuid);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/evenements/{planningUuid}/views/{uuid}")
|
|
|
+ PlanningView getPlanningViews(@PathVariable String planningUuid, @PathVariable String uuid) {
|
|
|
+ Optional<PlanningView> res = repositoryView.findByUuid(uuid);
|
|
|
+ if (res.isPresent()) {
|
|
|
+ return res.get();
|
|
|
+ } else {
|
|
|
+ throw new ResponseStatusException(HttpStatus.NOT_FOUND, "",
|
|
|
+ new EvenementNotFoundException(uuid));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/evenements/{planningUuid}/views")
|
|
|
+ void createPlanningView(@PathVariable String planningUuid, @RequestBody PlanningView view) {
|
|
|
+ Optional<PlanningView> res = repositoryView.findByUuid(view.getUuid());
|
|
|
+ if (res.isPresent()) {
|
|
|
+ throw new ResponseStatusException(HttpStatus.CONFLICT, "",
|
|
|
+ new EvenementNotFoundException(view.getUuid()));
|
|
|
+ } else {
|
|
|
+ repositoryView.save(view);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PutMapping("/evenements/{planningUuid}/views/{uuid}")
|
|
|
+ void updatePlanningView(@PathVariable String planningUuid, @PathVariable String uuid,
|
|
|
+ @RequestBody PlanningView view) {
|
|
|
+ Optional<PlanningView> res = repositoryView.findByUuid(view.getUuid());
|
|
|
+ if (res.isPresent()) {
|
|
|
+ PlanningView obj = res.get();
|
|
|
+
|
|
|
+ obj.setStart(view.getStart());
|
|
|
+ obj.setEnd(view.getEnd());
|
|
|
+ obj.setName(view.getName());
|
|
|
+ obj.setRessourceUuidList(view.getRessourceUuidList());
|
|
|
+
|
|
|
+ repositoryView.save(obj);
|
|
|
+ } else {
|
|
|
+ throw new ResponseStatusException(HttpStatus.NOT_FOUND, "",
|
|
|
+ new EvenementNotFoundException(view.getUuid()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("/evenements/{planningUuid}/views/{uuid}")
|
|
|
+ void deletePlanningView(@PathVariable String planningUuid, @PathVariable String uuid) {
|
|
|
+ Optional<PlanningView> res = repositoryView.findByUuid(uuid);
|
|
|
+ if (res.isPresent()) {
|
|
|
+ repositoryView.delete(res.get());
|
|
|
+ } else {
|
|
|
+ throw new ResponseStatusException(HttpStatus.NOT_FOUND, "",
|
|
|
+ new EvenementNotFoundException(uuid));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|