DB Preparations
This commit is contained in:
1 parent
29dce6ed0f
commit
2419085684
7 files changed
+131
-3
No files matched your search
@@ -69,6 +69,14 @@
|
|||||||
<artifactId>spring-boot-starter-webmvc-test</artifactId>
|
<artifactId>spring-boot-starter-webmvc-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package de.exodus_hn.excalibur_tour.controller;
|
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 lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -9,9 +11,16 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class CodeController {
|
public class CodeController {
|
||||||
|
|
||||||
|
private final TourCodeService tourCodeService;
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public String greeting(@RequestParam(defaultValue = "World") String name) {
|
public String greeting(@RequestParam(defaultValue = "ABCDEFGHI") String code) {
|
||||||
return "Hello " + name;
|
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) + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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<TourCode, Long> {
|
||||||
|
|
||||||
|
Optional<TourCode> findByCode(String code);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,9 @@
|
|||||||
spring.application.name=excalibur-tour
|
spring.application.name=excalibur-tour
|
||||||
springdoc.api-docs.enabled=true
|
springdoc.api-docs.enabled=true
|
||||||
springdoc.swagger-ui.enabled=true
|
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
|
||||||
Reference in new issue
Block a user