From ce464289a0a5e863007d1fe4d5df5ae2968f282c 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 | 24 ++++++++++++ .../httpmockserver/ApiManageController.java | 5 ++- .../ApiManageRestController.java | 39 +++++++++++-------- .../eactive/httpmockserver/ApiService.java | 6 +++ .../httpmockserver/ApiServiceImpl.java | 25 ++++++++++++ .../resources/templates/page/list-api.html | 25 +++++++++++- 6 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/eactive/httpmockserver/ApiDao.java b/src/main/java/com/eactive/httpmockserver/ApiDao.java index a58ca9a..9879b9e 100644 --- a/src/main/java/com/eactive/httpmockserver/ApiDao.java +++ b/src/main/java/com/eactive/httpmockserver/ApiDao.java @@ -1,9 +1,33 @@ package com.eactive.httpmockserver; +import java.util.List; + +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); + + @Query(value = "SELECT ai.apiGroupName FROM ApiInfo ai GROUP BY ai.apiGroupName") + List findAllApiGroupName(); } diff --git a/src/main/java/com/eactive/httpmockserver/ApiManageController.java b/src/main/java/com/eactive/httpmockserver/ApiManageController.java index 3700125..4044e13 100644 --- a/src/main/java/com/eactive/httpmockserver/ApiManageController.java +++ b/src/main/java/com/eactive/httpmockserver/ApiManageController.java @@ -4,7 +4,6 @@ import java.util.Optional; import javax.validation.Valid; -import com.eactive.httpmockserver.script.ScriptInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpMethod; @@ -17,6 +16,8 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import com.eactive.httpmockserver.script.ScriptInfoService; + @Controller public class ApiManageController { @Autowired @@ -30,7 +31,7 @@ public class ApiManageController { @GetMapping("/manage/findAllApis") public String getAllApis(Pageable pageable, Model model) { - //model.addAttribute("apiInfoList", apiService.findAllApis(pageable)); + model.addAttribute("apiGroupNameList", apiService.findAllApiGroupName()); return "page/list-api"; } 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..c465b80 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); @@ -22,4 +26,6 @@ public interface ApiService { public void delete(Long id); public long getRecordsCount(Example example); + + public List findAllApiGroupName(); } diff --git a/src/main/java/com/eactive/httpmockserver/ApiServiceImpl.java b/src/main/java/com/eactive/httpmockserver/ApiServiceImpl.java index 6b89a1f..28c0885 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,18 @@ 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); + } + } + + @Override + public List findAllApiGroupName() { + return apiDao.findAllApiGroupName(); + } } diff --git a/src/main/resources/templates/page/list-api.html b/src/main/resources/templates/page/list-api.html index cbc1666..cf1d03f 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,16 @@
+ + @@ -103,6 +119,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 +161,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