Skip to content

Commit

Permalink
Merge pull request #607 from bounswe/feature/BE-606-improve-resource-…
Browse files Browse the repository at this point in the history
…filtering

Filtering Resource by Receiver Id
  • Loading branch information
alitpc25 authored Dec 24, 2023
2 parents 4c35d75 + 8c34c3e commit 9a5dbfc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ public ResponseEntity<List<ResourceDto>> filterByCategory(@RequestParam(required
@RequestParam(required = false) BigDecimal longitude,
@RequestParam(required = false) BigDecimal latitude,
@RequestParam(required = false) Long userId,
@RequestParam(required = false) EResourceStatus status){
@RequestParam(required = false) EResourceStatus status,
@RequestParam(required = false) Long receiverId){
log.info("Filtering resources by category");
return resourceService.filterResource(latitude, longitude, categoryTreeId, userId, status);
return resourceService.filterResource(latitude, longitude, categoryTreeId, userId, status, receiverId);
}

@PreAuthorize("hasRole('COORDINATOR') or hasRole('VICTIM') or hasRole('RESPONDER')")
Expand All @@ -81,8 +82,9 @@ public ResponseEntity<List<ResourceDto>> filterByCategoryRectangularScope(@Reque
@RequestParam(required = false) BigDecimal latitude1,
@RequestParam(required = false) BigDecimal longitude2,
@RequestParam(required = false) BigDecimal latitude2,
@RequestParam(required = false) Long userId) {
@RequestParam(required = false) Long userId,
@RequestParam(required = false) Long receiverId) {
log.info("Filtering resources by category, rectangular scope");
return resourceService.filterResourceRectangularScope(latitude1, longitude1, latitude2, longitude2, categoryTreeId, userId);
return resourceService.filterResourceRectangularScope(latitude1, longitude1, latitude2, longitude2, categoryTreeId, userId, receiverId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public ResponseEntity<String> deleteResource(Long resourceId){
return ResponseEntity.ok("Resource deleted successfully");
}

public ResponseEntity<List<ResourceDto>> filterResource(BigDecimal latitude, BigDecimal longitude, String categoryTreeId, Long userId, EResourceStatus status){
public ResponseEntity<List<ResourceDto>> filterResource(BigDecimal latitude, BigDecimal longitude, String categoryTreeId, Long userId, EResourceStatus status, Long receiverId){
Specification<Resource> spec = Specification.where(null);

if (longitude != null && latitude != null) {
Expand All @@ -112,14 +112,17 @@ public ResponseEntity<List<ResourceDto>> filterResource(BigDecimal latitude, Big
if (status != null){
spec = spec.and(ResourceSpecifications.hasStatus(status));
}
if (receiverId != null){
spec = spec.and(ResourceSpecifications.hasReceiverId(receiverId));
}
return ResponseEntity.ok(resourceRepository.findAll(spec).stream().map(resource -> resourceConverter.convertToDto(resource)).toList());

}
public ResponseEntity<List<ResourceDto>> filterByDistance(BigDecimal latitude, BigDecimal longitude, BigDecimal distance){
return ResponseEntity.ok(resourceRepository.filterByDistance(latitude, longitude, distance).stream().map(resource -> resourceConverter.convertToDto(resource)).toList());
}

public ResponseEntity<List<ResourceDto>> filterResourceRectangularScope(BigDecimal latitude1, BigDecimal longitude1, BigDecimal latitude2, BigDecimal longitude2, String categoryTreeId, Long userId){
public ResponseEntity<List<ResourceDto>> filterResourceRectangularScope(BigDecimal latitude1, BigDecimal longitude1, BigDecimal latitude2, BigDecimal longitude2, String categoryTreeId, Long userId, Long receiverId){
Specification<Resource> spec = Specification.where(null);

if (longitude1 != null && latitude1 != null && longitude2 != null && latitude2 != null) {
Expand All @@ -133,6 +136,9 @@ public ResponseEntity<List<ResourceDto>> filterResourceRectangularScope(BigDecim
if (userId != null) {
spec = spec.and(ResourceSpecifications.hasOwnerId(userId));
}
if (receiverId != null){
spec = spec.and(ResourceSpecifications.hasReceiverId(receiverId));
}
return ResponseEntity.ok(resourceRepository.findAll(spec).stream().map(resource -> resourceConverter.convertToDto(resource)).toList());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ public static Specification<Resource> hasStatus(EResourceStatus status){
return (root, query, builder) -> builder.equal(root.get("status"), status);
}

public static Specification<Resource> hasReceiverId(Long receiverId){
return (root, query, builder) -> builder.equal(root.get("receiver").get("id"), receiverId);
}

}

0 comments on commit 9a5dbfc

Please sign in to comment.