package com.codecademy.plants.repositories; import java.util.List; import org.springframework.data.repository.CrudRepository; import com.codecademy.plants.entities.Plant; public interface PlantRepository extends CrudRepository<Plant, Integer> { List<Plant> findByHasFruitTrue(); List<Plant> findByHasFruitFalse(); List<Plant> findByQuantityLessThan(Integer quantity); List<Plant> findByHasFruitTrueAndQuantityLessThan(Integer quantity); List<Plant> findByHasFruitFalseAndQuantityLessThan(Integer quantity); } //Controller @GetMapping("/plants/search") public List<Plant> searchPlants( @RequestParam(name="hasFruit", required = false) Boolean hasFruit, @RequestParam(name="maxQuantity", required = false) Integer quantity ) { if (hasFruit != null && quantity != null && hasFruit) { return this.plantRepository.findByHasFruitTrueAndQuantityLessThan(quantity); } if (hasFruit != null && quantity != null && !hasFruit) { return this.plantRepository.findByHasFruitFalseAndQuantityLessThan(quantity); } if (hasFruit != null && hasFruit) { return this.plantRepository.findByHasFruitTrue(); } if (hasFruit != null && !hasFruit) { return this.plantRepository.findByHasFruitFalse(); } if (quantity != null) { return this.plantRepository.findByQuantityLessThan(quantity); } 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