package hello.controllers;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import hello.dao.GreetingService;
import hello.entities.Greeting;

@RestController
@RequestMapping(value = "/greeting", produces = MediaType.APPLICATION_JSON_VALUE)
public class GreetingControllerRest {

	@Autowired
	private GreetingService greetingService;

	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<Iterable<Greeting>> list() {
		return new ResponseEntity<Iterable<Greeting>>(greetingService.findAll(), HttpStatus.OK);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Resource<Greeting> greeting(@PathVariable("id") long id) {
		Greeting greeting = greetingService.findOne(id);

		return greetingToResource(greeting);
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public ResponseEntity<?> newGreeting() {
		return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).build();
	}

	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> createGreeting(@RequestBody @Valid Greeting greeting,
			BindingResult result, UriComponentsBuilder b) {

		if (result.hasErrors()) {
			return ResponseEntity.unprocessableEntity().build();
		}

		greetingService.save(greeting);
		UriComponents location = b.path("/greeting/{id}").buildAndExpand(greeting.getId());

		return ResponseEntity.created(location.toUri()).build();
	}

	private Resource<Greeting> greetingToResource(Greeting greeting) {
		Link selfLink = linkTo(GreetingControllerRest.class).slash(greeting.getId()).withSelfRel();

		return new Resource<Greeting>(greeting, selfLink);
	}
}

@@ -96,16 +96,146 @@
 .linkTo;
+%0Aimport static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -405,24 +405,70 @@
 s.Resource;%0A
+import org.springframework.hateoas.Resources;%0A
 import org.s
@@ -1377,35 +1377,30 @@
 blic Res
-ponseEntity%3CIterabl
+ources%3CResourc
 e%3CGreeti
@@ -1426,46 +1426,26 @@
 urn 
-new ResponseEntity%3CIterable%3CGreeting%3E%3E
+greetingToResource
 (gre
@@ -1470,23 +1470,8 @@
 ll()
-, HttpStatus.OK
 );%0A%09
@@ -2526,24 +2526,24 @@
 SelfRel();%0A%0A
-
 %09%09return new
@@ -2585,11 +2585,427 @@
 ink);%0A%09%7D
+%0A%0A%09private Resources%3CResource%3CGreeting%3E%3E greetingToResource(Iterable%3CGreeting%3E greetings) %7B%0A%09%09Link selfLink = linkTo(methodOn(GreetingControllerRest.class).list()).withSelfRel();%0A%0A%09%09List%3CResource%3CGreeting%3E%3E resources = new ArrayList%3CResource%3CGreeting%3E%3E();%0A%09%09for (Greeting greeting : greetings) %7B%0A%09%09%09resources.add(greetingToResource(greeting));%0A%09%09%7D%0A%0A%09%09return new Resources%3CResource%3CGreeting%3E%3E(resources, selfLink);%0A%09%7D
 %0A%7D%0A

Build the list of greetings as a Resources list.