From: Ronald Holshausen Date: Mon, 9 Nov 2020 03:51:03 +0000 (+1100) Subject: chore: add another endpoint X-Git-Tag: wip-umbau-array-contains~23 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=454632db216d6070f10ba37de9772914601a9f79;p=demos%2Fexample-siren chore: add another endpoint --- diff --git a/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java b/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java index b2f1d7f..d08e443 100644 --- a/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java +++ b/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java @@ -27,10 +27,9 @@ public class OrderController { @GetMapping public ResponseEntity> orders() { - Order order = new Order(new Random().nextLong()); - Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel() - .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null))) - .andAffordance(afford(methodOn(OrderController.class).delete(order.getId()))); + Long id = Math.abs(new Random().nextLong()); + Order order = new Order(id); + Link selfLink = actions(order); EntityModel model = EntityModel.of(order, selfLink); RepresentationModel orders = CollectionModel.of(model); orders.add(linkTo(methodOn(OrderController.class).orders()).withSelfRel()); @@ -40,20 +39,29 @@ public class OrderController { @GetMapping(value = "/{id}") public ResponseEntity> order(@PathVariable(value = "id", required = true) Long id) { Order order = new Order(id); - Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel() - .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null))) - .andAffordance(afford(methodOn(OrderController.class).delete(order.getId()))); + Link selfLink = actions(order); EntityModel model = EntityModel.of(order, selfLink); return ResponseEntity.ok(model); } @PutMapping("/{id}") public EntityModel update(@PathVariable(value = "id", required = true) Long id, Order order) { - Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel() + Link selfLink = actions(order); + return EntityModel.of(order, selfLink); + } + + private Link actions(Order order) { + return linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel() .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null))) - .andAffordance(afford(methodOn(OrderController.class).delete(order.getId()))); - EntityModel model = EntityModel.of(order, selfLink); - return model; + .andAffordance(afford(methodOn(OrderController.class).delete(order.getId()))) + .andAffordance(afford(methodOn(OrderController.class).changeStatus(order.getId(), null))); + } + + @PutMapping("/{id}/status") + public EntityModel changeStatus(@PathVariable(value = "id", required = true) Long id, String status) { + Order order = new Order(id); + Link selfLink = actions(order); + return EntityModel.of(order, selfLink); } @DeleteMapping("/{id}")