TourSession additions

This commit is contained in:
LionTion committed 2026-10-14 15:42:59 +02:00
1 parent 1e129d815a
commit 93b91c2c0f
3 files changed
+24 -9

No files matched your search

@@ -30,11 +30,11 @@ public class CodeController {
if (activationTimestamp == null) { if (activationTimestamp == null) {
t.setActivationTimestamp(now); t.setActivationTimestamp(now);
tourCodeService.updateCode(t); tourCodeService.updateCode(t);
} else if (activationTimestamp.plus(10, ChronoUnit.SECONDS).isBefore(now)) { } else if (activationTimestamp.plus(12, ChronoUnit.HOURS).isBefore(now)) {
return "Expired"; return "Expired";
} }
LocalDateTime heartbeat = t.getLastHeartbeatTimestamp(); 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)"; return "Expired (Wait a bit please)";
} }
t.setLastHeartbeatTimestamp(now); t.setLastHeartbeatTimestamp(now);
@@ -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.TourCodeService;
import de.exodus_hn.excalibur_tour.services.TourSessionService; import de.exodus_hn.excalibur_tour.services.TourSessionService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.cglib.core.Local;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@@ -34,16 +36,29 @@ public class SessionController {
} }
@PostMapping @PostMapping
public String setSession(@RequestParam(required = true) String code, @RequestBody(required = true) String data) { public ResponseEntity<String> setSession(@RequestParam(required = true) String code, @RequestBody(required = true) String data) {
if (code == null) { 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); TourSession session = tourSessionService.getSessionByTourCode(code);
if (session == null) { if (session == null) {
System.out.println("Session not found for " + code); session = tourSessionService.addTourSession(data, tourCode);
return "Not available"; } 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());
} }
} }
@@ -17,11 +17,11 @@ public class TourSessionService {
return tourSessionRepository.findByCode(code).orElse(null); 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)); return tourSessionRepository.save(new TourSession(data, tourCode));
} }
public TourSession updateCode(TourSession tourSession) { public TourSession updateSession(TourSession tourSession) {
return tourSessionRepository.save(tourSession); return tourSessionRepository.save(tourSession);
} }
} }