Last active
June 17, 2018 10:06
-
-
Save yashdsaraf/1c3c541570fb1157755fd9b69d9b3d20 to your computer and use it in GitHub Desktop.
CRUD (without search) operations for sample Patient model.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
@RequiredArgsConstructor | |
public class PatientHandler { | |
private final PatientRepo patientRepo; | |
Mono<ServerResponse> getPatient(ServerRequest request) { | |
UUID id = UUID.fromString(request.pathVariable("id")); | |
return patientRepo | |
.findById(id) | |
.flatMap(patient -> ServerResponse.ok().body(Mono.just(patient), Patient.class)) | |
.switchIfEmpty(ServerResponse.notFound().build()); | |
} | |
Mono<ServerResponse> search(ServerRequest request) { | |
return ServerResponse.ok() | |
.body(patientRepo.findAll(), Patient.class); | |
} | |
public Mono<ServerResponse> create(ServerRequest request) { | |
Mono<Patient> patientMono = request | |
.bodyToMono(Patient.class) | |
.doOnNext(patient -> patient.setId(UUID.randomUUID())); | |
return ServerResponse.ok() | |
.body(patientRepo.saveAll(patientMono), Patient.class); | |
} | |
Mono<ServerResponse> update(ServerRequest request) { | |
UUID id; | |
try { | |
id = UUID.fromString(request.pathVariable("id")); | |
} catch (IllegalArgumentException e) { | |
return ServerResponse.badRequest() | |
.body(fromObject(getOperationOutcome(IssueSeverity.FATAL, IssueType.INVALID, e.getMessage()))); | |
} | |
Mono<Patient> updatedPatientMono = request.bodyToMono(Patient.class) | |
.doOnNext(patient -> patient.setId(id)); | |
return patientRepo | |
.findById(id) | |
.flatMap(patient -> ServerResponse.ok() | |
.body( | |
updatedPatientMono | |
.flatMap(updatedPatient -> { | |
BeanUtils.copyProperties(updatedPatient, patient, getNullProperties(updatedPatient)); | |
return patientRepo.save(updatedPatient); | |
}), | |
Patient.class)) | |
.switchIfEmpty(ServerResponse | |
.created(URI.create("http://localhost:8080/patient/" + id)) | |
.lastModified(ZonedDateTime.now()) | |
.body(patientRepo.saveAll(updatedPatientMono), Patient.class)); | |
} | |
Mono<ServerResponse> delete(ServerRequest request) { | |
UUID id = UUID.fromString(request.pathVariable("id")); | |
return ServerResponse.noContent() | |
.build(patientRepo.deleteById(id)); | |
} | |
private OperationOutcome getOperationOutcome(IssueSeverity severity, IssueType type, String diagnostics) { | |
OperationOutcome outcome = new OperationOutcome(); | |
OperationOutcomeIssueComponent issue = new OperationOutcomeIssueComponent(); | |
issue.setSeverity(severity); | |
issue.setCode(type); | |
issue.setDiagnostics(diagnostics); | |
outcome.setIssue(Collections.singletonList(issue)); | |
return outcome; | |
} | |
private String[] getNullProperties(Object bean) { | |
BeanWrapper wrapper = new BeanWrapperImpl(bean); | |
return Stream.of(wrapper.getPropertyDescriptors()) | |
.map(FeatureDescriptor::getName) | |
.filter(s -> wrapper.getPropertyValue(s) == null) | |
.toArray(String[]::new); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment