Skip to content

Commit

Permalink
Readability improvements & Use stream instread of forEach (spring-pro…
Browse files Browse the repository at this point in the history
…jects#1055)

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

* Improve controllers readability

* Rollback stream instead of for-each
  • Loading branch information
ckyeon authored Sep 24, 2022
1 parent 7e91b98 commit f48227a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

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;
Expand Down Expand Up @@ -74,10 +72,9 @@ public String processCreationForm(@Valid Owner owner, BindingResult result) {
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")
Expand All @@ -89,7 +86,6 @@ public String initFindForm(Map<String, Object> model) {
@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
Expand All @@ -102,15 +98,15 @@ public String processFindForm(@RequestParam(defaultValue = "1") int page, Owner
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) {
Expand All @@ -124,11 +120,9 @@ private String addPaginationModel(int page, Model model, Page<Owner> paginated)
}

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")
Expand All @@ -144,11 +138,10 @@ public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
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}";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,15 +85,15 @@ public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult res
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")
Expand All @@ -105,11 +109,10 @@ public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owne
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}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ public void setAllowedFields(WebDataBinder dataBinder) {
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;
Expand All @@ -71,7 +73,7 @@ public Visit loadPetWithVisit(@PathVariable("ownerId") int ownerId, @PathVariabl
// 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";
}

Expand All @@ -83,11 +85,10 @@ public String processNewVisitForm(@ModelAttribute Owner owner, @PathVariable int
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}";
}

}

0 comments on commit f48227a

Please sign in to comment.