From 98a5467bb321144506505e688aed4ecd317afea9 Mon Sep 17 00:00:00 2001 From: hsoh Date: Tue, 15 Nov 2022 12:20:03 +0900 Subject: [PATCH] =?UTF-8?q?api=20=EA=B2=80=EC=83=89=20=EC=98=B5=EC=85=98?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/eactive/httpmockserver/ApiDao.java | 19 +++++++++ .../ApiManageRestController.java | 39 +++++++++++-------- .../eactive/httpmockserver/ApiService.java | 4 ++ .../httpmockserver/ApiServiceImpl.java | 20 ++++++++++ .../resources/templates/page/list-api.html | 26 ++++++++++++- 5 files changed, 90 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/eactive/httpmockserver/ApiDao.java b/src/main/java/com/eactive/httpmockserver/ApiDao.java index a58ca9a..f232da4 100644 --- a/src/main/java/com/eactive/httpmockserver/ApiDao.java +++ b/src/main/java/com/eactive/httpmockserver/ApiDao.java @@ -1,9 +1,28 @@ package com.eactive.httpmockserver; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; 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; @Repository public interface ApiDao extends JpaRepository { ApiInfo findByUrlAndServiceValue(String url, String serviceValue); + + @Query(value = "SELECT ai FROM ApiInfo ai WHERE (:apiGroupName is null or ai.apiGroupName = :apiGroupName)" + + "AND ((:searchStr1 is null or ai.apiGroupName LIKE %:searchStr1%)" + + "OR (:searchStr2 is null or ai.apiName LIKE %:searchStr2%)" + + "OR (:searchStr3 is null or ai.serviceValue LIKE :searchStr3%))") + Page findApisByParameters(@Param("apiGroupName") String apiGroupName, + @Param("searchStr1") String searchStr1, @Param("searchStr2") String searchStr2, + @Param("searchStr3") String searchStr3, Pageable pageable); + + @Query(value = "SELECT COUNT(*) FROM ApiInfo ai WHERE (:apiGroupName is null or ai.apiGroupName = :apiGroupName)" + + "AND ((:searchStr1 is null or ai.apiGroupName LIKE %:searchStr1%)" + + "OR (:searchStr2 is null or ai.apiName LIKE %:searchStr2%)" + + "OR (:searchStr3 is null or ai.serviceValue LIKE :searchStr3%))") + long countApisByParameters(@Param("apiGroupName") String apiGroupName, @Param("searchStr1") String searchStr1, + @Param("searchStr2") String searchStr2, @Param("searchStr3") String searchStr3); } diff --git a/src/main/java/com/eactive/httpmockserver/ApiManageRestController.java b/src/main/java/com/eactive/httpmockserver/ApiManageRestController.java index af50e86..f120a4d 100644 --- a/src/main/java/com/eactive/httpmockserver/ApiManageRestController.java +++ b/src/main/java/com/eactive/httpmockserver/ApiManageRestController.java @@ -70,23 +70,28 @@ public class ApiManageRestController { public DataTablesResponse getAllApisForDatatables(@RequestBody DataTablesRequest req) { Pageable pageable = assignJpaPageable(req); - // search option - Example example = null; - if (StringUtils.isNotBlank(req.getSearch().getValue())) { - ApiInfo apiInfo = new ApiInfo(); - apiInfo.setApiGroupName(req.getSearch().getValue()); - apiInfo.setApiName(req.getSearch().getValue()); - apiInfo.setServiceValue(req.getSearch().getValue()); - - ExampleMatcher matcher = ExampleMatcher.matchingAny() - .withMatcher("apiGroupName", new GenericPropertyMatcher().contains().ignoreCase()) - .withMatcher("apiName", new GenericPropertyMatcher().contains().ignoreCase()) - .withMatcher("serviceValue", new GenericPropertyMatcher().startsWith().ignoreCase()); - example = Example.of(apiInfo, matcher); - } - - DataTablesResponse res = new DataTablesResponse(apiService.findApis(example, pageable)); - res.setRecordsFiltered(apiService.getRecordsCount(example)); +// // search option +// Example example = null; +// if (StringUtils.isNotBlank(req.getSearch().getValue())) { +// ApiInfo apiInfo = new ApiInfo(); +// apiInfo.setApiGroupName(req.getSearch().getValue()); +// apiInfo.setApiName(req.getSearch().getValue()); +// apiInfo.setServiceValue(req.getSearch().getValue()); +// +// ExampleMatcher matcher = ExampleMatcher.matchingAny() +// .withMatcher("apiGroupName", new GenericPropertyMatcher().contains().ignoreCase()) +// .withMatcher("apiName", new GenericPropertyMatcher().contains().ignoreCase()) +// .withMatcher("serviceValue", new GenericPropertyMatcher().startsWith().ignoreCase()); +// example = Example.of(apiInfo, matcher); +// } +// +// DataTablesResponse res = new DataTablesResponse(apiService.findApis(example, pageable)); +// res.setRecordsFiltered(apiService.getRecordsCount(example)); + String apiGroupName = StringUtils.defaultIfBlank(req.getColumns().get(0).getSearch().getValue(), null); + String searchStr = StringUtils.defaultIfBlank(req.getSearch().getValue(), null); + DataTablesResponse res = new DataTablesResponse( + apiService.findApis(apiGroupName, searchStr, pageable)); + res.setRecordsFiltered(apiService.countApis(apiGroupName, searchStr)); res.setRecordsTotal(apiService.getRecordsCount(null)); res.setDraw(req.getDraw()); return res; diff --git a/src/main/java/com/eactive/httpmockserver/ApiService.java b/src/main/java/com/eactive/httpmockserver/ApiService.java index ca1a499..ab75f3e 100644 --- a/src/main/java/com/eactive/httpmockserver/ApiService.java +++ b/src/main/java/com/eactive/httpmockserver/ApiService.java @@ -15,6 +15,10 @@ public interface ApiService { public List findApis(Example example, Pageable pageable); + public List findApis(String apiGroupName, String searchStr, Pageable pageable); + + public long countApis(String apiGroupName, String searchStr); + public ApiInfo update(ApiInfo apiInfo); public ApiInfo findByUrlAndServiceValue(String url, String serviceValue); diff --git a/src/main/java/com/eactive/httpmockserver/ApiServiceImpl.java b/src/main/java/com/eactive/httpmockserver/ApiServiceImpl.java index 6b89a1f..306ec47 100644 --- a/src/main/java/com/eactive/httpmockserver/ApiServiceImpl.java +++ b/src/main/java/com/eactive/httpmockserver/ApiServiceImpl.java @@ -52,6 +52,17 @@ public class ApiServiceImpl implements ApiService { } } + @Override + public List findApis(String apiGroupName, String searchStr, Pageable pageable) { + Page pagedResult = apiDao.findApisByParameters(apiGroupName, searchStr, searchStr, searchStr, + pageable); + if (pagedResult.hasContent()) { + return pagedResult.getContent(); + } else { + return new ArrayList(); + } + } + @Override @Transactional(propagation = Propagation.REQUIRED, readOnly = false) public ApiInfo update(ApiInfo apiInfo) { @@ -77,4 +88,13 @@ public class ApiServiceImpl implements ApiService { return apiDao.count(example); } } + + @Override + public long countApis(String apiGroupName, String searchStr) { + if (apiGroupName == null && searchStr == null) { + return apiDao.count(); + } else { + return apiDao.countApisByParameters(apiGroupName, searchStr, searchStr, searchStr); + } + } } diff --git a/src/main/resources/templates/page/list-api.html b/src/main/resources/templates/page/list-api.html index cbc1666..c1fbfcc 100644 --- a/src/main/resources/templates/page/list-api.html +++ b/src/main/resources/templates/page/list-api.html @@ -11,6 +11,12 @@ th:href="@{/plugins/datatables-responsive/css/responsive.bootstrap4.min.css}"> + +
@@ -66,6 +72,17 @@
+ + @@ -103,6 +120,7 @@ "serverSide": true, "pageLength": 50, "searchDelay": 1200, + "dom": '<"row"<"col-sm-12 col-md-6"B><"col-sm-12 col-md-6"<"search-ext">f>>rt<"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>', "ajax": { "url": "/manage/rest/apis", "type": "POST", @@ -144,9 +162,15 @@ ], "buttons": ["new", "excel", "colvis"], "initComplete": function() { - listApiTable.buttons().container().appendTo('#listApiTable_wrapper .col-md-6:eq(0)'); + //listApiTable.buttons().container().appendTo('#listApiTable_wrapper .col-md-6:eq(0)'); } }); + + $('.search-ext').html($('#api_search').html()); + + $("#searchApiGroupName").change(function(){ + listApiTable.columns(0).search(this.value).draw(); // apiGroupName + }); /* $(".dataTables_filter input").unbind().bind("input", function(e) { // Bind our desired behavior