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.RequestMapping;
15 import org.springframework.web.bind.annotation.RestController;
17 import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.afford;
18 import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
19 import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
22 @ExposesResourceFor(Order.class)
23 @RequestMapping("/orders")
24 public class OrderController {
26 @GetMapping("/orders")
27 public RepresentationModel<?> orders() {
28 Order order = new Order(1234L);
29 Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel()
30 .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null)))
31 .andAffordance(afford(methodOn(OrderController.class).delete(order.getId())));
32 EntityModel<Order> model = EntityModel.of(order, selfLink);
33 RepresentationModel<?> orders = CollectionModel.of(model);
34 orders.add(linkTo(methodOn(OrderController.class).orders()).withSelfRel());
38 @GetMapping("/orders/{id}")
39 public EntityModel<Order> order(@PathVariable(value = "id", required = true) Long id) {
40 Order order = new Order(id);
41 Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel()
42 .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null)))
43 .andAffordance(afford(methodOn(OrderController.class).delete(order.getId())));
44 EntityModel<Order> model = EntityModel.of(order, selfLink);
48 @PutMapping("/orders/{id}")
49 public EntityModel<Order> update(@PathVariable(value = "id", required = true) Long id, Order order) {
50 Link selfLink = linkTo(methodOn(OrderController.class).order(order.getId())).withSelfRel()
51 .andAffordance(afford(methodOn(OrderController.class).update(order.getId(), null)))
52 .andAffordance(afford(methodOn(OrderController.class).delete(order.getId())));
53 EntityModel<Order> model = EntityModel.of(order, selfLink);
57 @DeleteMapping("/orders/{id}")
58 public ResponseEntity<Void> delete(@PathVariable(value = "id", required = true) Long id) {
59 return ResponseEntity.ok().build();