Skip to content

Commit

Permalink
add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
BBesrour committed Sep 7, 2024
1 parent 306e346 commit fb6a128
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.tum.cit.ase.artemistelemetry.domain.Telemetry;
import de.tum.cit.ase.artemistelemetry.repository.TelemetryRepository;
import de.tum.cit.ase.artemistelemetry.service.dto.TelemetryDTO;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
Expand Down Expand Up @@ -48,4 +49,19 @@ public Telemetry updateTelemetryByUniversityName(Telemetry newTelemetry) {
newTelemetry.setId(telemetry.getId());
return telemetryRepository.save(newTelemetry);
}

public void validateTelemetry(TelemetryDTO telemetry) throws IllegalArgumentException {
if (telemetry.operator() == null || telemetry.operator().isEmpty()) {
throw new IllegalArgumentException("University name must not be empty");
}
if (telemetry.profiles() == null || telemetry.profiles().isEmpty()) {
throw new IllegalArgumentException("Profiles must not be empty");
}
if (telemetry.serverUrl() == null || telemetry.serverUrl().isEmpty()) {
throw new IllegalArgumentException("Server URL must not be empty");
}
if (telemetry.version() == null || telemetry.version().isEmpty()) {
throw new IllegalArgumentException("Version must not be empty");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public ResponseEntity<TelemetryDTO> getTelemetry(@PathVariable Long id) {
}

@PostMapping
public ResponseEntity<TelemetryDTO> postTelemetry(@RequestBody TelemetryDTO telemetryDTO) {
public ResponseEntity<?> createTelemetry(@RequestBody TelemetryDTO telemetryDTO) {
try {
telemetryService.validateTelemetry(telemetryDTO);
} catch (IllegalArgumentException e) {
return ResponseEntity.status(400).body(e.getMessage());
}
Telemetry savedTelemetry = telemetryService.saveNewTelemetry(TelemetryDTO.to(telemetryDTO));
return ResponseEntity.ok(TelemetryDTO.from(savedTelemetry));
}
Expand Down

0 comments on commit fb6a128

Please sign in to comment.