Skip to content

Commit

Permalink
OH2-400 | OH2-403 Refactoring ResponseEntity (#482)
Browse files Browse the repository at this point in the history
* chore:refactor BillController

* chore:refactor AdmissionController

* chore:use full path for endpoints

* chore:refactor AdmissionTypeController

* chore:refactor AgeTypeController

* chore:refactor DischargeTypeController

* chore:refactor DiseaseController

* chore:refactor DiseaseTypeController

* chore:refactor DeliveryResultTypeController

* chore:refactor DeliveryResultController

* chore:refactor ExamController and ExamRowController

* chore:refactor ExaminationController

* chore:refactor ExamTypeController

* chore:refactor HospitalController

* chore:refactor LaboratoryController

* chore:refactor LoginController

* chore:refactor MedicalController

* chore:refactor MedicalStockMovementController and StockMovementController

* chore:refactor MedicalStockWardController and MedStockMovementTypeController

* chore:refactor MedicalTypeController and OpdController

* chore:refactor OperationController

* chore:refactor OperationTypeController and PatientController

* chore:refactor PregnantTreatmentTypeController and PermisionsController

* chore:refactor SMS, PriceList, PriceOthers and Reports controllers

* chore:refactor Supplier, Therapy and UserGroup controllers

* chore:refactor Vists, Vaccine, VaccineType, Ward and UserSetting controllers

* chore:update spec file

* chore:remove extra produces

* chore:remove responseentity in controller

* fix:fix tests

* fix:fix API spec file

* fix:fix API spec file
  • Loading branch information
SilverD3 authored Nov 8, 2024
1 parent a951404 commit 4ffa8ee
Show file tree
Hide file tree
Showing 65 changed files with 4,118 additions and 5,219 deletions.
1,162 changes: 95 additions & 1,067 deletions openapi/oh.yaml

Large diffs are not rendered by default.

256 changes: 104 additions & 152 deletions src/main/java/org/isf/accounting/rest/BillController.java

Large diffs are not rendered by default.

501 changes: 247 additions & 254 deletions src/main/java/org/isf/admission/rest/AdmissionController.java

Large diffs are not rendered by default.

120 changes: 61 additions & 59 deletions src/main/java/org/isf/admtype/rest/AdmissionTypeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package org.isf.admtype.rest;

import java.util.List;
import java.util.stream.Collectors;

import org.isf.admtype.dto.AdmissionTypeDTO;
import org.isf.admtype.manager.AdmissionTypeBrowserManager;
Expand All @@ -33,120 +32,123 @@
import org.isf.utils.exception.model.OHExceptionMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController(value = "/admissiontypes")
@RestController()
@Tag(name = "AdmissionTypes")
@SecurityRequirement(name = "bearerAuth")
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class AdmissionTypeController {

private static final Logger LOGGER = LoggerFactory.getLogger(AdmissionTypeController.class);

@Autowired
protected AdmissionTypeBrowserManager admtManager;
private final AdmissionTypeBrowserManager admissionTypeManager;

@Autowired
protected AdmissionTypeMapper mapper;
private final AdmissionTypeMapper mapper;

public AdmissionTypeController(AdmissionTypeBrowserManager admtManager, AdmissionTypeMapper admissionTypemapper) {
this.admtManager = admtManager;
public AdmissionTypeController(
AdmissionTypeBrowserManager admissionTypeBrowserManager, AdmissionTypeMapper admissionTypemapper
) {
this.admissionTypeManager = admissionTypeBrowserManager;
this.mapper = admissionTypemapper;
}

/**
* Create a new {@link AdmissionType}
*
* @param admissionTypeDTO
*
* @param admissionTypeDTO Admission Type payload
* @return {@code true} if the admission type has been stored, {@code false} otherwise.
* @throws OHServiceException
* @throws OHServiceException When failed to create admission type
*/
@PostMapping(value = "/admissiontypes", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<AdmissionTypeDTO> newAdmissionType(@RequestBody AdmissionTypeDTO admissionTypeDTO) throws OHServiceException {
@PostMapping(value = "/admissiontypes")
@ResponseStatus(HttpStatus.CREATED)
public AdmissionTypeDTO newAdmissionType(@RequestBody AdmissionTypeDTO admissionTypeDTO) throws OHServiceException {
String code = admissionTypeDTO.getCode();

LOGGER.info("Create Admission Type {}", code);
AdmissionType newAdmissionType = admtManager.newAdmissionType(mapper.map2Model(admissionTypeDTO));
if (!admtManager.isCodePresent(code)) {

AdmissionType newAdmissionType = admissionTypeManager.newAdmissionType(mapper.map2Model(admissionTypeDTO));
if (!admissionTypeManager.isCodePresent(code)) {
throw new OHAPIException(new OHExceptionMessage("Admission Type is not created."), HttpStatus.INTERNAL_SERVER_ERROR);
}
return ResponseEntity.status(HttpStatus.CREATED).body(mapper.map2DTO(newAdmissionType));

return mapper.map2DTO(newAdmissionType);
}

/**
* Updates the specified {@link AdmissionType}.
*
* @param admissionTypeDTO
*
* @param admissionTypeDTO Admission Type payload
* @return {@code true} if the admission type has been updated, {@code false} otherwise.
* @throws OHServiceException
* @throws OHServiceException When failed to update the admission type
*/
@PutMapping(value = "/admissiontypes", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<AdmissionTypeDTO> updateAdmissionTypes(@RequestBody AdmissionTypeDTO admissionTypeDTO)
throws OHServiceException {
LOGGER.info("Update admissiontypes code: {}", admissionTypeDTO.getCode());
AdmissionType admt = mapper.map2Model(admissionTypeDTO);
if (!admtManager.isCodePresent(admt.getCode())) {
@PutMapping(value = "/admissiontypes")
public AdmissionTypeDTO updateAdmissionTypes(
@RequestBody AdmissionTypeDTO admissionTypeDTO
) throws OHServiceException {
LOGGER.info("Update admission types code: {}", admissionTypeDTO.getCode());

AdmissionType admissionType = mapper.map2Model(admissionTypeDTO);
if (!admissionTypeManager.isCodePresent(admissionType.getCode())) {
throw new OHAPIException(new OHExceptionMessage("Admission Type not found."));
}
AdmissionType updatedAdmissionType = admtManager.updateAdmissionType(admt);
return ResponseEntity.ok(mapper.map2DTO(updatedAdmissionType));

return mapper.map2DTO(admissionTypeManager.updateAdmissionType(admissionType));
}

/**
* Get all the available {@link AdmissionType}s.
*
*
* @return a {@link List} of {@link AdmissionType} or NO_CONTENT if there is no data found.
* @throws OHServiceException
* @throws OHServiceException When failed to get admission types
*/
@GetMapping(value = "/admissiontypes", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<AdmissionTypeDTO>> getAdmissionTypes() throws OHServiceException {
@GetMapping(value = "/admissiontypes")
public List<AdmissionTypeDTO> getAdmissionTypes() throws OHServiceException {
LOGGER.info("Get all Admission Types ");
List<AdmissionType> admissionTypes = admtManager.getAdmissionType();
List<AdmissionTypeDTO> admissionTypeDTOs = mapper.map2DTOList(admissionTypes);
if (admissionTypeDTOs.isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(admissionTypeDTOs);
} else {
return ResponseEntity.ok(admissionTypeDTOs);
}

return mapper.map2DTOList(admissionTypeManager.getAdmissionType());
}

/**
* Delete {@link AdmissionType} for specified code.
*
* @param code
*
* @param code Admission Type Code
* @return {@code true} if the {@link AdmissionType} has been deleted, {@code false} otherwise.
* @throws OHServiceException
* @throws OHServiceException When failed to delete admission type
*/
@DeleteMapping(value = "/admissiontypes/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> deleteAdmissionType(@PathVariable("code") String code) throws OHServiceException {
public boolean deleteAdmissionType(@PathVariable("code") String code) throws OHServiceException {
LOGGER.info("Delete Admission Type code: {}", code);
if (admtManager.isCodePresent(code)) {
List<AdmissionType> admissionTypes = admtManager.getAdmissionType();
List<AdmissionType> admtFounds = admissionTypes.stream().filter(ad -> ad.getCode().equals(code))
.collect(Collectors.toList());
if (!admtFounds.isEmpty()) {
try {
admtManager.deleteAdmissionType(admtFounds.get(0));
} catch (OHServiceException serviceException) {
LOGGER.error("Delete Admission: {} failed.", code);
throw new OHAPIException(new OHExceptionMessage("Admission not deleted."));
}

if (!admissionTypeManager.isCodePresent(code)) {
throw new OHAPIException(new OHExceptionMessage("Admission type not found with ID :" + code), HttpStatus.NOT_FOUND);
}

List<AdmissionType> admissionTypes = admissionTypeManager.getAdmissionType();
List<AdmissionType> admissionTypesFound = admissionTypes.stream().filter(ad -> ad.getCode().equals(code)).toList();

if (!admissionTypesFound.isEmpty()) {
try {
admissionTypeManager.deleteAdmissionType(admissionTypesFound.get(0));
} catch (OHServiceException serviceException) {
LOGGER.error("Delete Admission: {} failed.", code);
throw new OHAPIException(new OHExceptionMessage("Admission not deleted."));
}
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
return ResponseEntity.ok(true);
}

return true;
}
}
90 changes: 44 additions & 46 deletions src/main/java/org/isf/agetype/rest/AgeTypeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,115 +37,113 @@
import org.isf.utils.exception.model.OHExceptionMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController(value = "/agetypes")
@RestController
@Tag(name = "AgeTypes")
@SecurityRequirement(name = "bearerAuth")
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class AgeTypeController {

private static final Logger LOGGER = LoggerFactory.getLogger(AgeTypeController.class);

@Autowired
private AgeTypeBrowserManager ageTypeManager;

@Autowired
private AgeTypeMapper mapper;

private final AgeTypeBrowserManager ageTypeManager;

private final AgeTypeMapper mapper;

public AgeTypeController(AgeTypeBrowserManager ageTypeManager, AgeTypeMapper ageTypeMapper) {
this.ageTypeManager = ageTypeManager;
this.mapper = ageTypeMapper;
}

/**
* Get all the age types stored
*
* @return the list of age types found
* @throws OHServiceException
* @throws OHServiceException When failed to get age types
*/
@GetMapping(value = "/agetypes", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<AgeTypeDTO>> getAllAgeTypes() throws OHServiceException {
@GetMapping(value = "/agetypes")
public List<AgeTypeDTO> getAllAgeTypes() throws OHServiceException {
LOGGER.info("Get age types");
List<AgeType> results = ageTypeManager.getAgeType();
List<AgeTypeDTO> parsedResults = mapper.map2DTOList(results);
if (!parsedResults.isEmpty()) {
return ResponseEntity.ok(parsedResults);
} else {
LOGGER.info("Empty age types list");
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(parsedResults);
}

return mapper.map2DTOList(ageTypeManager.getAgeType());
}

/**
* Update an age type
* @param ageTypeDTO - the age type to be updated
* @return {@link AgeTypeDTO} the updated age type
* @throws OHServiceException
* @throws OHServiceException When failed to update age type
*/
@PutMapping(value = "/agetypes", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<AgeTypeDTO> updateAgeType(@Valid @RequestBody AgeTypeDTO ageTypeDTO) throws OHServiceException {
@PutMapping(value = "/agetypes")
public AgeTypeDTO updateAgeType(@Valid @RequestBody AgeTypeDTO ageTypeDTO) throws OHServiceException {
if (ageTypeDTO.getCode() == null || ageTypeDTO.getCode().trim().isEmpty()) {
throw new OHAPIException(new OHExceptionMessage("The age type is not valid."));
}

LOGGER.info("Update age type");

AgeType ageType = mapper.map2Model(ageTypeDTO);
List<AgeType> ageTypes = new ArrayList<>();
ageTypes.add(ageType);

try {
ageTypeManager.updateAgeType(ageTypes);
return ResponseEntity.ok(ageTypeDTO);
return ageTypeDTO;
} catch (OHServiceException ex) {
throw new OHAPIException(new OHExceptionMessage("The age type is not updated."), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

/**
* Get the code of an age type whose ages range includes a given age
* @param age - the given age
* @return the code of the age type matching the given age
* @throws OHServiceException
* @throws OHServiceException When failed to get age type
*/
@GetMapping(value = "/agetypes/code", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> getAgeTypeCodeByAge(@RequestParam("age") int age) throws OHServiceException {
@GetMapping(value = "/agetypes/code")
public Map<String, String> getAgeTypeCodeByAge(@RequestParam("age") int age) throws OHServiceException {
LOGGER.info("Get age type by age: {}", age);

String result = ageTypeManager.getTypeByAge(age);
Map<String, String> responseBody = new HashMap<>();

if (result != null){
responseBody.put("code", result);
return ResponseEntity.ok(responseBody);
} else {
LOGGER.info("No corresponding age code for the given age");
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(responseBody);
}
} else {
LOGGER.info("No corresponding age code for the given age");
}

return responseBody;
}

/**
* Gets the {@link AgeType} from the code index.
* @param index the code index.
* @return the retrieved element.
* @throws OHServiceException
* @throws OHServiceException When failed to get age type
*/
@GetMapping(value = "/agetypes/{index}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<AgeTypeDTO> getAgeTypeByIndex(@PathVariable int index) throws OHServiceException {
@GetMapping(value = "/agetypes/{index}")
public AgeTypeDTO getAgeTypeByIndex(@PathVariable int index) throws OHServiceException {
LOGGER.info("Get age type by index: {}", index);
AgeType result = ageTypeManager.getTypeByCode(index);
if (result != null){
return ResponseEntity.ok(mapper.map2DTO(result));
} else {
LOGGER.info("No corresponding age code for the given index");
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
}

if (result == null){
LOGGER.info("No corresponding age code for the given index");
throw new OHAPIException(new OHExceptionMessage("Age type not found with index :" + index), HttpStatus.NOT_FOUND);
}

return mapper.map2DTO(result);
}

}
Loading

0 comments on commit 4ffa8ee

Please sign in to comment.