TourSession init
This commit is contained in:
1 parent
7ac7bfa2a8
commit
1e129d815a
4 files changed
+95
No files matched your search
@@ -0,0 +1,49 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.controller;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.Utils;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.TourCode;
|
||||||
|
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.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
|
||||||
|
@CrossOrigin
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/sessions")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SessionController {
|
||||||
|
|
||||||
|
private final TourCodeService tourCodeService;
|
||||||
|
private final TourSessionService tourSessionService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getSession(@RequestParam(required = false) String code) {
|
||||||
|
if (code == null) {
|
||||||
|
return "No tour code given.";
|
||||||
|
}
|
||||||
|
TourSession session = tourSessionService.getSessionByTourCode(code);
|
||||||
|
if (session == null) {
|
||||||
|
System.out.println("Session not found for " + code);
|
||||||
|
return "Not available";
|
||||||
|
}
|
||||||
|
return String.format("%s:%s", session.getTourCode().getCode(), session.getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public String setSession(@RequestParam(required = true) String code, @RequestBody(required = true) String data) {
|
||||||
|
if (code == null) {
|
||||||
|
return "No tour code given.";
|
||||||
|
}
|
||||||
|
TourSession session = tourSessionService.getSessionByTourCode(code);
|
||||||
|
if (session == null) {
|
||||||
|
System.out.println("Session not found for " + code);
|
||||||
|
return "Not available";
|
||||||
|
}
|
||||||
|
return String.format("%s:%s", session.getTourCode().getCode(), session.getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -29,6 +29,11 @@ public class TourSession {
|
|||||||
@Column(name = "data", columnDefinition = "text")
|
@Column(name = "data", columnDefinition = "text")
|
||||||
private String data;
|
private String data;
|
||||||
|
|
||||||
|
public TourSession(String data, TourCode tourCode) {
|
||||||
|
this.data = data;
|
||||||
|
this.tourCode = tourCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*@JdbcTypeCode(SqlTypes.JSON)
|
/*@JdbcTypeCode(SqlTypes.JSON)
|
||||||
@Column(name = "data", columnDefinition = "jsonb")
|
@Column(name = "data", columnDefinition = "jsonb")
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.repository;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.TourSession;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface TourSessionRepository extends JpaRepository<TourSession, Long> {
|
||||||
|
|
||||||
|
@Query("SELECT s FROM TourSession s JOIN s.tourCode c WHERE c.code = ?1")
|
||||||
|
Optional<TourSession> findByCode(String code);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.services;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.TourCode;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.TourSession;
|
||||||
|
import de.exodus_hn.excalibur_tour.repository.TourCodeRepository;
|
||||||
|
import de.exodus_hn.excalibur_tour.repository.TourSessionRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TourSessionService {
|
||||||
|
|
||||||
|
private final TourSessionRepository tourSessionRepository;
|
||||||
|
|
||||||
|
public TourSession getSessionByTourCode(String code) {
|
||||||
|
return tourSessionRepository.findByCode(code).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TourSession addTourCode(String data, TourCode tourCode) {
|
||||||
|
return tourSessionRepository.save(new TourSession(data, tourCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
public TourSession updateCode(TourSession tourSession) {
|
||||||
|
return tourSessionRepository.save(tourSession);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user