Skip to content

Commit

Permalink
OH2-295 Fix Operation delete response code
Browse files Browse the repository at this point in the history
  • Loading branch information
mwithi committed Jun 20, 2024
1 parent 3cc17e0 commit c6dc49e
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions src/main/java/org/isf/operation/rest/OperationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ public class OperationController {

@Autowired
protected OperationBrowserManager operationManager;

@Autowired
protected AdmissionBrowserManager admissionManager;

@Autowired
protected OperationRowBrowserManager operationRowManager;

@Autowired
protected PatientBrowserManager patientBrowserManager;

@Autowired
protected OperationMapper mapper;

@Autowired
protected OpdMapper opdMapper;

@Autowired
protected OperationRowMapper opRowMapper;

Expand All @@ -95,6 +95,7 @@ public OperationController(OperationBrowserManager operationManager, OperationMa

/**
* Create a new {@link Operation}.
*
* @param operationDTO
* @return {@code true} if the operation has been stored, {@code false} otherwise.
* @throws OHServiceException
Expand All @@ -115,13 +116,14 @@ ResponseEntity<OperationDTO> newOperation(@RequestBody OperationDTO operationDTO

/**
* Updates the specified {@link Operation}.
*
* @param operationDTO
* @return {@code true} if the operation has been updated, {@code false} otherwise.
* @throws OHServiceException
*/
@PutMapping(value = "/operations/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<OperationDTO> updateOperation(@PathVariable String code, @RequestBody OperationDTO operationDTO)
throws OHServiceException {
throws OHServiceException {
LOGGER.info("Update operations code: {}.", operationDTO.getCode());
Operation operation = mapper.map2Model(operationDTO);
if (!operationManager.isCodePresent(code)) {
Expand All @@ -137,6 +139,7 @@ ResponseEntity<OperationDTO> updateOperation(@PathVariable String code, @Request

/**
* Get all the available {@link Operation}s.
*
* @return a {@link List} of {@link Operation} or NO_CONTENT if there is no data found.
* @throws OHServiceException
*/
Expand All @@ -151,9 +154,10 @@ public ResponseEntity<List<OperationDTO>> getOperations() throws OHServiceExcept
return ResponseEntity.ok(operationDTOs);
}
}

/**
* Get the {@link Operation} with the specified code.
*
* @return found operation
* @throws OHServiceException
*/
Expand All @@ -167,9 +171,10 @@ public ResponseEntity<OperationDTO> getOperationByCode(@PathVariable String code
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
}
}

/**
* Get all {@link Operation}s whose {@link OperationType}'s description matches specified string.
*
* @return {@link List} of {@link Operation} or NO_CONTENT if there is no data found.
* @throws OHServiceException
*/
Expand All @@ -187,6 +192,7 @@ public ResponseEntity<List<OperationDTO>> getOperationByTypeDescription(@Request

/**
* Delete {@link Operation} for specified code.
*
* @param code
* @return {@code true} if the {@link Operation} has been deleted, {@code false} otherwise.
* @throws OHServiceException
Expand All @@ -195,20 +201,21 @@ public ResponseEntity<List<OperationDTO>> getOperationByTypeDescription(@Request
public ResponseEntity<Boolean> deleteOperation(@PathVariable("code") String code) throws OHServiceException {
LOGGER.info("Delete operation code: {}.", code);
Operation operation = operationManager.getOperationByCode(code);

if (operation == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
try {
operationManager.deleteOperation(operation);
return ResponseEntity.ok(true);
} catch (OHServiceException serviceException) {
return ResponseEntity.ok(false);
LOGGER.error("Delete Operation: {} failed.", code);
throw new OHAPIException(new OHExceptionMessage("Operation not deleted."));
}
return ResponseEntity.ok(true);
}

/**
* Create a new {@link OperationRow}.
*
* @param operationRowDTO
* @return {@code true} if the operation has been stored, {@code false} otherwise.
* @throws OHServiceException
Expand All @@ -218,24 +225,25 @@ ResponseEntity<OperationRowDTO> newOperationRow(@RequestBody OperationRowDTO ope
int code = operationRowDTO.getAdmission().getId();
LOGGER.info("Create operation: {}.", code);
if (operationRowDTO.getAdmission() == null && operationRowDTO.getOpd() == null) {
throw new OHAPIException(new OHExceptionMessage("At least one field between admission and Opd is required."));
throw new OHAPIException(new OHExceptionMessage("At least one field between admission and Opd is required."));
}
OperationRow opRow = opRowMapper.map2Model(operationRowDTO);

OperationRow createOpeRow = operationRowManager.newOperationRow(opRow);
List<OperationRow> opRowFounds = operationRowManager.getOperationRowByAdmission(opRow.getAdmission()).stream().filter(op -> op.getAdmission().getId() == code)
.collect(Collectors.toList());
List<OperationRow> opRowFounds = operationRowManager.getOperationRowByAdmission(opRow.getAdmission()).stream()
.filter(op -> op.getAdmission().getId() == code)
.collect(Collectors.toList());
OperationRow opCreated = null;
if (!opRowFounds.isEmpty()) {
opCreated = opRowFounds.get(0);
}
if (createOpeRow == null || opCreated == null) {
throw new OHAPIException(new OHExceptionMessage("Operation row not created."));
}
OperationRowDTO opR = opRowMapper.map2DTO(opCreated);
OperationRowDTO opR = opRowMapper.map2DTO(opCreated);
return ResponseEntity.status(HttpStatus.CREATED).body(opR);
}

/**
* Updates the specified {@link OperationRow}.
*
Expand All @@ -262,9 +270,10 @@ ResponseEntity<Integer> updateOperationRow(@RequestBody OperationRowDTO operatio
}
return ResponseEntity.ok(opRow.getId());
}

/**
* Get {@link OperationRow}s for specified admission.
*
* @return {@link List} of {@link OperationRow} or NO_CONTENT if there is no data found.
* @throws OHServiceException
*/
Expand All @@ -285,6 +294,7 @@ public ResponseEntity<List<OperationRowDTO>> getOperationRowsByAdmt(@RequestPara

/**
* Get {@link OperationRow}s for specified patient.
*
* @return {@link List} of {@link OperationRow} or NO_CONTENT if there is no data found.
* @throws OHServiceException
*/
Expand All @@ -300,9 +310,10 @@ public ResponseEntity<List<OperationRowDTO>> getOperationRowsByPatient(@RequestP
return ResponseEntity.ok(operationRowDTOs);
}
}

/**
* Get {@link OperationRow}s for specified {@link OpdDTO}.
*
* @return {@link List} of {@link OperationRow} or NO_CONTENT if there is no data found.
* @throws OHServiceException
*/
Expand All @@ -311,16 +322,17 @@ public ResponseEntity<List<OperationRowDTO>> getOperationRowsByOpd(@RequestBody
LOGGER.info("Get operations row for provided opd.");
List<OperationRow> operationRows = operationRowManager.getOperationRowByOpd(opdMapper.map2Model(opdDTO));
List<OperationRowDTO> operationRowDTOs = operationRows.stream().map(operation -> opRowMapper.map2DTO(operation)).collect(Collectors.toList());

if (operationRowDTOs.isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(operationRowDTOs);
} else {
return ResponseEntity.ok(operationRowDTOs);
}
}

/**
* Delete the {@link OperationRow} with the specified code.
*
* @param code
* @return {@code true} if the {@link OperationRow} has been deleted, {@code false} otherwise.
* @throws OHServiceException
Expand Down

0 comments on commit c6dc49e

Please sign in to comment.