Skip to content

Commit

Permalink
API - Filtering Different filters implemented for buildings.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlperMulayim committed Sep 13, 2023
1 parent 2aa2c9f commit c6f57ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.alper.leasesoftprov2.leasesoft.buildings.mappers.dtos.BuildingDto;
import jakarta.transaction.NotSupportedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
Expand All @@ -20,16 +17,7 @@ public class BuildingController {
public BuildingService service;

@GetMapping("") //add filter, error handling. add pagination
public List<BuildingDto> getAllBuildings(@RequestParam("beds") Optional<Integer> beds,
@RequestParam("baths") Optional<Integer> baths,
@RequestParam("city") Optional<String> city
) {

BuildingFilter filter = BuildingFilter.builder()
.beds(beds)
.baths(baths)
.city(city)
.build();
public List<BuildingDto> getAllBuildings(@RequestBody BuildingFilter filter) {
return this.service.getBuildings(filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public class BuildingFilter {
private Optional<Integer> beds;
private Optional<Integer> baths;
private Optional<String> city;
private Optional<Double> priceFrom;
private Optional<Double> priceTo;
private Optional<Double> sizeTo;
private Optional<Double> sizeFrom;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,52 @@ public Specification<Building> filterBy(BuildingFilter filter){
return Specification
.where(hasBeds(filter.getBeds()))
.and(hasBaths(filter.getBaths()))
.and(inCity(filter.getCity()));
.and(inCity(filter.getCity()))
.and(priceFrom(filter.getPriceFrom()))
.and(priceTo(filter.getPriceTo()))
.and(sizeTo(filter.getSizeTo()))
.and(sizeFrom(filter.getSizeFrom()));
}

Specification<Building> hasBeds(Optional<Integer> beds){
return (((root, query, criteriaBuilder) -> beds.isPresent() ?
return (((root, query, criteriaBuilder) -> beds != null && beds.isPresent() ?
criteriaBuilder.equal(root.get("bedrooms"),beds.get()):
criteriaBuilder.conjunction()));
}

Specification<Building> hasBaths(Optional<Integer> baths){
return (((root, query, criteriaBuilder) -> baths.isPresent() ?
return (((root, query, criteriaBuilder) -> baths!=null && baths.isPresent() ?
criteriaBuilder.equal(root.get("bathrooms"),baths.get()):
criteriaBuilder.conjunction() ));
}

Specification<Building> inCity(Optional<String> city){
return (((root, query, criteriaBuilder) -> city.isPresent() && !city.get().isEmpty() ?
return (((root, query, criteriaBuilder) -> city!=null && city.isPresent() && !city.get().isEmpty() ?
criteriaBuilder.equal(root.get("city"),city.get()):
criteriaBuilder.conjunction()));
}

Specification<Building> priceFrom(Optional<Double> priceFrom){
return (((root, query, criteriaBuilder) -> priceFrom !=null && priceFrom.isPresent() ?
criteriaBuilder.greaterThanOrEqualTo(root.get("listing").get("price"),priceFrom.get()) :
criteriaBuilder.conjunction()));
}

Specification<Building> priceTo(Optional<Double> priceTo){
return (((root, query, criteriaBuilder) -> priceTo !=null && priceTo.isPresent() ?
criteriaBuilder.lessThanOrEqualTo(root.get("listing").get("price"),priceTo.get()) :
criteriaBuilder.conjunction()));
}

Specification<Building> sizeTo(Optional<Double> sizeTo){
return (((root, query, criteriaBuilder) -> sizeTo !=null && sizeTo.isPresent() ?
criteriaBuilder.lessThanOrEqualTo(root.get("size"),sizeTo.get()) :
criteriaBuilder.conjunction()));
}

Specification<Building> sizeFrom(Optional<Double> sizeFrom){
return (((root, query, criteriaBuilder) -> sizeFrom !=null && sizeFrom.isPresent() ?
criteriaBuilder.greaterThanOrEqualTo(root.get("size"),sizeFrom.get()) :
criteriaBuilder.conjunction()));
}
}

0 comments on commit c6f57ca

Please sign in to comment.