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 0000000000000000000000000000000000000000..8054fc8f064ab43b10b6e29374aa1918bc148cb4 --- /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<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 ad5a3d6dcf93dedb03e1dc71fa3ca5bc22a0f9d3..48b33ac410ac427ab212b6f38c163a07514041ce 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<String, Object> model) { - Owner owner = new Owner(); - model.put("owner", owner); - return "owner/createOrUpdateOwnerForm"; + + @ModelAttribute + public Owner retrieveOwner(@PathVariable("ownerId") int ownerId) { + return this.clinicService.findOwnerById(ownerId); } - @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(); - } + // 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/list", method = RequestMethod.GET, - produces = MediaType.APPLICATION_JSON_VALUE) - public Collection<Owner> findOwners(@RequestParam("lastName") String ownerLastName) { - if (ownerLastName == null) { - ownerLastName = ""; - } - - Collection<Owner> 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) { - return this.clinicService.findOwnerById(ownerId); + 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 98da549aaa644ab1c565d094c2badea2e445ac30..cb3c4b058a9e7a785d760c9c6ad4464beba4d83a 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 e568d3870c86c297acb03cfd0f5c60b13122ba8b..021e85d69666276d2dd14f306bdfb04577d2955a 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'); + } - \ No newline at end of file + loadOwner($scope, $resource, $rootScope); + +}]); + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/scripts/app/owner/ownerForm.html b/src/main/webapp/scripts/app/owner/ownerForm.html index cc62bb829482b8558f847692370b425d5257a3a6..ad604d442eb435891da1ad2c17d14d4f42318d34 100644 --- a/src/main/webapp/scripts/app/owner/ownerForm.html +++ b/src/main/webapp/scripts/app/owner/ownerForm.html @@ -1,37 +1,36 @@ <div class="container"> - <h2>Owner</h2> - <form method="POST" class="form-horizontal" id="ownerForm"> + <form class="form-horizontal" name="ownerForm" ng-controller="ownerFormController"> <fieldset> <div class="control-group" id="firstName"> <label class="control-label">First name </label> - <input ng-model="owner.firstName" /> - <span ng-show="owner.firstName.$error.required" class="help-inline">First name is required.</span> + <input ng-model="owner.firstName" name="firstName" required/> + <span ng-show="ownerForm.firstName.$error.required" class="help-inline">First name is required.</span> </div> <div class="control-group" id="lastName"> <label class="control-label">Last name </label> - <input ng-model="owner.lastName" /> - <span ng-show="myForm.lastName.$error.required" class="help-inline">Last name is required.</span> + <input ng-model="owner.lastName" name="lastName" required/> + <span ng-show="ownerForm.lastName.$error.required" class="help-inline">Last name is required.</span> </div> <div class="control-group" id="address"> <label class="control-label">Address </label> - <input ng-model="owner.address" /> - <span ng-show="myForm.address.$error.required" class="help-inline">Address is required.</span> + <input ng-model="owner.address" name="address" required/> + <span ng-show="ownerForm.address.$error.required" class="help-inline">Address is required.</span> </div> <div class="control-group" id="city"> <label class="control-label">City </label> - <input ng-model="owner.city" /> - <span ng-show="myForm.city.$error.required" class="help-inline">City is required.</span> + <input ng-model="owner.city" name="city" required/> + <span ng-show="ownerForm.city.$error.required" class="help-inline">City is required.</span> </div> <div class="control-group" id="telephone"> <label class="control-label">Telephone </label> - <input ng-model="owner.telephone" /> - <span ng-show="myForm.telephone.$error.required" class="help-inline">Telephone is required.</span> + <input ng-model="owner.telephone" name="telephone" required/> + <span ng-show="ownerForm.telephone.$error.required" class="help-inline">Telephone is required.</span> </div> <div class="form-actions"> - <button type="submit" ng-click="submitOwnerFindForm()">Submit</button> + <button type="submit" ng-click="submitOwnerForm()">Submit</button> </div> </fieldset> </form> diff --git a/src/main/webapp/scripts/app/owner/ownerSearchForm.html b/src/main/webapp/scripts/app/owner/ownerSearchForm.html index b916755f16b3737a5c5ffafac1a470b1d8761e8f..21e974672ea5ce4fca6b44f6fa6390725aa38651 100644 --- a/src/main/webapp/scripts/app/owner/ownerSearchForm.html +++ b/src/main/webapp/scripts/app/owner/ownerSearchForm.html @@ -1,6 +1,5 @@ <h2>Find Owners</h2> - - <form class="form-horizontal" id="ownerFindForm"> + <form class="form-horizontal" id="ownerFindForm" ng-controller="ownerSearchController"> <fieldset> <div class="control-group" id="lastName"> <label class="control-label">Last name </label>