|
|
@@ -1,14 +1,19 @@
|
|
|
package fr.jaquin.bdlg.planner.controller;
|
|
|
|
|
|
+import java.security.Principal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
-import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
+import java.util.Optional;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
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;
|
|
|
@@ -17,69 +22,95 @@ import fr.jaquin.bdlg.planner.persistence.EvenementLobRepository;
|
|
|
import fr.jaquin.bdlg.planner.persistence.EvenementNotFoundException;
|
|
|
import fr.jaquin.bdlg.planner.persistence.EvenementRepository;
|
|
|
import fr.jaquin.bdlg.planner.persistence.CustomUser;
|
|
|
+import fr.jaquin.bdlg.planner.persistence.CustomUserRepository;
|
|
|
|
|
|
-@RestController("/api")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api")
|
|
|
public class ApiController {
|
|
|
|
|
|
private final EvenementRepository repository;
|
|
|
|
|
|
private final EvenementLobRepository repositoryLob;
|
|
|
|
|
|
- public ApiController(EvenementRepository repository, EvenementLobRepository repositoryLob) {
|
|
|
+ private final CustomUserRepository repositoryUser;
|
|
|
+
|
|
|
+ public ApiController(EvenementRepository repository, EvenementLobRepository repositoryLob,
|
|
|
+ CustomUserRepository repositoryUser) {
|
|
|
this.repository = repository;
|
|
|
this.repositoryLob = repositoryLob;
|
|
|
+ this.repositoryUser = repositoryUser;
|
|
|
}
|
|
|
|
|
|
+ @CrossOrigin(origins = "http://localhost:8081")
|
|
|
@GetMapping("/evenements")
|
|
|
public List<Evenement> getEvenements() {
|
|
|
- return (List<Evenement>) repository.findAll();
|
|
|
+ return (List<Evenement>) repository.findByLastVersionIsTrue();
|
|
|
}
|
|
|
|
|
|
// Single evenement data
|
|
|
+ @CrossOrigin(origins = "http://localhost:8081")
|
|
|
@GetMapping("/evenements/{uuid}")
|
|
|
String one(@PathVariable String uuid) {
|
|
|
- List<Evenement> allVersion = repository.findByUuid(uuid);
|
|
|
- if (allVersion.size() < 1) {
|
|
|
+ List<Evenement> allVersions = repository.findFirst1ByUuidOrderByLastModifiedDesc(uuid);
|
|
|
+ if (allVersions.size() < 1) {
|
|
|
throw new EvenementNotFoundException(uuid);
|
|
|
}
|
|
|
- Evenement lastVersion = allVersion.get(0);
|
|
|
- for (Evenement evenement : allVersion) {
|
|
|
- if (evenement.getLastModified().isAfter(evenement.getLastModified())) {
|
|
|
- lastVersion = evenement;
|
|
|
- }
|
|
|
- }
|
|
|
- return repositoryLob.findById(lastVersion.getId()).orElseThrow(() -> new EvenementNotFoundException(uuid))
|
|
|
- .getJsonContent();
|
|
|
+ return repositoryLob.findById(allVersions.get(0).getId())
|
|
|
+ .orElseThrow(() -> new EvenementNotFoundException(uuid)).getJsonContent();
|
|
|
}
|
|
|
|
|
|
+ @CrossOrigin(origins = "http://localhost:8081")
|
|
|
@GetMapping("/evenements/history/{uuid}")
|
|
|
List<Evenement> history(@PathVariable String uuid) {
|
|
|
- return repository.findByUuid(uuid);
|
|
|
+ return repository.findByUuidOrderByLastModifiedDesc(uuid);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/evenements/history/{uuid}/content/{id}")
|
|
|
+ String historyContent(@PathVariable String uuid, @PathVariable Long id) {
|
|
|
+ Optional<EvenementLob> lob = repositoryLob.findById(id);
|
|
|
+ if (lob.isPresent() && lob.get().getEvenement().getUuid().equals(uuid)) {
|
|
|
+ return lob.get().getJsonContent();
|
|
|
+ } else {
|
|
|
+ throw new EvenementNotFoundException(uuid);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@PostMapping("/evenements")
|
|
|
- Evenement newEvenement(@AuthenticationPrincipal CustomUser customUser, @RequestBody EvenementData evt) {
|
|
|
- if (repository.findByUuid(evt.getUuid()).size() > 0) {
|
|
|
- throw new EvenementAlreadyExistException(evt.getUuid());
|
|
|
+ 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, customUser);
|
|
|
+ return saveEvenementData(evt, principal.getName());
|
|
|
}
|
|
|
|
|
|
@PutMapping("/evenements/{uuid}")
|
|
|
- Evenement newEvenement(@AuthenticationPrincipal CustomUser customUser, @RequestBody EvenementData evt,
|
|
|
- @PathVariable String id) {
|
|
|
- if (repository.findByUuid(evt.getUuid()).size() == 0) {
|
|
|
- throw new EvenementNotFoundException(evt.getUuid());
|
|
|
+ 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());
|
|
|
}
|
|
|
- return saveEvenementData(evt, customUser);
|
|
|
}
|
|
|
|
|
|
- private Evenement saveEvenementData(EvenementData evt, CustomUser customUser) {
|
|
|
+ private Evenement saveEvenementData(EvenementData evt, String username) {
|
|
|
+ CustomUser 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);
|
|
|
EvenementLob blob = new EvenementLob();
|
|
|
blob.setEvenement(newEvenement);
|