1 package io.pactflow.example.sirenprovider.controllers;
3 import io.pactflow.example.sirenprovider.models.Order;
4 import org.springframework.hateoas.CollectionModel;
5 import org.springframework.hateoas.EntityModel;
6 import org.springframework.hateoas.Link;
7 import org.springframework.hateoas.RepresentationModel;
8 import org.springframework.hateoas.server.ExposesResourceFor;
9 import org.springframework.http.ResponseEntity;
10 import org.springframework.web.bind.annotation.DeleteMapping;
11 import org.springframework.web.bind.annotation.GetMapping;
12 import org.springframework.web.bind.annotation.PathVariable;
13 import org.springframework.web.bind.annotation.PutMapping;
14 import org.springframework.web.bind.annotation.RestController;
16 import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.afford;
17 import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
18 import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
21 @ExposesResourceFor(Order.class)
22 public class OrderController {
24 @GetMapping("/orders")
25 public RepresentationModel<?> orders() {
26 Order order = new Order(1234L);
27 Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel()
28 .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null)))
29 .andAffordance(afford(methodOn(OrderController.class).delete(order.getId())));
30 EntityModel<Order> model = EntityModel.of(order, selfLink);
31 RepresentationModel<?> orders = CollectionModel.of(model);
32 orders.add(linkTo(methodOn(OrderController.class).orders()).withSelfRel());
36 @GetMapping("/orders/{id}")
37 public EntityModel<Order> order(@PathVariable(value = "id", required = true) Long id) {
38 Order order = new Order(id);
39 Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel()
40 .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null)))
41 .andAffordance(afford(methodOn(OrderController.class).delete(order.getId())));
42 EntityModel<Order> model = EntityModel.of(order, selfLink);
46 @PutMapping("/orders/{id}")
47 public EntityModel<Order> update(@PathVariable(value = "id", required = true) Long id, Order order) {
48 Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel()
49 .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null)))
50 .andAffordance(afford(methodOn(OrderController.class).delete(order.getId())));
51 EntityModel<Order> model = EntityModel.of(order, selfLink);
55 @DeleteMapping("/orders/{id}")
56 public ResponseEntity<Void> delete(@PathVariable(value = "id", required = true) Long id) {
57 return ResponseEntity.ok().build();