|
|
@@ -0,0 +1,91 @@
|
|
|
+package fr.jaquin.bdlg.planner;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
+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.RestController;
|
|
|
+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.EvenementLob;
|
|
|
+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.Users;
|
|
|
+
|
|
|
+@RestController("/api")
|
|
|
+public class ApiController {
|
|
|
+
|
|
|
+ private final EvenementRepository repository;
|
|
|
+
|
|
|
+ private final EvenementLobRepository repositoryLob;
|
|
|
+
|
|
|
+ public ApiController(EvenementRepository repository, EvenementLobRepository repositoryLob) {
|
|
|
+ this.repository = repository;
|
|
|
+ this.repositoryLob = repositoryLob;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/evenements")
|
|
|
+ public List<Evenement> getEvenements() {
|
|
|
+ return (List<Evenement>) repository.findAll();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Single evenement data
|
|
|
+ @GetMapping("/evenements/{uuid}")
|
|
|
+ String one(@PathVariable String uuid) {
|
|
|
+ List<Evenement> allVersion = repository.findByUuid(uuid);
|
|
|
+ if (allVersion.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();
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/evenements/history/{uuid}")
|
|
|
+ List<Evenement> history(@PathVariable String uuid) {
|
|
|
+ return repository.findByUuid(uuid);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/evenements")
|
|
|
+ Evenement newEvenement(@AuthenticationPrincipal Users customUser,
|
|
|
+ @RequestBody EvenementData evt) {
|
|
|
+ if (repository.findByUuid(evt.getUuid()).size() > 0) {
|
|
|
+ throw new EvenementAlreadyExistException(evt.getUuid());
|
|
|
+ }
|
|
|
+ return saveEvenementData(evt, customUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PutMapping("/evenements/{uuid}")
|
|
|
+ Evenement newEvenement(@AuthenticationPrincipal Users customUser, @RequestBody EvenementData evt,
|
|
|
+ @PathVariable String id) {
|
|
|
+ if (repository.findByUuid(evt.getUuid()).size() == 0) {
|
|
|
+ throw new EvenementNotFoundException(evt.getUuid());
|
|
|
+ }
|
|
|
+ return saveEvenementData(evt, customUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Evenement saveEvenementData(EvenementData evt, Users customUser) {
|
|
|
+ Evenement newEvenement = new Evenement();
|
|
|
+ newEvenement.setName(evt.getName());
|
|
|
+ newEvenement.setUuid(evt.getUuid());
|
|
|
+ newEvenement.setLastModified(LocalDateTime.now());
|
|
|
+ newEvenement.setLastEditor(customUser);
|
|
|
+ newEvenement = repository.save(newEvenement);
|
|
|
+ EvenementLob blob = new EvenementLob();
|
|
|
+ blob.setEvenement(newEvenement);
|
|
|
+ blob.setJsonContent(evt.getContent());
|
|
|
+ repositoryLob.save(blob);
|
|
|
+ return newEvenement;
|
|
|
+ }
|
|
|
+}
|