Skip to content
Snippets Groups Projects
Commit fedb7d43 authored by michaelisvy's avatar michaelisvy
Browse files

Merged OwnerListResource and OwnerResource

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
parent e4931777
Branches
No related tags found
No related merge requests found
/*
* 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;
}
}
}
......@@ -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;
}
}
}
......@@ -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;
......
<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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment