From 93b91c2c0f00b4cfa807013b6c48e30a165401c4 Mon Sep 17 00:00:00 2001 From: LionTion Date: Wed, 14 Oct 2026 15:42:59 +0200 Subject: [PATCH] TourSession additions --- .../controller/CodeController.java | 4 +-- .../controller/SessionController.java | 25 +++++++++++++++---- .../services/TourSessionService.java | 4 +-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/exodus_hn/excalibur_tour/controller/CodeController.java b/src/main/java/de/exodus_hn/excalibur_tour/controller/CodeController.java index b9153b0..4950692 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/controller/CodeController.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/controller/CodeController.java @@ -30,11 +30,11 @@ public class CodeController { if (activationTimestamp == null) { t.setActivationTimestamp(now); tourCodeService.updateCode(t); - } else if (activationTimestamp.plus(10, ChronoUnit.SECONDS).isBefore(now)) { + } else if (activationTimestamp.plus(12, ChronoUnit.HOURS).isBefore(now)) { return "Expired"; } LocalDateTime heartbeat = t.getLastHeartbeatTimestamp(); - if (heartbeat != null && heartbeat.plus(2, ChronoUnit.SECONDS).isAfter(now)) { + if (heartbeat != null && heartbeat.plus(3, ChronoUnit.MINUTES).isAfter(now)) { return "Expired (Wait a bit please)"; } t.setLastHeartbeatTimestamp(now); diff --git a/src/main/java/de/exodus_hn/excalibur_tour/controller/SessionController.java b/src/main/java/de/exodus_hn/excalibur_tour/controller/SessionController.java index 13207f8..8b07765 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/controller/SessionController.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/controller/SessionController.java @@ -6,6 +6,8 @@ import de.exodus_hn.excalibur_tour.entities.TourSession; import de.exodus_hn.excalibur_tour.services.TourCodeService; import de.exodus_hn.excalibur_tour.services.TourSessionService; import lombok.RequiredArgsConstructor; +import org.springframework.cglib.core.Local; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; @@ -34,16 +36,29 @@ public class SessionController { } @PostMapping - public String setSession(@RequestParam(required = true) String code, @RequestBody(required = true) String data) { + public ResponseEntity setSession(@RequestParam(required = true) String code, @RequestBody(required = true) String data) { if (code == null) { - return "No tour code given."; + return ResponseEntity.badRequest().body("No Code"); + } + TourCode tourCode = tourCodeService.getTourCodeByCode(code); + if (tourCode == null) { + return ResponseEntity.notFound().build(); + } + LocalDateTime now = Utils.getDateTimeNow(); + LocalDateTime heartbeat = tourCode.getLastHeartbeatTimestamp(); + if (heartbeat != null && heartbeat.plus(3, ChronoUnit.MINUTES).isBefore(now)) { + return ResponseEntity.badRequest().body("Code invalid"); } TourSession session = tourSessionService.getSessionByTourCode(code); if (session == null) { - System.out.println("Session not found for " + code); - return "Not available"; + session = tourSessionService.addTourSession(data, tourCode); + } else { + session.setData(data); + session = tourSessionService.updateSession(session); } - return String.format("%s:%s", session.getTourCode().getCode(), session.getData()); + tourCode.setLastHeartbeatTimestamp(now); + tourCodeService.updateCode(tourCode); + return ResponseEntity.ok(session.getId().toString()); } } \ No newline at end of file diff --git a/src/main/java/de/exodus_hn/excalibur_tour/services/TourSessionService.java b/src/main/java/de/exodus_hn/excalibur_tour/services/TourSessionService.java index f3e76c9..68ea4f5 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/services/TourSessionService.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/services/TourSessionService.java @@ -17,11 +17,11 @@ public class TourSessionService { return tourSessionRepository.findByCode(code).orElse(null); } - public TourSession addTourCode(String data, TourCode tourCode) { + public TourSession addTourSession(String data, TourCode tourCode) { return tourSessionRepository.save(new TourSession(data, tourCode)); } - public TourSession updateCode(TourSession tourSession) { + public TourSession updateSession(TourSession tourSession) { return tourSessionRepository.save(tourSession); } }