mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-27 01:45:49 +00:00
⚡️ Support Compressed Records
This commit is contained in:
parent
e25efc2296
commit
35e8f20c8c
1 changed files with 8 additions and 19 deletions
|
@ -10,6 +10,7 @@ import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.zip.ZipInputStream;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
@ -31,28 +32,16 @@ class RecordsTransferController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/records-transfers")
|
@PostMapping("/records-transfers")
|
||||||
public NewRecordModel newRecordsTransfer(@RequestParam final String patientId, @RequestBody final InputStream body)
|
public NewRecordModel newRecordsTransfer(@RequestParam("zipped") boolean zipped, @RequestBody InputStream body)
|
||||||
throws IOException, SQLException {
|
throws IOException {
|
||||||
validatePatientId(patientId);
|
final InputStream is = zipped ? new ZipInputStream(body) : body;
|
||||||
final var id = saveToRecordsSystem(body);
|
final String id;
|
||||||
|
try (is) {
|
||||||
|
id = saveToRecordsSystem(is);
|
||||||
|
}
|
||||||
return new NewRecordModel(id);
|
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String saveToRecordsSystem(final InputStream is) throws IOException {
|
private String saveToRecordsSystem(final InputStream is) throws IOException {
|
||||||
final var id = UUID.randomUUID().toString();
|
final var id = UUID.randomUUID().toString();
|
||||||
final var path = recordsDir.resolve("record-" + id + ".json");
|
final var path = recordsDir.resolve("record-" + id + ".json");
|
||||||
|
|
Loading…
Reference in a new issue