diff --git a/src/main/java/org/springframework/samples/petclinic/web/OwnerListResource.java b/src/main/java/org/springframework/samples/petclinic/web/OwnerListResource.java deleted file mode 100644 index ae7782be9876bdf5de26fd5741f8095bd6976785..0000000000000000000000000000000000000000 --- a/src/main/java/org/springframework/samples/petclinic/web/OwnerListResource.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2002-2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.samples.petclinic.web; - -import java.util.Collection; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.service.ClinicService; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -/** - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - * @author Michael Isvy - */ -@RestController -public class OwnerListResource { - - private final ClinicService clinicService; - - - @Autowired - public OwnerListResource(ClinicService clinicService) { - this.clinicService = clinicService; - } - - @RequestMapping(value = "/owner/list", method = RequestMethod.GET, - produces = MediaType.APPLICATION_JSON_VALUE) - public Collection<Owner> findOwnerCollection(@RequestParam("lastName") String ownerLastName) { - - if (ownerLastName == null) { - ownerLastName = ""; - } - - Collection<Owner> results = this.clinicService.findOwnerByLastName(ownerLastName); - if (results.isEmpty()) { - return null; - } - else { - return results; - } - } - -} diff --git a/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java b/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java index 966a8a43ee2dd943caec0d16bd9b2443dc1111e5..fbcbd494f20747b4cfbb21b9756eb3167cd27b0f 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java @@ -15,17 +15,19 @@ */ package org.springframework.samples.petclinic.web; +import java.util.Collection; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.ModelAttribute; 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.RequestParam; import org.springframework.web.bind.annotation.RestController; /** @@ -50,14 +52,14 @@ public class OwnerResource { dataBinder.setDisallowedFields("id"); } - @ModelAttribute - public Owner retrieveOwner(@PathVariable("ownerId") int ownerId) { + private Owner retrieveOwner(int ownerId) { return this.clinicService.findOwnerById(ownerId); } - // TODO: should be improved so we have a single method parameter @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.PUT) - public Owner updateOwner(@ModelAttribute Owner ownerModel, @RequestBody Owner ownerRequest) { + public Owner updateOwner(@PathVariable("ownerId") int ownerId, @RequestBody Owner ownerRequest) { + Owner ownerModel = retrieveOwner(ownerId); + // This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. ownerModel.setFirstName(ownerRequest.getFirstName()); ownerModel.setLastName(ownerRequest.getLastName()); ownerModel.setCity(ownerRequest.getCity()); @@ -71,9 +73,25 @@ public class OwnerResource { @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) - public Owner findOwner(Owner owner) { - // Has already been retrieved from 'retrieveOwner' method - return owner; + public Owner findOwner(@PathVariable("ownerId") int ownerId) { + return retrieveOwner(ownerId); + } + + @RequestMapping(value = "/owner/list", method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON_VALUE) + public Collection<Owner> findOwnerCollection(@RequestParam("lastName") String ownerLastName) { + + if (ownerLastName == null) { + ownerLastName = ""; + } + + Collection<Owner> results = this.clinicService.findOwnerByLastName(ownerLastName); + if (results.isEmpty()) { + return null; + } + else { + return results; + } } } diff --git a/src/main/webapp/scripts/app/owner/OwnerController.js b/src/main/webapp/scripts/app/owner/OwnerController.js index 8202a17bf2451aeb0fd70373842a1067e5ab48d2..07acfc0e8ea70e146d5a591ab3d6b500645198b8 100644 --- a/src/main/webapp/scripts/app/owner/OwnerController.js +++ b/src/main/webapp/scripts/app/owner/OwnerController.js @@ -47,7 +47,7 @@ function loadOwner($scope, $resource, $stateParams) { angular.module('controllers').controller('ownerFormController', ['$scope', '$resource', '$http', '$stateParams', '$state', function($scope, $resource, $http, $stateParams, $state) { - scope.submitOwnerForm = {}; + $scope.submitOwnerForm = {}; $scope.submitOwnerForm = function() { var form = $scope.owner; diff --git a/src/main/webapp/scripts/app/owner/ownerSearchForm.html b/src/main/webapp/scripts/app/owner/ownerSearchForm.html index 528a87e79aa3ce4bce8a5fc4b5d07b92a852dc2d..7b9fe5c489dde87328020596dbe7fa49799ca39b 100644 --- a/src/main/webapp/scripts/app/owner/ownerSearchForm.html +++ b/src/main/webapp/scripts/app/owner/ownerSearchForm.html @@ -1,4 +1,4 @@ - <h2>Find Ownersss</h2> + <h2>Find Owners</h2> <form class="form-horizontal" ng-controller="ownerSearchController"> <fieldset> <div class="control-group" id="lastName"> @@ -6,10 +6,10 @@ <input ng-model="ownerSearchForm.lastName" size="30" maxlength="80"/> </div> <div class="form-actions"> - <button type="submit" ng-click="submitOwnerSearchForm()">Find Ownerss</button> + <button type="submit" ng-click="submitOwnerSearchForm()">Find Owners</button> </div> </fieldset> </form> <br/> - <a href="/owners/new">Add Ownerss</a> + <a href="/owners/new">Add Owners</a>