mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-26 01:15:50 +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.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 {
|
||||
|
|
Loading…
Reference in a new issue