diff --git a/src/main/java/nl/andrewl/railsignalapi/dao/ComponentRepository.java b/src/main/java/nl/andrewl/railsignalapi/dao/ComponentRepository.java index 862c122..805c7d2 100644 --- a/src/main/java/nl/andrewl/railsignalapi/dao/ComponentRepository.java +++ b/src/main/java/nl/andrewl/railsignalapi/dao/ComponentRepository.java @@ -3,7 +3,9 @@ package nl.andrewl.railsignalapi.dao; import nl.andrewl.railsignalapi.model.RailSystem; import nl.andrewl.railsignalapi.model.component.Component; import nl.andrewl.railsignalapi.model.component.Position; +import org.springframework.data.domain.Page; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @@ -11,7 +13,7 @@ import java.util.List; import java.util.Optional; @Repository -public interface ComponentRepository extends JpaRepository { +public interface ComponentRepository extends JpaRepository, JpaSpecificationExecutor { Optional findByIdAndRailSystemId(long id, long rsId); boolean existsByNameAndRailSystem(String name, RailSystem rs); diff --git a/src/main/java/nl/andrewl/railsignalapi/rest/ComponentsApiController.java b/src/main/java/nl/andrewl/railsignalapi/rest/ComponentsApiController.java index 4ff3345..3b8eead 100644 --- a/src/main/java/nl/andrewl/railsignalapi/rest/ComponentsApiController.java +++ b/src/main/java/nl/andrewl/railsignalapi/rest/ComponentsApiController.java @@ -4,12 +4,15 @@ import lombok.RequiredArgsConstructor; import nl.andrewl.railsignalapi.rest.dto.PathNodeUpdatePayload; import nl.andrewl.railsignalapi.rest.dto.component.in.ComponentPayload; import nl.andrewl.railsignalapi.rest.dto.component.out.ComponentResponse; +import nl.andrewl.railsignalapi.rest.dto.component.out.SimpleComponentResponse; import nl.andrewl.railsignalapi.service.ComponentCreationService; import nl.andrewl.railsignalapi.service.ComponentService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import javax.validation.Valid; import java.util.List; @RestController @@ -24,6 +27,15 @@ public class ComponentsApiController { return componentService.getComponents(rsId); } + @GetMapping(path = "/search") + public Page searchComponents( + @RequestParam(name = "q", required = false) String searchQuery, + @PageableDefault(sort = "name") + Pageable pageable + ) { + return componentService.search(searchQuery, pageable); + } + @GetMapping(path = "/{cId}") public ComponentResponse getComponent(@PathVariable long rsId, @PathVariable long cId) { return componentService.getComponent(rsId, cId); diff --git a/src/main/java/nl/andrewl/railsignalapi/service/ComponentService.java b/src/main/java/nl/andrewl/railsignalapi/service/ComponentService.java index bfcb029..ace76b1 100644 --- a/src/main/java/nl/andrewl/railsignalapi/service/ComponentService.java +++ b/src/main/java/nl/andrewl/railsignalapi/service/ComponentService.java @@ -8,6 +8,9 @@ import nl.andrewl.railsignalapi.model.component.Component; import nl.andrewl.railsignalapi.model.component.PathNode; import nl.andrewl.railsignalapi.rest.dto.PathNodeUpdatePayload; import nl.andrewl.railsignalapi.rest.dto.component.out.ComponentResponse; +import nl.andrewl.railsignalapi.rest.dto.component.out.SimpleComponentResponse; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -38,6 +41,16 @@ public class ComponentService { return ComponentResponse.of(c); } + @Transactional(readOnly = true) + public Page search(String searchQuery, Pageable pageable) { + return componentRepository.findAll((root, query, cb) -> { + if (searchQuery != null && !searchQuery.isBlank()) { + return cb.like(cb.lower(root.get("name")), '%' + searchQuery.toLowerCase() + '%'); + } + return cb.and(); + }, pageable).map(SimpleComponentResponse::new); + } + @Transactional public void removeComponent(long rsId, long componentId) { var c = componentRepository.findByIdAndRailSystemId(componentId, rsId) diff --git a/src/main/java/nl/andrewl/railsignalapi/service/SwitchService.java b/src/main/java/nl/andrewl/railsignalapi/service/SwitchService.java index ffa65b1..c1df8ed 100644 --- a/src/main/java/nl/andrewl/railsignalapi/service/SwitchService.java +++ b/src/main/java/nl/andrewl/railsignalapi/service/SwitchService.java @@ -9,6 +9,9 @@ import nl.andrewl.railsignalapi.model.component.Switch; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +/** + * Service for managing switches. + */ @Service @RequiredArgsConstructor public class SwitchService {