1 package de.spring.example.rest.controllers;
3 import javax.inject.Inject;
5 import org.resthub.web.controller.RepositoryBasedRestController;
6 import org.springframework.data.domain.Page;
7 import org.springframework.data.domain.PageRequest;
8 import org.springframework.data.domain.Sort;
9 import org.springframework.data.history.Revision;
10 import org.springframework.util.Assert;
11 import org.springframework.web.bind.annotation.PathVariable;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestMethod;
14 import org.springframework.web.bind.annotation.RequestParam;
15 import org.springframework.web.bind.annotation.ResponseBody;
16 import org.springframework.web.bind.annotation.RestController;
18 import de.spring.example.persistence.domain.Ad;
19 import de.spring.example.persistence.repository.AdRepository;
22 @RequestMapping("/ads/")
23 public class AdController extends RepositoryBasedRestController<Ad, Long, AdRepository> {
27 public void setRepository(AdRepository repository) {
28 this.repository = repository;
31 // I do not have to do anything here because all I need is implemented by RepositoryBasedRestController :)
33 // @Transactional is implemented by org.springframework.data.jpa.repository.support.SimpleJpaRepository
34 // By default, SimpleJpaRepository will be automatically implemented by my
35 // Spring JPA repositories: AdRepository and AdDescriptionRepository.
43 /** WE ARE EXTENDING RepositoryBasedRestController WITH METHODS FOR RETRIEVING REVISION NUMBERS!!! **/
47 * Returns a {@link Page} of revisions for the entity with the given id
49 * @param page Page number starting from 0. default to 0
50 * @param size Number of resources by pages. default to 10
51 * @param direction Optional sort direction, could be "asc" or "desc"
52 * @param properties Ordered list of comma separeted properies used for sorting resulats. At least one property should be provided if direction is specified
53 * @return OK http status code if the request has been correctly processed, with the a paginated collection of all resource enclosed in the body.
55 @RequestMapping(value="{id}/revisions", method = RequestMethod.GET)
57 public Page<Revision<Integer, Ad>> findRevisionsPaginated(@PathVariable Long id,
58 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
59 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
60 @RequestParam(value = "direction", required = false, defaultValue = "") String direction,
61 @RequestParam(value = "properties", required = false) String properties) {
62 Assert.isTrue(page > 0, "Page index must be greater than 0");
63 Assert.isTrue(direction.isEmpty() || direction.equalsIgnoreCase(Sort.Direction.ASC.toString()) || direction.equalsIgnoreCase(Sort.Direction.DESC.toString()), "Direction should be ASC or DESC");
64 if(direction.isEmpty()) {
65 return this.repository.findRevisions(id, new PageRequest(page - 1, size));
67 Assert.notNull(properties);
68 return this.repository.findRevisions(id, new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(","))));