start stuff

This commit is contained in:
LionTion committed 2026-10-14 17:44:58 +02:00
1 parent 93b91c2c0f
commit 338714983d
4 files changed
+64 -6

No files matched your search

@@ -1,9 +1,16 @@
package de.exodus_hn.excalibur_tour.controller; package de.exodus_hn.excalibur_tour.controller;
import de.exodus_hn.excalibur_tour.Utils; 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.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.TourCodeService;
import de.exodus_hn.excalibur_tour.services.TourSessionService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
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;
@@ -16,13 +23,20 @@ import java.time.temporal.ChronoUnit;
public class CodeController { public class CodeController {
private final TourCodeService tourCodeService; private final TourCodeService tourCodeService;
private final TourSessionService tourSessionService;
private final RiddleService riddleService;
private final RiddleMapper riddleMapper;
@GetMapping @GetMapping
public String test(@RequestParam(defaultValue = "ABCDEFGHI") String code) { public ResponseEntity<GetSessionAndAllRiddleData> check(@RequestParam() String code) {
if (code == null || code.length() < 9) {
return ResponseEntity.notFound().build();
}
TourCode t = tourCodeService.getTourCodeByCode(code); TourCode t = tourCodeService.getTourCodeByCode(code);
if (t == null) { if (t == null) {
t = tourCodeService.addTourCode(code); 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(); LocalDateTime now = Utils.getDateTimeNow();
if (t.getActivated()) { if (t.getActivated()) {
@@ -31,17 +45,26 @@ public class CodeController {
t.setActivationTimestamp(now); t.setActivationTimestamp(now);
tourCodeService.updateCode(t); tourCodeService.updateCode(t);
} else if (activationTimestamp.plus(12, ChronoUnit.HOURS).isBefore(now)) { } else if (activationTimestamp.plus(12, ChronoUnit.HOURS).isBefore(now)) {
return "Expired"; return ResponseEntity.notFound().build();
} }
LocalDateTime heartbeat = t.getLastHeartbeatTimestamp(); LocalDateTime heartbeat = t.getLastHeartbeatTimestamp();
if (heartbeat != null && heartbeat.plus(3, ChronoUnit.MINUTES).isAfter(now)) { if (heartbeat != null && heartbeat.plus(3, ChronoUnit.MINUTES).isAfter(now)) {
return "Expired (Wait a bit please)"; return ResponseEntity.notFound().build();
} }
t.setLastHeartbeatTimestamp(now); t.setLastHeartbeatTimestamp(now);
tourCodeService.updateCode(t); 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();
} }
} }
@@ -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;
}
@@ -11,4 +11,7 @@ public interface TourSessionRepository extends JpaRepository<TourSession, Long>
@Query("SELECT s FROM TourSession s JOIN s.tourCode c WHERE c.code = ?1") @Query("SELECT s FROM TourSession s JOIN s.tourCode c WHERE c.code = ?1")
Optional<TourSession> findByCode(String code); Optional<TourSession> findByCode(String code);
@Query("SELECT s FROM TourSession s JOIN s.tourCode c WHERE c.id = ?1")
Optional<TourSession> findByTourCodeId(Long id);
} }
@@ -17,6 +17,10 @@ public class TourSessionService {
return tourSessionRepository.findByCode(code).orElse(null); 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) { public TourSession addTourSession(String data, TourCode tourCode) {
return tourSessionRepository.save(new TourSession(data, tourCode)); return tourSessionRepository.save(new TourSession(data, tourCode));
} }