87f5e677b2543154cab1ff11717f1741525d6b61
[demos/example-siren] / provider / src / main / java / io / pactflow / example / sirenprovider / controllers / OrderController.java
1 package io.pactflow.example.sirenprovider.controllers;
2
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.RepresentationModel;
7 import org.springframework.hateoas.server.ExposesResourceFor;
8 import org.springframework.web.bind.annotation.GetMapping;
9 import org.springframework.web.bind.annotation.PathVariable;
10 import org.springframework.web.bind.annotation.RestController;
11
12 import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
13 import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
14
15 @RestController
16 @ExposesResourceFor(Order.class)
17 public class OrderController {
18
19   @GetMapping("/orders")
20   public RepresentationModel<?> orders() {
21     Order order = new Order(1234L);
22     EntityModel<Order> model = EntityModel.of(order);
23     model.add(linkTo(methodOn(OrderController.class).order(1234L)).withSelfRel());
24     RepresentationModel<?> orders = CollectionModel.of(model);
25     return orders;
26   }
27
28   @GetMapping("/orders/{id}")
29   public EntityModel<Order> order(@PathVariable(value = "id", required = true) Long id) {
30     Order order = new Order(id);
31     EntityModel<Order> model = EntityModel.of(order);
32     model.add(linkTo(methodOn(OrderController.class).order(id)).withSelfRel());
33     return model;
34   }
35
36 }