diff --git a/src/main/java/org/springframework/samples/petclinic/records/RecordsTransferController.java b/src/main/java/org/springframework/samples/petclinic/records/RecordsTransferController.java index 307464b2a..289004f66 100644 --- a/src/main/java/org/springframework/samples/petclinic/records/RecordsTransferController.java +++ b/src/main/java/org/springframework/samples/petclinic/records/RecordsTransferController.java @@ -10,6 +10,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.UUID; +import java.util.zip.ZipInputStream; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -31,26 +32,14 @@ class RecordsTransferController { } @PostMapping("/records-transfers") - public NewRecordModel newRecordsTransfer(@RequestParam final String patientId, @RequestBody final InputStream body) - throws IOException, SQLException { - validatePatientId(patientId); - final var id = saveToRecordsSystem(body); - return new NewRecordModel(id); - } - - /** - * @param patientId id of the patient - * @throws SQLException when there is a problem with the database - * @throws IllegalArgumentException when the patient does not exist - */ - private void validatePatientId(final String patientId) throws SQLException { - final Connection connection = dataSource.getConnection(); - final PreparedStatement statement = connection.prepareStatement("SELECT * FROM patients WHERE id = ?"); - statement.setString(1, patientId); - final ResultSet rs = statement.executeQuery(); - if (!rs.next()) { - throw new IllegalArgumentException("Patient with id " + patientId + " does not exist"); + public NewRecordModel newRecordsTransfer(@RequestParam("zipped") boolean zipped, @RequestBody InputStream body) + throws IOException { + final InputStream is = zipped ? new ZipInputStream(body) : body; + final String id; + try (is) { + id = saveToRecordsSystem(is); } + return new NewRecordModel(id); } private String saveToRecordsSystem(final InputStream is) throws IOException {