api 검색 옵션 추가

This commit is contained in:
hsoh
2022-11-15 12:20:03 +09:00
parent aa15af7ab9
commit ce464289a0
6 changed files with 104 additions and 20 deletions
@@ -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, Long> {
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<ApiInfo> 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<String> findAllApiGroupName();
}
@@ -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";
}
@@ -70,23 +70,28 @@ public class ApiManageRestController {
public DataTablesResponse<ApiInfo> getAllApisForDatatables(@RequestBody DataTablesRequest req) {
Pageable pageable = assignJpaPageable(req);
// search option
Example<ApiInfo> 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<ApiInfo> res = new DataTablesResponse<ApiInfo>(apiService.findApis(example, pageable));
res.setRecordsFiltered(apiService.getRecordsCount(example));
// // search option
// Example<ApiInfo> 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<ApiInfo> res = new DataTablesResponse<ApiInfo>(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<ApiInfo> res = new DataTablesResponse<ApiInfo>(
apiService.findApis(apiGroupName, searchStr, pageable));
res.setRecordsFiltered(apiService.countApis(apiGroupName, searchStr));
res.setRecordsTotal(apiService.getRecordsCount(null));
res.setDraw(req.getDraw());
return res;
@@ -15,6 +15,10 @@ public interface ApiService {
public List<ApiInfo> findApis(Example<ApiInfo> example, Pageable pageable);
public List<ApiInfo> 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<ApiInfo> example);
public List<String> findAllApiGroupName();
}
@@ -52,6 +52,17 @@ public class ApiServiceImpl implements ApiService {
}
}
@Override
public List<ApiInfo> findApis(String apiGroupName, String searchStr, Pageable pageable) {
Page<ApiInfo> pagedResult = apiDao.findApisByParameters(apiGroupName, searchStr, searchStr, searchStr,
pageable);
if (pagedResult.hasContent()) {
return pagedResult.getContent();
} else {
return new ArrayList<ApiInfo>();
}
}
@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<String> findAllApiGroupName() {
return apiDao.findAllApiGroupName();
}
}
@@ -11,6 +11,12 @@
th:href="@{/plugins/datatables-responsive/css/responsive.bootstrap4.min.css}">
<link rel="stylesheet"
th:href="@{/plugins/datatables-buttons/css/buttons.bootstrap4.min.css}">
<style type="text/css" class="init">
.search-ext {
float:right;
}
</style>
</th:block>
<div class="content-wrapper" layout:fragment="content">
@@ -66,6 +72,16 @@
<!-- /.row -->
</div>
<!-- /.container-fluid -->
<div id="api_search" style="display:none">
<label>
&nbsp; &nbsp; API그룹:
<select id="searchApiGroupName" style="margin-left: 0.5em; height: calc(1.8125rem + 2px); border: 1px solid #ced4da; border-radius: 0.2rem;">
<option value="">-- 선택 --</option>
<option th:each="apiGroupName : ${apiGroupNameList}" th:value="${apiGroupName}" th:text="${apiGroupName}"></option>
</select>
</label>
</div>
</section>
</div>
@@ -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