From 41ec22b4becac3b5057fe0930f0aab204984066d Mon Sep 17 00:00:00 2001 From: michaelisvy Date: Sun, 14 Jun 2015 20:50:22 +0800 Subject: [PATCH] ownerForm and validation now working --- .../petclinic/web/OwnerListResource.java | 70 ++++++++++++++ .../samples/petclinic/web/OwnerResource.java | 93 ++++++------------- src/main/webapp/scripts/app/app.js | 2 +- .../scripts/app/owner/OwnerController.js | 70 +++++++++++--- .../webapp/scripts/app/owner/ownerForm.html | 25 +++-- .../scripts/app/owner/ownerSearchForm.html | 3 +- 6 files changed, 167 insertions(+), 96 deletions(-) create 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 new file mode 100644 index 000000000..8054fc8f0 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerListResource.java @@ -0,0 +1,70 @@ +/* + * 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 javax.validation.Valid; + +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.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.InitBinder; +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; + +/** + * @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 findOwnerCollection(@RequestParam("lastName") String ownerLastName) { + + if (ownerLastName == null) { + ownerLastName = ""; + } + + Collection 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 ad5a3d6dc..48b33ac41 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java @@ -16,7 +16,6 @@ package org.springframework.samples.petclinic.web; import java.util.Collection; -import java.util.Map; import javax.validation.Valid; @@ -24,17 +23,16 @@ 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.ui.Model; import org.springframework.validation.BindingResult; 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; -import org.springframework.web.bind.support.SessionStatus; -import org.springframework.web.servlet.ModelAndView; /** * @author Juergen Hoeller @@ -57,70 +55,31 @@ public class OwnerResource { public void setAllowedFields(WebDataBinder dataBinder) { dataBinder.setDisallowedFields("id"); } - - @RequestMapping(value = "/owner/new", method = RequestMethod.GET) - public String initCreationForm(Map model) { - Owner owner = new Owner(); - model.put("owner", owner); - return "owner/createOrUpdateOwnerForm"; - } - - @RequestMapping(value = "/owner/new", method = RequestMethod.POST) - public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) { - if (result.hasErrors()) { - return "owner/createOrUpdateOwnerForm"; - } else { - this.clinicService.saveOwner(owner); - status.setComplete(); - return "redirect:/owner/" + owner.getId(); - } - } - - @RequestMapping(value = "/owner/list", method = RequestMethod.GET, - produces = MediaType.APPLICATION_JSON_VALUE) - public Collection findOwners(@RequestParam("lastName") String ownerLastName) { - - if (ownerLastName == null) { - ownerLastName = ""; - } - - Collection results = this.clinicService.findOwnerByLastName(ownerLastName); - if (results.isEmpty()) { - return null; - } - else { - return results; - } - } - - @RequestMapping(value = "/owner/{ownerId}/edit", method = RequestMethod.GET) - public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { - Owner owner = this.clinicService.findOwnerById(ownerId); - model.addAttribute(owner); - return "owner/createOrUpdateOwnerForm"; - } - - @RequestMapping(value = "/owner/{ownerId}/edit", method = RequestMethod.PUT) - public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) { - if (result.hasErrors()) { - return "owners/createOrUpdateOwnerForm"; - } else { - this.clinicService.saveOwner(owner); - status.setComplete(); - return "redirect:/owner/{ownerId}"; - } - } - - /** - * Custom handler for displaying an owner. - * - * @param ownerId the ID of the owner to display - * @return a ModelMap with the model attributes for the view - */ - @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.GET, - produces = MediaType.APPLICATION_JSON_VALUE) - public Owner showOwner(@PathVariable("ownerId") int ownerId) { + + @ModelAttribute + public Owner retrieveOwner(@PathVariable("ownerId") int ownerId) { return this.clinicService.findOwnerById(ownerId); } + // TODO: should be improved so we have a single method parameter + @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.POST) + public Owner updateOwner(@ModelAttribute Owner ownerModel, @RequestBody Owner ownerRequest) { + ownerModel.setFirstName(ownerRequest.getFirstName()); + ownerModel.setLastName(ownerRequest.getLastName()); + ownerModel.setCity(ownerRequest.getCity()); + ownerModel.setAddress(ownerRequest.getAddress()); + ownerModel.setTelephone(ownerRequest.getTelephone()); + this.clinicService.saveOwner(ownerModel); + return ownerModel; + // TODO: need to handle failure + } + + + @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; + } + } diff --git a/src/main/webapp/scripts/app/app.js b/src/main/webapp/scripts/app/app.js index 98da549aa..cb3c4b058 100644 --- a/src/main/webapp/scripts/app/app.js +++ b/src/main/webapp/scripts/app/app.js @@ -33,7 +33,7 @@ petClinicApp.config(['$routeProvider', }). when('/owner/:id/edit', { templateUrl: 'scripts/app/owner/ownerForm.html', - controller: 'ownerDetailController' + controller: 'ownerFormController' }). when('/vets', { templateUrl: 'scripts/app/vet/vetList.html', diff --git a/src/main/webapp/scripts/app/owner/OwnerController.js b/src/main/webapp/scripts/app/owner/OwnerController.js index e568d3870..021e85d69 100644 --- a/src/main/webapp/scripts/app/owner/OwnerController.js +++ b/src/main/webapp/scripts/app/owner/OwnerController.js @@ -5,14 +5,14 @@ */ angular.module('controllers').controller('ownerSearchController', ['$scope', '$rootScope', '$resource', '$location', function($scope, $rootScope, $resource, $location) { - - $scope.submitOwnerFindForm = function(item, event) { - + + $scope.submitOwnerFindForm = function() { + var destUrl = '/petclinic/owner/list?lastName=' if(angular.isDefined($scope.ownerFindForm)) { destUrl += $scope.ownerFindForm.lastName; } - + var ownerResource = $resource(destUrl); $rootScope.owners = ownerResource.query(); $location.path('/owner/list'); @@ -26,23 +26,67 @@ angular.module('controllers').controller('ownerListController', ['$scope', '$roo if ($rootScope.owners!=null){ $scope.ownerList = $rootScope.owners; } - + $scope.displayOwnerDetail = function(id) { var url = "owner/" + id + "/view"; $rootScope.ownerId = id; $location.path(url); - } + } }]); /* * Owners detail (used for both Editable and non-editable pages) */ angular.module('controllers').controller('ownerDetailController', ['$scope', '$resource', '$rootScope', - function($scope, $resource, $rootScope) { - var ownerResource = $resource('/petclinic/owner/' + $rootScope.ownerId); - $scope.owner = ownerResource.get(); + loadOwner +]); + +function loadOwner($scope, $resource, $rootScope) { + var ownerResource = $resource('/petclinic/owner/' + $rootScope.ownerId); + $scope.owner = ownerResource.get(); +} + +/* + * Owner Edit Form + */ +angular.module('controllers').controller('ownerFormController', ['$scope', '$resource', '$http', '$rootScope', '$location', +function($scope, $resource, $http, $rootScope, $location) { + + $scope.submitOwnerForm = function() { + var form = $scope.owner; + + // Creating a Javascript object + var data = { + firstName: form.firstName, + lastName: form.lastName, + address: form.address, + city: form.city, + telephone: form.telephone + }; + var restUrl = "/petclinic/owner/" + $rootScope.ownerId; + $http.post(restUrl, data); + $location.path('/owner/list'); + } + + loadOwner($scope, $resource, $rootScope); + }]); - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/scripts/app/owner/ownerForm.html b/src/main/webapp/scripts/app/owner/ownerForm.html index cc62bb829..ad604d442 100644 --- a/src/main/webapp/scripts/app/owner/ownerForm.html +++ b/src/main/webapp/scripts/app/owner/ownerForm.html @@ -1,37 +1,36 @@
-

Owner

-
+
- - First name is required. + + First name is required.
- - Last name is required. + + Last name is required.
- - Address is required. + + Address is required.
- - City is required. + + City is required.
- - Telephone is required. + + Telephone is required.
- +
diff --git a/src/main/webapp/scripts/app/owner/ownerSearchForm.html b/src/main/webapp/scripts/app/owner/ownerSearchForm.html index b916755f1..21e974672 100644 --- a/src/main/webapp/scripts/app/owner/ownerSearchForm.html +++ b/src/main/webapp/scripts/app/owner/ownerSearchForm.html @@ -1,6 +1,5 @@

Find Owners

- -
+