Skip to content

Commit

Permalink
Add timestamp and save every request
Browse files Browse the repository at this point in the history
  • Loading branch information
BBesrour committed Aug 5, 2024
1 parent 2be9d67 commit 4c8ab56
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> profiles) {
public record TelemetryDTO(Long id, String version, String serverUrl, String universityName, String mainAdminName, List<String> profiles, ZonedDateTime timestamp) {

public static TelemetryDTO from(Telemetry telemetry) {
List<String> 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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public ResponseEntity<TelemetryDTO> getTelemetry(@PathVariable Long id) {

@PostMapping
public ResponseEntity<TelemetryDTO> 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));
}

Expand Down

0 comments on commit 4c8ab56

Please sign in to comment.