From 0c2f38ef7afb31bcad575a39a5be44d3740ae5dc Mon Sep 17 00:00:00 2001 From: Alexey Zinchenko Date: Wed, 8 May 2019 02:01:17 +0300 Subject: [PATCH] Added autocomplete for order edit/create page. --- pom.xml | 7 ++- .../api/AutocompleteApiController.java | 36 +++++++++++++++ .../repository/ClientRepository.java | 4 ++ .../repository/MechanicRepository.java | 4 ++ .../carrepair/service/ClientService.java | 4 ++ .../carrepair/service/MechanicService.java | 4 ++ src/main/resources/static/css/main.css | 9 +++- src/main/resources/templates/main.html | 7 ++- src/main/resources/templates/order/edit.html | 45 ++++++++++++++++--- 9 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/github/prominence/carrepair/controller/api/AutocompleteApiController.java diff --git a/pom.xml b/pom.xml index 8dc9bbb..05e4d63 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.3.RELEASE + 2.1.4.RELEASE com.github.prominence @@ -75,6 +75,11 @@ jquery 3.3.1 + + org.webjars + jQuery-Autocomplete + 1.4.9 + diff --git a/src/main/java/com/github/prominence/carrepair/controller/api/AutocompleteApiController.java b/src/main/java/com/github/prominence/carrepair/controller/api/AutocompleteApiController.java new file mode 100644 index 0000000..2e9405d --- /dev/null +++ b/src/main/java/com/github/prominence/carrepair/controller/api/AutocompleteApiController.java @@ -0,0 +1,36 @@ +package com.github.prominence.carrepair.controller.api; + +import com.github.prominence.carrepair.model.Client; +import com.github.prominence.carrepair.model.Mechanic; +import com.github.prominence.carrepair.service.ClientService; +import com.github.prominence.carrepair.service.MechanicService; +import org.springframework.http.MediaType; +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 java.util.List; + +@RestController +@RequestMapping("/api/autocomplete") +public class AutocompleteApiController { + + private ClientService clientService; + private MechanicService mechanicService; + + public AutocompleteApiController(ClientService clientService, MechanicService mechanicService) { + this.clientService = clientService; + this.mechanicService = mechanicService; + } + + @GetMapping(value = "/client", produces = MediaType.APPLICATION_JSON_VALUE) + public List clientAutocomplete(@RequestParam String query) { + return clientService.searchByInitials(query); + } + + @GetMapping(value = "/mechanic", produces = MediaType.APPLICATION_JSON_VALUE) + public List mechanicAutocomplete(@RequestParam String query) { + return mechanicService.searchByInitials(query); + } +} diff --git a/src/main/java/com/github/prominence/carrepair/repository/ClientRepository.java b/src/main/java/com/github/prominence/carrepair/repository/ClientRepository.java index 22d4fe7..f8ee66f 100644 --- a/src/main/java/com/github/prominence/carrepair/repository/ClientRepository.java +++ b/src/main/java/com/github/prominence/carrepair/repository/ClientRepository.java @@ -3,6 +3,7 @@ package com.github.prominence.carrepair.repository; import com.github.prominence.carrepair.model.Client; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -12,4 +13,7 @@ public interface ClientRepository extends JpaRepository { @Query(value = "select c.id from Client c") public List findAllClientIds(); + + @Query(value = "select c from Client c where c.firstName like concat('%', :query, '%') or c.middleName like concat('%', :query, '%') or c.lastName like concat('%', :query, '%')") + public List findAllByInitials(@Param(value = "query") String query); } diff --git a/src/main/java/com/github/prominence/carrepair/repository/MechanicRepository.java b/src/main/java/com/github/prominence/carrepair/repository/MechanicRepository.java index e023f3f..6fb11c6 100644 --- a/src/main/java/com/github/prominence/carrepair/repository/MechanicRepository.java +++ b/src/main/java/com/github/prominence/carrepair/repository/MechanicRepository.java @@ -3,6 +3,7 @@ package com.github.prominence.carrepair.repository; import com.github.prominence.carrepair.model.Mechanic; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -12,4 +13,7 @@ public interface MechanicRepository extends JpaRepository { @Query(value = "select m.id from Mechanic m") public List findAllMechanicIds(); + + @Query(value = "select m from Mechanic m where m.firstName like concat('%', :query, '%') or m.middleName like concat('%', :query, '%') or m.lastName like concat('%', :query, '%')") + public List findAllByInitials(@Param(value = "query") String query); } diff --git a/src/main/java/com/github/prominence/carrepair/service/ClientService.java b/src/main/java/com/github/prominence/carrepair/service/ClientService.java index f367a03..9dfed06 100644 --- a/src/main/java/com/github/prominence/carrepair/service/ClientService.java +++ b/src/main/java/com/github/prominence/carrepair/service/ClientService.java @@ -46,4 +46,8 @@ public class ClientService { public List getAllClientIds() { return clientRepository.findAllClientIds(); } + + public List searchByInitials(String query) { + return clientRepository.findAllByInitials(query); + } } diff --git a/src/main/java/com/github/prominence/carrepair/service/MechanicService.java b/src/main/java/com/github/prominence/carrepair/service/MechanicService.java index 0ba76b7..e479eec 100644 --- a/src/main/java/com/github/prominence/carrepair/service/MechanicService.java +++ b/src/main/java/com/github/prominence/carrepair/service/MechanicService.java @@ -66,4 +66,8 @@ public class MechanicService { public List getAllMechanicIds() { return mechanicRepository.findAllMechanicIds(); } + + public List searchByInitials(String query) { + return mechanicRepository.findAllByInitials(query); + } } diff --git a/src/main/resources/static/css/main.css b/src/main/resources/static/css/main.css index 5d91a51..d684996 100644 --- a/src/main/resources/static/css/main.css +++ b/src/main/resources/static/css/main.css @@ -11,4 +11,11 @@ body { .btn-group { display: flex; -} \ No newline at end of file +} + +.autocomplete-suggestions { border: 1px solid #999; background: #FFF; overflow: auto; } +.autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; } +.autocomplete-selected { background: #F0F0F0; } +.autocomplete-suggestions strong { font-weight: normal; color: #3399FF; } +.autocomplete-group { padding: 2px 5px; } +.autocomplete-group strong { display: block; border-bottom: 1px solid #000; } \ No newline at end of file diff --git a/src/main/resources/templates/main.html b/src/main/resources/templates/main.html index 8478c76..e669dfa 100644 --- a/src/main/resources/templates/main.html +++ b/src/main/resources/templates/main.html @@ -50,10 +50,13 @@ -
+ -
+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/order/edit.html b/src/main/resources/templates/order/edit.html index 7c7211f..6fba700 100644 --- a/src/main/resources/templates/order/edit.html +++ b/src/main/resources/templates/order/edit.html @@ -17,20 +17,20 @@
- +
- + +
- + +
@@ -71,7 +71,38 @@ + + + + + + \ No newline at end of file