Thêm M I Patient
Thêm M I Patient
Patient
- Thêm mới patient
@PostMapping("/patient/add")
public ResponseEntity<Map<String, Object>> addPatient(@RequestBody
PatientDTO patientDTO) {
Map<String, Object> response = new HashMap<>();
try {
PatientEntity patientEntity = new PatientEntity();
patientEntity.setFirstName(patientDTO.getFirstName());
patientEntity.setLastName(patientDTO.getLastName());
patientEntity.setGender(patientDTO.getGender());
patientEntity.setDateOfBirth(patientDTO.getDob());
patientEntity.setStreet(patientDTO.getStreet());
patientEntity.setPhoneNumber(patientDTO.getPhoneNumber());
- Update patient
@PutMapping("patient/edit/{id}")
public ResponseEntity<Map<String,String>>
updatePatient(@PathVariable Integer id, @RequestBody PatientDTO
patientDetails) {
try {
Optional<PatientEntity> existingPatient =
patientRepository.findById(id);
Optional<Districts> district =
districtRepository.findById(patientDetails.getDistrictId());
if (district.isPresent()) {
patient.setDistrict(district.get());
} else {
return new ResponseEntity<>(Map.of("message", "District not
found"), HttpStatus.BAD_REQUEST);
}
Optional<Wards> ward =
wardRepository.findById(patientDetails.getWardId());
if (ward.isPresent()) {
patient.setWard(ward.get());
} else {
return new ResponseEntity<>(Map.of("message", "Ward not
found"), HttpStatus.BAD_REQUEST);
}
patientRepository.save(patient);
} catch (Exception e) {
return new ResponseEntity<>(Map.of("message", e.getMessage()),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
- Delete Patient
@DeleteMapping("patient/delete/{id}")
public ResponseEntity<Map<String, String>>
deletePatient(@PathVariable Integer id) {
Map<String, String> response = new HashMap<>();
try {
int updated = patientRepository.softDeletePatient(id);
if (updated > 0) {
response.put("message", "Xóa thành công");
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.put("message", "Bệnh nhân không tồn tại.");
return new ResponseEntity<>(response,
HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
response.put("message", "Không thành công: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Admission
- Thêm mới admission
@PostMapping("/admission/add")
public ResponseEntity<Map<String, Object>> addAdmission(@RequestBody
AdmissionDTO admissionDTO) {
Map<String, Object> response = new HashMap<>();
try {
// Check if Patient exists
PatientEntity patient =
patientRepository.findById(admissionDTO.getInpatientId())
.orElseThrow(() -> new IllegalArgumentException("Patient not found
with ID: " + admissionDTO.getInpatientId()));
Optional<InPatientEntity> optionalInPatient =
inPatientRepository.findByPatient_ID(patient.getID());
InPatientEntity inPatient;
if (optionalInPatient.isPresent()) {
inPatient = optionalInPatient.get();
} else {
inPatient = new InPatientEntity();
inPatient.setPatient(patient);
admissionEntity.setDateOfDischarge(admissionDTO.getDateOfDischarge());
admissionEntity.setFee(admissionDTO.getFee());
admissionRepository.save(admissionEntity);
- Update Admission
@PostMapping(value = "/admission/edit", consumes =
MediaType.APPLICATION_JSON_VALUE)
@Transactional
public ResponseEntity<Map<String, Object>>
editAdmission(@RequestParam("id") Integer admissionId, @RequestBody
AdmissionDTO admissionDTO) {
Map<String, Object> response = new HashMap<>();
try {
// Find existing admission by ID
Optional<AdmissionEntity> optionalAdmission =
admissionRepository.findById(admissionId);
if (!optionalAdmission.isPresent()) {
response.put("status", "error");
response.put("message", "Admission not found.");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}
response.put("status", "success");
response.put("message", "Cập nhật thành công!");
response.put("admissionId", admissionId);
return ResponseEntity.ok(response);
} catch (Exception e) {
response.put("status", "error");
response.put("message", "Không thành công: " + e.getMessage());
return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response)
;
}
}
- Admission delete
@DeleteMapping("admission/delete/{id}")
public ResponseEntity<Map<String, String>> admission_del(@PathVariable
Integer id) {
Map<String, String> response = new HashMap<>();
try {
int updated = admissionRepository.admission_del(id);
if (updated > 0) {
response.put("message", "Xóa thành công");
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.put("message", "Phiếu đăng kí không tồn tại");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
response.put("message", "Không thành công: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Examination
- Thêm mới examination
@PostMapping("examination/add")
public ResponseEntity<Map<String, Object>>
addExamination(@RequestBody ExaminationDTO examinationDTO) {
Map<String, Object> response = new HashMap<>();
try {
PatientEntity patient =
patientRepository.findById(examinationDTO.getOutPatientId())
.orElseThrow(() -> new IllegalArgumentException("Patient not
found with ID: " + examinationDTO.getOutPatientId()));
Optional<OutPatientEntity> optionalOutPatient =
outPatientRepository.findByPatient_ID(patient.getID());
OutPatientEntity outPatient;
if (optionalOutPatient.isPresent()) {
outPatient = optionalOutPatient.get();
} else {
outPatient = new OutPatientEntity();
outPatient.setPatient(patient);
examinationService.addExamination(examinationDTO);
- Update Exmination
@PostMapping("examination/edit")
@Transactional
public ResponseEntity<Map<String, Object>>
editExamination(@RequestParam("id") Integer examinationId, @RequestBody
ExaminationDTO examinationDTO) {
Map<String, Object> response = new HashMap<>();
try {
// Call the service layer to update the examination
examinationService.updateExamination(examinationDTO);
return ResponseEntity.ok(response);
} catch (Exception e) {
// Handle any errors
response.put("status", "error");
response.put("message", "Cập nhật không thành công: " +
e.getMessage());
return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(respo
nse);
}
}
- Examination Delete
@DeleteMapping("/examination/delete/{id}")
public ResponseEntity<Map<String, String>>
examination_del(@PathVariable Integer id) {
Map<String, String> response = new HashMap<>();
try {
int updated = examinationRepository.examination_del(id);
if (updated > 0) {
response.put("message", "Xóa thành công");
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.put("message", "Đợt khám không tồn tại");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
response.put("message", "Không thành công: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Treatment
try {
// Call the service layer to update the examination
treatmentService.addTreatment(treatmentDTO);
return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response)
;
}
}
- Update Treatment
@PostMapping("/treatment/edit")
@Transactional
public ResponseEntity<Map<String, Object>>
editTreatment(@RequestParam("id") Integer treatmentId, @RequestBody
TreatmentDTO treatmentDTO) {
Map<String, Object> response = new HashMap<>();
treatmentDTO.setId(treatmentId);
try {
treatmentService.updateTreatment(treatmentDTO);
// Construct success response
response.put("status", "success");
response.put("message", "Cập nhật thành công!");
response.put("treatmentId", treatmentId); // Optionally return the
examination ID
return ResponseEntity.ok(response);
} catch (Exception e) {
// Handle any errors
response.put("status", "error");
response.put("message", "Lỗi " + e.getMessage());
return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response)
;
}
}
- Delete Treatment
@DeleteMapping("/treatment/delete/{id}")
public ResponseEntity<Map<String, String>> treatment_del(@PathVariable
Integer id) {
Map<String, String> response = new HashMap<>();
try {
int updated = treatmentRepository.treatment_del(id);
if (updated > 0) {
response.put("message", "Xóa thành công");
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.put("message", "Quá trình điều trị không tồn tại");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
response.put("message", "Không thành công: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}