package com.codecademy.people.repositories; import java.util.List; import org.springframework.data.repository.CrudRepository; import com.codecademy.people.entities.Person; //Repository folder public interface PersonRepository extends CrudRepository<Person, Integer> { // this declaration is all we need! List<Person> findByEyeColor(String eyeColor); List<Person> findByAgeLessThan(Integer age); } //Controller folder @GetMapping("/people/search") public List<Person> searchPeople(@RequestParam(name = "eyeColor", required = false) String eyeColor) { if (eyeColor != null) { return this.personRepository.findByEyeColor(eyeColor) } else { return new ArrayList<>(); } } //Advanced Controller folder @GetMapping("/people/search") public List<Person> searchPeople( @RequestParam(name = "eyeColor", required = false) String eyeColor, @RequestParam(name = "maxAge", required = false) Integer maxAge ) { if (eyeColor != null) { return this.personRepository.findByEyeColor(eyeColor) } else if (maxAge != null) { return this.personRepository.findByAgeLessThan(maxAge); } else { return new ArrayList<>(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter