diff --git a/affordances/README.adoc b/affordances/README.adoc index 0f3ab3a..ad9f624 100644 --- a/affordances/README.adoc +++ b/affordances/README.adoc @@ -128,17 +128,17 @@ class EmployeeController { ResponseEntity>> findAll() { List> employeeResources = StreamSupport.stream(repository.findAll().spliterator(), false) - .map(employee -> EntityModel.of(employee, - linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel() - .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, employee.getId()))) - .andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(employee.getId()))), - linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees") - )) - .collect(Collectors.toList()); + .map(employee -> EntityModel.of(employee, + linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel() + .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, employee.getId()))) + .andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(employee.getId()))), + linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"))) + .collect(Collectors.toList()); - return ResponseEntity.ok(CollectionModel.of(employeeResources, - linkTo(methodOn(EmployeeController.class).findAll()).withSelfRel() - .andAffordance(afford(methodOn(EmployeeController.class).newEmployee(null))))); + return ResponseEntity.ok(CollectionModel.of( // + employeeResources, // + linkTo(methodOn(EmployeeController.class).findAll()).withSelfRel() + .andAffordance(afford(methodOn(EmployeeController.class).newEmployee(null))))); } @PostMapping("/employees") @@ -147,21 +147,20 @@ class EmployeeController { Employee savedEmployee = repository.save(employee); return EntityModel.of(savedEmployee, - linkTo(methodOn(EmployeeController.class).findOne(savedEmployee.getId())).withSelfRel() - .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, savedEmployee.getId()))) - .andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(savedEmployee.getId()))), - linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees") - ).getId() - .map(Link::getHref) - .map(href -> { - try { - return new URI(href); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - }) - .map(uri -> ResponseEntity.noContent().location(uri).build()) - .orElse(ResponseEntity.badRequest().body("Unable to create " + employee)); + linkTo(methodOn(EmployeeController.class).findOne(savedEmployee.getId())).withSelfRel() + .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, savedEmployee.getId()))) + .andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(savedEmployee.getId()))), + linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees")).getLink(IanaLinkRelations.SELF) + .map(Link::getHref) // + .map(href -> { + try { + return new URI(href); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + }) // + .map(uri -> ResponseEntity.noContent().location(uri).build()) + .orElse(ResponseEntity.badRequest().body("Unable to create " + employee)); } ... @@ -207,14 +206,13 @@ class EmployeeController { ResponseEntity> findOne(@PathVariable long id) { return repository.findById(id) - .map(employee -> EntityModel.of(employee, - linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel() - .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, employee.getId()))) - .andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(employee.getId()))), - linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees") - )) - .map(ResponseEntity::ok) - .orElse(ResponseEntity.notFound().build()); + .map(employee -> EntityModel.of(employee, + linkTo(methodOn(EmployeeController.class).findOne(employee.getId())).withSelfRel() + .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, employee.getId()))) + .andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(employee.getId()))), + linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"))) + .map(ResponseEntity::ok) // + .orElse(ResponseEntity.notFound().build()); } @PutMapping("/employees/{id}") @@ -226,21 +224,19 @@ class EmployeeController { Employee updatedEmployee = repository.save(employeeToUpdate); return EntityModel.of(updatedEmployee, - linkTo(methodOn(EmployeeController.class).findOne(updatedEmployee.getId())).withSelfRel() - .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, updatedEmployee.getId()))) - .andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(updatedEmployee.getId()))), - linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees") - ).getId() - .map(Link::getHref) - .map(href -> { - try { - return new URI(href); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - }) - .map(uri -> ResponseEntity.noContent().location(uri).build()) - .orElse(ResponseEntity.badRequest().body("Unable to update " + employeeToUpdate)); + linkTo(methodOn(EmployeeController.class).findOne(updatedEmployee.getId())).withSelfRel() + .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, updatedEmployee.getId()))) + .andAffordance(afford(methodOn(EmployeeController.class).deleteEmployee(updatedEmployee.getId()))), + linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees")).getLink(IanaLinkRelations.SELF) + .map(Link::getHref).map(href -> { + try { + return new URI(href); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + }) // + .map(uri -> ResponseEntity.noContent().location(uri).build()) // + .orElse(ResponseEntity.badRequest().body("Unable to update " + employeeToUpdate)); } ...