From 9777f26defdb7706cf488160af693766954a1bb5 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 29 Sep 2020 14:06:08 +1000 Subject: [PATCH] feat: added Siren actions to order resource --- .../controllers/OrderController.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) 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 87f5e67..72cf973 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 @@ -3,12 +3,17 @@ package io.pactflow.example.sirenprovider.controllers; import io.pactflow.example.sirenprovider.models.Order; import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; +import org.springframework.hateoas.Link; import org.springframework.hateoas.RepresentationModel; import org.springframework.hateoas.server.ExposesResourceFor; +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.PutMapping; import org.springframework.web.bind.annotation.RestController; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.afford; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; @@ -19,18 +24,37 @@ public class OrderController { @GetMapping("/orders") public RepresentationModel orders() { Order order = new Order(1234L); - EntityModel model = EntityModel.of(order); - model.add(linkTo(methodOn(OrderController.class).order(1234L)).withSelfRel()); + 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()))); + EntityModel model = EntityModel.of(order, selfLink); RepresentationModel orders = CollectionModel.of(model); + orders.add(linkTo(methodOn(OrderController.class).orders()).withSelfRel()); return orders; } @GetMapping("/orders/{id}") public EntityModel order(@PathVariable(value = "id", required = true) Long id) { Order order = new Order(id); - EntityModel model = EntityModel.of(order); - model.add(linkTo(methodOn(OrderController.class).order(id)).withSelfRel()); + 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()))); + EntityModel model = EntityModel.of(order, selfLink); return model; } + @PutMapping("/orders/{id}") + public EntityModel update(Long id, Order order) { + 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()))); + EntityModel model = EntityModel.of(order, selfLink); + return model; + } + + @DeleteMapping("/order/{id}") + public ResponseEntity delete(Long id) { + return ResponseEntity.ok().build(); + } + } -- 2.20.1