diff --git a/pom.xml b/pom.xml index d3041c2..57344c9 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,14 @@ spring-boot-starter-webmvc-test test + + org.springframework.data + spring-data-jpa + + + org.springframework.boot + spring-boot-starter-data-jpa + diff --git a/src/main/java/de/exodus_hn/excalibur_tour/Utils.java b/src/main/java/de/exodus_hn/excalibur_tour/Utils.java new file mode 100644 index 0000000..04ad11a --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/Utils.java @@ -0,0 +1,20 @@ +package de.exodus_hn.excalibur_tour; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.TimeZone; + +public class Utils { + + public static ZoneId utc = TimeZone.getTimeZone(ZoneId.of("UTC")).toZoneId(); + + public static LocalDateTime getDateTimeNow() { + return LocalDateTime.now(utc); + } + + public static LocalDateTime parseLocalDateTime(String raw) { + if (raw == null) return null; + return LocalDateTime.parse(raw).atZone(utc).toLocalDateTime(); + } + +} 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 45a1608..d5f0072 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,5 +1,7 @@ package de.exodus_hn.excalibur_tour.controller; +import de.exodus_hn.excalibur_tour.entities.TourCode; +import de.exodus_hn.excalibur_tour.services.TourCodeService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -9,9 +11,16 @@ import org.springframework.web.bind.annotation.*; @RequiredArgsConstructor public class CodeController { + private final TourCodeService tourCodeService; + @GetMapping - public String greeting(@RequestParam(defaultValue = "World") String name) { - return "Hello " + name; + public String greeting(@RequestParam(defaultValue = "ABCDEFGHI") String code) { + TourCode t = tourCodeService.getTourCodeByCode(code); + if (t == null) { + t = tourCodeService.addTourCode(code); + return "Created TC " + String.valueOf(t.id); + } + return "Hello " + t.code + "(" + String.valueOf(t.activated) + ")"; } } diff --git a/src/main/java/de/exodus_hn/excalibur_tour/entities/TourCode.java b/src/main/java/de/exodus_hn/excalibur_tour/entities/TourCode.java new file mode 100644 index 0000000..a536a87 --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/entities/TourCode.java @@ -0,0 +1,45 @@ +package de.exodus_hn.excalibur_tour.entities; + +import de.exodus_hn.excalibur_tour.Utils; +import jakarta.persistence.*; +import lombok.*; +import org.hibernate.annotations.ColumnDefault; + +import java.time.LocalDateTime; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Entity +@Table(name = "TourCodes") +public class TourCode { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long id; + + @Column(nullable = false, unique = true) + public String code; + + @Column(nullable = false) + private LocalDateTime creationTimestamp; + + @Column(nullable = true) + private LocalDateTime activationTimestamp = null; + + @Column(nullable = true) + private LocalDateTime lastHeartbeatTimestamp = null; + + @Builder.Default + @Column(nullable = false) + public Boolean activated = true; + + public TourCode(String code) { + this.code = code; + this.creationTimestamp = Utils.getDateTimeNow(); + this.activated = true; + } + +} diff --git a/src/main/java/de/exodus_hn/excalibur_tour/repository/TourCodeRepository.java b/src/main/java/de/exodus_hn/excalibur_tour/repository/TourCodeRepository.java new file mode 100644 index 0000000..3b5ab63 --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/repository/TourCodeRepository.java @@ -0,0 +1,11 @@ +package de.exodus_hn.excalibur_tour.repository; + +import de.exodus_hn.excalibur_tour.entities.TourCode; +import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + +public interface TourCodeRepository extends JpaRepository { + + Optional findByCode(String code); + +} diff --git a/src/main/java/de/exodus_hn/excalibur_tour/services/TourCodeService.java b/src/main/java/de/exodus_hn/excalibur_tour/services/TourCodeService.java new file mode 100644 index 0000000..0d82e6e --- /dev/null +++ b/src/main/java/de/exodus_hn/excalibur_tour/services/TourCodeService.java @@ -0,0 +1,29 @@ +package de.exodus_hn.excalibur_tour.services; + +import de.exodus_hn.excalibur_tour.entities.TourCode; +import de.exodus_hn.excalibur_tour.repository.TourCodeRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class TourCodeService { + + private final TourCodeRepository tourCodeRepository; + + public TourCode getTourCodeByID(Long id) { + return tourCodeRepository.getReferenceById(id); + } + + public TourCode getTourCodeByCode(String code) { + return tourCodeRepository.findByCode(code).orElse(null); + } + + public TourCode addTourCode(String code) { + return tourCodeRepository.save(new TourCode(code)); + } + + public TourCode updateUser(TourCode tourCode) { + return tourCodeRepository.save(tourCode); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ef56d29..80b11c9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,9 @@ spring.application.name=excalibur-tour springdoc.api-docs.enabled=true -springdoc.swagger-ui.enabled=true \ No newline at end of file +springdoc.swagger-ui.enabled=true + +spring.datasource.url=jdbc:mariadb://${MYSQL_HOST:localhost}:3306/test +spring.datasource.username=root +spring.datasource.password=EinPasswort +spring.jpa.hibernate.ddl-auto=update +spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect \ No newline at end of file