Readability improvements & Use stream instread of forEach (#1055)

* Use stream instead of forEach on find PetType logic of parse()

* Improve controllers readability

* Rollback stream instead of for-each
This commit is contained in:
Kiyeon Cho 2022-09-24 14:40:31 +09:00 committed by GitHub
parent 7e91b98f71
commit f48227aea0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 38 deletions

View file

@ -17,9 +17,7 @@ package org.springframework.samples.petclinic.owner;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@ -74,10 +72,9 @@ class OwnerController {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
else {
this.owners.save(owner);
return "redirect:/owners/" + owner.getId();
}
this.owners.save(owner);
return "redirect:/owners/" + owner.getId();
}
@GetMapping("/owners/find")
@ -89,7 +86,6 @@ class OwnerController {
@GetMapping("/owners")
public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner owner, BindingResult result,
Model model) {
// allow parameterless GET request for /owners to return all records
if (owner.getLastName() == null) {
owner.setLastName(""); // empty string signifies broadest possible search
@ -102,15 +98,15 @@ class OwnerController {
result.rejectValue("lastName", "notFound", "not found");
return "owners/findOwners";
}
else if (ownersResults.getTotalElements() == 1) {
if (ownersResults.getTotalElements() == 1) {
// 1 owner found
owner = ownersResults.iterator().next();
return "redirect:/owners/" + owner.getId();
}
else {
// multiple owners found
return addPaginationModel(page, model, ownersResults);
}
// multiple owners found
return addPaginationModel(page, model, ownersResults);
}
private String addPaginationModel(int page, Model model, Page<Owner> paginated) {
@ -124,11 +120,9 @@ class OwnerController {
}
private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) {
int pageSize = 5;
Pageable pageable = PageRequest.of(page - 1, pageSize);
return owners.findByLastName(lastname, pageable);
}
@GetMapping("/owners/{ownerId}/edit")
@ -144,11 +138,10 @@ class OwnerController {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
else {
owner.setId(ownerId);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
owner.setId(ownerId);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
/**

View file

@ -15,15 +15,19 @@
*/
package org.springframework.samples.petclinic.owner;
import java.util.Collection;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Collection;
import org.springframework.web.bind.annotation.GetMapping;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author Juergen Hoeller
@ -81,15 +85,15 @@ class PetController {
if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
result.rejectValue("name", "duplicate", "already exists");
}
owner.addPet(pet);
if (result.hasErrors()) {
model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
}
else {
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
@GetMapping("/pets/{petId}/edit")
@ -105,11 +109,10 @@ class PetController {
model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
}
else {
owner.addPet(pet);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
owner.addPet(pet);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
}

View file

@ -60,9 +60,11 @@ class VisitController {
public Visit loadPetWithVisit(@PathVariable("ownerId") int ownerId, @PathVariable("petId") int petId,
Map<String, Object> model) {
Owner owner = this.owners.findById(ownerId);
Pet pet = owner.getPet(petId);
model.put("pet", pet);
model.put("owner", owner);
Visit visit = new Visit();
pet.addVisit(visit);
return visit;
@ -71,7 +73,7 @@ class VisitController {
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is
// called
@GetMapping("/owners/{ownerId}/pets/{petId}/visits/new")
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
public String initNewVisitForm() {
return "pets/createOrUpdateVisitForm";
}
@ -83,11 +85,10 @@ class VisitController {
if (result.hasErrors()) {
return "pets/createOrUpdateVisitForm";
}
else {
owner.addVisit(petId, visit);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
owner.addVisit(petId, visit);
this.owners.save(owner);
return "redirect:/owners/{ownerId}";
}
}