Skip to content

Commit

Permalink
spring-hateoas #end
Browse files Browse the repository at this point in the history
  • Loading branch information
jrdalpra committed Apr 5, 2014
1 parent 5db5b51 commit 4cf90c9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,44 @@
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import com.zeroturnaround.rebellabs.addresses.api.LocalesRepository;
import com.zeroturnaround.rebellabs.addresses.api.NeighborhoodsRepository;
import com.zeroturnaround.rebellabs.addresses.api.PublicPlacesRepository;
import com.zeroturnaround.rebellabs.addresses.api.TypesOfPublicPlacesRepository;
import com.zeroturnaround.rebellabs.addresses.api.exceptions.NotFoundException;
import com.zeroturnaround.rebellabs.addresses.model.Locale;
import com.zeroturnaround.rebellabs.addresses.model.Neighborhood;
import com.zeroturnaround.rebellabs.addresses.model.PublicPlace;
import com.zeroturnaround.rebellabs.addresses.model.TypeOfPublicPlaces;

public class InMemoryPublicPlacesRepository implements PublicPlacesRepository {

private Map<Long, PublicPlace> data = new HashMap<>();
private Map<Long, PublicPlace> data = new HashMap<>();

@Inject
private LocalesRepository locales;

@Inject
private NeighborhoodsRepository neighborhoods;

@Inject
private TypesOfPublicPlacesRepository types;

@PostConstruct
public void setup() {
Long id = 0l;
for (Locale locale : locales.list(0, 99999)) {
for (Neighborhood neighborhood : neighborhoods.listRelatedWith(locale, 0, 99999)) {
for (TypeOfPublicPlaces type : types.list(0, 99999)) {
id++;
data.put(id, new PublicPlace(id, "Public Place " + id, type, locale, neighborhood));
}
}
}
}

@Override
public PublicPlace get(Long id) throws NotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public PublicPlaceResource toResource(PublicPlace entity) {
private PublicPlacesRepository publicPlaces;

@RequestMapping("/publicplaces/{id}")
public ResponseEntity<PublicPlaceResource> get(PublicPlace publicPlace) {
public ResponseEntity<PublicPlaceResource> get(@PathVariable("id") PublicPlace publicPlace) {
return new ResponseEntity<>(new PublicPlaceResource(publicPlaces.reload(publicPlace)), HttpStatus.OK);
}

Expand All @@ -104,15 +104,15 @@ private Iterable<Link> withPageLinks(Integer page, Integer max) {
public ResponseEntity<Resources<PublicPlaceResource>> listRelatedWith(@PathVariable("id") Locale locale,
@RequestParam(value = "page", defaultValue = "0") final Integer page,
@RequestParam(value = "max", defaultValue = "10") final Integer max) {
return new ResponseEntity<>(new Resources<>(ofPublicPlacesFrom(locale, page, max), withPageLinksFrom(locale, page, max)), HttpStatus.OK);
return new ResponseEntity<>(new Resources<>(ofPublicPlacesRelatedWith(locale, page, max), withPageLinksRelatedWith(locale, page, max)), HttpStatus.OK);

}

private List<PublicPlaceResource> ofPublicPlacesFrom(Locale locale, Integer page, Integer max) {
private List<PublicPlaceResource> ofPublicPlacesRelatedWith(Locale locale, Integer page, Integer max) {
return listOfResourcesFrom(publicPlaces.listRelatedWith(locale, page, max));
}

private Iterable<Link> withPageLinksFrom(Locale locale, Integer page, Integer max) {
private Iterable<Link> withPageLinksRelatedWith(Locale locale, Integer page, Integer max) {
return asList(linkTo(methodOn(PublicPlacesController.class).listRelatedWith(locale, 0, max)).withRel("first"),
linkTo(methodOn(PublicPlacesController.class).listRelatedWith(locale, page.orWhenNull(0) + 1, max)).withRel("next"),
linkTo(methodOn(PublicPlacesController.class).listRelatedWith(locale, publicPlaces.lastPageRelatedWith(locale, max), max)).withRel("last"));
Expand Down

0 comments on commit 4cf90c9

Please sign in to comment.