From 338714983db90015280a496bd78a3cf0aab05b77 Mon Sep 17 00:00:00 2001 From: LionTion Date: Wed, 14 Oct 2026 17:44:58 +0200 Subject: [PATCH] start stuff --- .../controller/CodeController.java | 35 +++++++++++++++---- .../session/GetSessionAndAllRiddleData.java | 28 +++++++++++++++ .../repository/TourSessionRepository.java | 3 ++ .../services/TourSessionService.java | 4 +++ 4 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/main/java/de/exodus_hn/excalibur_tour/dto/session/GetSessionAndAllRiddleData.java 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 4950692..473fd1d 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 @@ -1,9 +1,16 @@ package de.exodus_hn.excalibur_tour.controller; import de.exodus_hn.excalibur_tour.Utils; +import de.exodus_hn.excalibur_tour.dto.riddle.ReadAllRiddleData; +import de.exodus_hn.excalibur_tour.dto.session.GetSessionAndAllRiddleData; import de.exodus_hn.excalibur_tour.entities.TourCode; +import de.exodus_hn.excalibur_tour.entities.TourSession; +import de.exodus_hn.excalibur_tour.mapper.RiddleMapper; +import de.exodus_hn.excalibur_tour.services.RiddleService; import de.exodus_hn.excalibur_tour.services.TourCodeService; +import de.exodus_hn.excalibur_tour.services.TourSessionService; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; @@ -16,13 +23,20 @@ import java.time.temporal.ChronoUnit; public class CodeController { private final TourCodeService tourCodeService; + private final TourSessionService tourSessionService; + private final RiddleService riddleService; + private final RiddleMapper riddleMapper; @GetMapping - public String test(@RequestParam(defaultValue = "ABCDEFGHI") String code) { + public ResponseEntity check(@RequestParam() String code) { + if (code == null || code.length() < 9) { + return ResponseEntity.notFound().build(); + } TourCode t = tourCodeService.getTourCodeByCode(code); if (t == null) { t = tourCodeService.addTourCode(code); - return "Created TC " + t.getId(); + System.out.println("DEBUG: Created TC " + t.getId()); + return ResponseEntity.notFound().build(); } LocalDateTime now = Utils.getDateTimeNow(); if (t.getActivated()) { @@ -31,17 +45,26 @@ public class CodeController { t.setActivationTimestamp(now); tourCodeService.updateCode(t); } else if (activationTimestamp.plus(12, ChronoUnit.HOURS).isBefore(now)) { - return "Expired"; + return ResponseEntity.notFound().build(); } LocalDateTime heartbeat = t.getLastHeartbeatTimestamp(); if (heartbeat != null && heartbeat.plus(3, ChronoUnit.MINUTES).isAfter(now)) { - return "Expired (Wait a bit please)"; + return ResponseEntity.notFound().build(); } t.setLastHeartbeatTimestamp(now); tourCodeService.updateCode(t); - return "Have fun Ig"; + TourSession tourSession = tourSessionService.getSessionByTourCode(t); + return ResponseEntity.ok( + GetSessionAndAllRiddleData.builder() + .riddles(ReadAllRiddleData.builder() + .riddles(riddleService.getAllRiddles().stream() + .map(riddleMapper::riddleToReadRiddleData) + .toList()) + .build()) + .session(tourSession.getData()).build() + ); } - return "Expired (Disabled actually"; + return ResponseEntity.notFound().build(); } } \ No newline at end of file diff --git a/src/main/java/de/exodus_hn/excalibur_tour/dto/session/GetSessionAndAllRiddleData.java b/src/main/java/de/exodus_hn/excalibur_tour/dto/session/GetSessionAndAllRiddleData.java new file mode 100644 index 0000000..c4c3f2c --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/dto/session/GetSessionAndAllRiddleData.java @@ -0,0 +1,28 @@ +package de.exodus_hn.excalibur_tour.dto.session; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import de.exodus_hn.excalibur_tour.dto.riddle.ReadAllRiddleData; +import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData; +import jakarta.validation.constraints.NotNull; +import lombok.*; + +import java.util.List; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +public class GetSessionAndAllRiddleData { + + @NotNull + @JsonProperty(value = "riddles", required = true) + private ReadAllRiddleData riddles; + + @NotNull + @JsonProperty(value = "session", required = true) + private String session; + +} diff --git a/src/main/java/de/exodus_hn/excalibur_tour/repository/TourSessionRepository.java b/src/main/java/de/exodus_hn/excalibur_tour/repository/TourSessionRepository.java index abcf10c..f962164 100644 --- a/src/main/java/de/exodus_hn/excalibur_tour/repository/TourSessionRepository.java +++ b/src/main/java/de/exodus_hn/excalibur_tour/repository/TourSessionRepository.java @@ -11,4 +11,7 @@ public interface TourSessionRepository extends JpaRepository @Query("SELECT s FROM TourSession s JOIN s.tourCode c WHERE c.code = ?1") Optional findByCode(String code); + @Query("SELECT s FROM TourSession s JOIN s.tourCode c WHERE c.id = ?1") + Optional findByTourCodeId(Long id); + } 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 68ea4f5..deab936 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,6 +17,10 @@ public class TourSessionService { return tourSessionRepository.findByCode(code).orElse(null); } + public TourSession getSessionByTourCode(TourCode tourCode) { + return tourSessionRepository.findByTourCodeId(tourCode.getId()).orElse(null); + } + public TourSession addTourSession(String data, TourCode tourCode) { return tourSessionRepository.save(new TourSession(data, tourCode)); }