From 4c8ab56723f040a1a8b6f19b44220c7e4d4e7d94 Mon Sep 17 00:00:00 2001 From: Mohamed Bilel Besrour Date: Mon, 5 Aug 2024 02:17:34 +0200 Subject: [PATCH] Add timestamp and save every request --- .../ase/artemistelemetry/domain/Telemetry.java | 15 ++++++++++++++- .../service/TelemetryService.java | 7 +++++++ .../service/dto/TelemetryDTO.java | 6 ++++-- .../web/rest/TelemetryResource.java | 3 +-- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/tum/cit/ase/artemistelemetry/domain/Telemetry.java b/src/main/java/de/tum/cit/ase/artemistelemetry/domain/Telemetry.java index 015af74..aa91506 100644 --- a/src/main/java/de/tum/cit/ase/artemistelemetry/domain/Telemetry.java +++ b/src/main/java/de/tum/cit/ase/artemistelemetry/domain/Telemetry.java @@ -4,6 +4,8 @@ import jakarta.persistence.Entity; import jakarta.persistence.Table; +import java.time.ZonedDateTime; + @Entity @Table(name = "telemetry") public class Telemetry extends DomainObject { @@ -14,7 +16,7 @@ public class Telemetry extends DomainObject { @Column(name = "version") private String version; - @Column(name = "university_name", unique = true) + @Column(name = "university_name") private String universityName; @Column(name = "admin_name") @@ -23,6 +25,9 @@ public class Telemetry extends DomainObject { @Column(name = "profiles") private String profiles; + @Column(name = "timestamp") + private ZonedDateTime timestamp; + public String getProfiles() { return profiles; } @@ -62,4 +67,12 @@ public String getServerUrl() { public void setServerUrl(String serverUrl) { this.serverUrl = serverUrl; } + + public ZonedDateTime getTimestamp() { + return timestamp; + } + + public void setTimestamp(ZonedDateTime timestamp) { + this.timestamp = timestamp; + } } diff --git a/src/main/java/de/tum/cit/ase/artemistelemetry/service/TelemetryService.java b/src/main/java/de/tum/cit/ase/artemistelemetry/service/TelemetryService.java index bfc8416..cca777e 100644 --- a/src/main/java/de/tum/cit/ase/artemistelemetry/service/TelemetryService.java +++ b/src/main/java/de/tum/cit/ase/artemistelemetry/service/TelemetryService.java @@ -4,6 +4,7 @@ import de.tum.cit.ase.artemistelemetry.repository.TelemetryRepository; import org.springframework.stereotype.Service; +import java.time.ZonedDateTime; import java.util.List; import java.util.stream.StreamSupport; @@ -16,6 +17,12 @@ public TelemetryService(TelemetryRepository telemetryRepository) { this.telemetryRepository = telemetryRepository; } + public Telemetry saveNewTelemetry(Telemetry telemetry) { + telemetry.setId(null); + telemetry.setTimestamp(ZonedDateTime.now()); + return telemetryRepository.save(telemetry); + } + public Telemetry save(Telemetry telemetry) { return telemetryRepository.save(telemetry); } diff --git a/src/main/java/de/tum/cit/ase/artemistelemetry/service/dto/TelemetryDTO.java b/src/main/java/de/tum/cit/ase/artemistelemetry/service/dto/TelemetryDTO.java index 98f9bf4..5e58f30 100644 --- a/src/main/java/de/tum/cit/ase/artemistelemetry/service/dto/TelemetryDTO.java +++ b/src/main/java/de/tum/cit/ase/artemistelemetry/service/dto/TelemetryDTO.java @@ -3,14 +3,15 @@ import com.fasterxml.jackson.annotation.JsonInclude; import de.tum.cit.ase.artemistelemetry.domain.Telemetry; +import java.time.ZonedDateTime; import java.util.List; @JsonInclude(JsonInclude.Include.NON_EMPTY) -public record TelemetryDTO(Long id, String version, String serverUrl, String universityName, String mainAdminName, List profiles) { +public record TelemetryDTO(Long id, String version, String serverUrl, String universityName, String mainAdminName, List profiles, ZonedDateTime timestamp) { public static TelemetryDTO from(Telemetry telemetry) { List profilesList = List.of(telemetry.getProfiles().split(",")); - return new TelemetryDTO(telemetry.getId(), telemetry.getVersion(), telemetry.getServerUrl(), telemetry.getUniversityName(), telemetry.getMainAdminName(), profilesList); + return new TelemetryDTO(telemetry.getId(), telemetry.getVersion(), telemetry.getServerUrl(), telemetry.getUniversityName(), telemetry.getMainAdminName(), profilesList, telemetry.getTimestamp()); } public static Telemetry to(TelemetryDTO telemetryDTO) { @@ -22,6 +23,7 @@ public static Telemetry to(TelemetryDTO telemetryDTO) { telemetry.setUniversityName(telemetryDTO.universityName()); telemetry.setMainAdminName(telemetryDTO.mainAdminName()); telemetry.setProfiles(profiles); + telemetry.setTimestamp(telemetryDTO.timestamp()); return telemetry; } } diff --git a/src/main/java/de/tum/cit/ase/artemistelemetry/web/rest/TelemetryResource.java b/src/main/java/de/tum/cit/ase/artemistelemetry/web/rest/TelemetryResource.java index 0b7644d..7fbdb75 100644 --- a/src/main/java/de/tum/cit/ase/artemistelemetry/web/rest/TelemetryResource.java +++ b/src/main/java/de/tum/cit/ase/artemistelemetry/web/rest/TelemetryResource.java @@ -25,8 +25,7 @@ public ResponseEntity getTelemetry(@PathVariable Long id) { @PostMapping public ResponseEntity postTelemetry(@RequestBody TelemetryDTO telemetryDTO) { - Telemetry savedTelemetry; - savedTelemetry = telemetryService.updateTelemetryByUniversityName(TelemetryDTO.to(telemetryDTO)); + Telemetry savedTelemetry = telemetryService.saveNewTelemetry(TelemetryDTO.to(telemetryDTO)); return ResponseEntity.ok(TelemetryDTO.from(savedTelemetry)); }