From fedb7d43fc3f295b034d7f2143e0338eff84c3c5 Mon Sep 17 00:00:00 2001 From: michaelisvy <misvy@gopivotal,com> Date: Wed, 1 Jul 2015 09:16:02 +0800 Subject: [PATCH] Merged OwnerListResource and OwnerResource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Realised that @ModelAttribute annotated methods are not suitable because they can’t be used in 2 cases: - when I’m using a list of Owners - when I’m creating a new Owner So we’re just calling the “retrieveOwner†method manually --- .../petclinic/web/OwnerListResource.java | 63 ------------------- .../samples/petclinic/web/OwnerResource.java | 34 +++++++--- .../scripts/app/owner/OwnerController.js | 2 +- .../scripts/app/owner/ownerSearchForm.html | 6 +- 4 files changed, 30 insertions(+), 75 deletions(-) delete mode 100644 src/main/java/org/springframework/samples/petclinic/web/OwnerListResource.java 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 ae7782be..00000000 --- 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 966a8a43..fbcbd494 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 8202a17b..07acfc0e 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 528a87e7..7b9fe5c4 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> -- GitLab