X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=provider%2Fsrc%2Fmain%2Fjava%2Fio%2Fpactflow%2Fexample%2Fsirenprovider%2Fcontrollers%2FOrderController.java;h=72cf97335a53412029a7731867d2d69df317d49d;hb=9777f26defdb7706cf488160af693766954a1bb5;hp=87f5e677b2543154cab1ff11717f1741525d6b61;hpb=d3a04cd969afdc761963b831f0846e81eaa5d89f;p=demos%2Fexample-siren 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(); + } + }