cleaned up client api

pull/210/head
Justin Richer 2012-08-28 12:29:59 -04:00
parent 72c125ba64
commit 4af3dd89be
1 changed files with 13 additions and 18 deletions

View File

@ -30,6 +30,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
@ -48,13 +49,8 @@ public class ClientAPI {
@Autowired @Autowired
private ClientDetailsEntityService clientService; private ClientDetailsEntityService clientService;
private JsonParser parser = new JsonParser();
/** private Gson gson = new Gson();
* constructor
*/
public ClientAPI() {
}
/** /**
* Get a list of all clients * Get a list of all clients
@ -81,11 +77,9 @@ public class ClientAPI {
@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json") @RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
public String apiAddClient(@RequestBody String jsonString, Model m, Principal principal) { public String apiAddClient(@RequestBody String jsonString, Model m, Principal principal) {
// TODO: sanity check if the thing really is a JSON object JsonObject json = parser.parse(jsonString).getAsJsonObject();
JsonObject json = new JsonParser().parse(jsonString).getAsJsonObject();
// parse the client passed in (from JSON) and fetch the old client from the store ClientDetailsEntity client = gson.fromJson(json, ClientDetailsEntity.class);
ClientDetailsEntity client = new Gson().fromJson(json, ClientDetailsEntity.class);
// if they leave the client secret empty, force it to be generated // if they leave the client secret empty, force it to be generated
if (Strings.isNullOrEmpty(client.getClientId())) { if (Strings.isNullOrEmpty(client.getClientId())) {
@ -102,7 +96,8 @@ public class ClientAPI {
//TODO: owner has been replaced by a list of contacts, which should be styled as email addresses. //TODO: owner has been replaced by a list of contacts, which should be styled as email addresses.
client.setDynamicallyRegistered(false); client.setDynamicallyRegistered(false);
m.addAttribute("entity", clientService.saveNewClient(client)); ClientDetailsEntity newClient = clientService.saveNewClient(client);
m.addAttribute("entity", newClient);
return "jsonClientView"; return "jsonClientView";
} }
@ -119,10 +114,10 @@ public class ClientAPI {
public String apiUpdateClient(@PathVariable("id") Long id, @RequestBody String jsonString, Model m, Principal principal) { public String apiUpdateClient(@PathVariable("id") Long id, @RequestBody String jsonString, Model m, Principal principal) {
// TODO: sanity check if the thing really is a JSON object // TODO: sanity check if the thing really is a JSON object
JsonObject json = new JsonParser().parse(jsonString).getAsJsonObject(); JsonObject json = parser.parse(jsonString).getAsJsonObject();
// parse the client passed in (from JSON) and fetch the old client from the store // parse the client passed in (from JSON) and fetch the old client from the store
ClientDetailsEntity client = new Gson().fromJson(json, ClientDetailsEntity.class); ClientDetailsEntity client = gson.fromJson(json, ClientDetailsEntity.class);
ClientDetailsEntity oldClient = clientService.getClientById(id); ClientDetailsEntity oldClient = clientService.getClientById(id);
if (oldClient == null) { if (oldClient == null) {
@ -143,7 +138,8 @@ public class ClientAPI {
// client.setOwner(principal.getName()); // client.setOwner(principal.getName());
//TODO: owner has been replaced by a list of contacts, which should be styled as email addresses. //TODO: owner has been replaced by a list of contacts, which should be styled as email addresses.
m.addAttribute("entity", clientService.updateClient(oldClient, client)); ClientDetailsEntity newClient = clientService.updateClient(oldClient, client);
m.addAttribute("entity", newClient);
return "jsonClientView"; return "jsonClientView";
} }
@ -171,11 +167,10 @@ public class ClientAPI {
* @return * @return
*/ */
@RequestMapping(value="/{id}", method=RequestMethod.GET, headers="Accept=application/json") @RequestMapping(value="/{id}", method=RequestMethod.GET, headers="Accept=application/json")
@ResponseBody public ModelAndView apiShowClient(@PathVariable("id") Long id, ModelAndView modelAndView) {
public Object apiShowClient(@PathVariable("id") Long id, ModelAndView modelAndView) {
ClientDetailsEntity client = clientService.getClientById(id); ClientDetailsEntity client = clientService.getClientById(id);
if (client == null) { if (client == null) {
return new ResponseEntity<String>(HttpStatus.NOT_FOUND); throw new ClientNotFoundException("Could not find client: " + id);
} }
modelAndView.addObject("entity", client); modelAndView.addObject("entity", client);