Skip to content

Commit

Permalink
#44
Browse files Browse the repository at this point in the history
  • Loading branch information
xlorne committed Apr 24, 2024
1 parent 5040bd6 commit 5c448ff
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>

<url>https://github.com/codingapi/springboot-framewrok</url>
<name>springboot-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-data-fast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.2.0</version>
<version>3.2.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.2.0</version>
<version>3.2.1</version>
</parent>

<artifactId>springboot-starter-security</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
</parent>
<artifactId>springboot-starter</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
Expand All @@ -15,7 +17,7 @@
import java.util.List;

/**
* HttpServletRequest 请求参数解析成 PageRequest对象
* HttpServletRequest 请求参数解析成 PageRequest对象
*/
public class SearchRequest {

Expand Down Expand Up @@ -50,6 +52,15 @@ public void addSort(Sort sort) {

public void removeFilter(String key) {
pageRequest.removeFilter(key);
this.removeKeys.add(key);
}

public String getParameter(String key) {
return request.getParameter(key);
}

public String[] getParameterValues(String key) {
return request.getParameterValues(key);
}

public PageRequest addFilter(String key, Relation relation, Object... value) {
Expand All @@ -69,7 +80,6 @@ public PageRequest orFilters(Filter... filters) {
}



private String decode(String value) {
return new String(Base64.getDecoder().decode(value));
}
Expand All @@ -85,14 +95,20 @@ public ClassContent(Class<?> clazz, PageRequest pageRequest) {
this.pageRequest = pageRequest;
}

public void addFilter(String key, Relation relation, String value) {
Class<?> keyClass = getKeyType(key);
Object v = parseObject(value, keyClass);
pageRequest.addFilter(key, relation, v);
}

public void addFilter(String key, String value) {
Class<?> keyClass = getKeyType(key);
Object v = parseObject(value, keyClass);
pageRequest.addFilter(key, Relation.EQUAL, v);
}

private Object parseObject(String value, Class<?> keyClass) {
if(value.getClass().equals(keyClass)) {
if (value.getClass().equals(keyClass)) {
return value;
}
return JSON.parseObject(value, keyClass);
Expand Down Expand Up @@ -124,12 +140,37 @@ private Class<?> getKeyType(String key) {

}

@Setter
@Getter
static class ParamOperation {
private String key;
private String type;

public Relation getOperation() {
return Relation.valueOf(type);
}
}

private List<ParamOperation> loadParamOperations() {
String params = request.getParameter("params");
if (StringUtils.hasLength(params)) {
params = decode(params);
if (JSON.isValid(params)) {
removeKeys.add("params");
return JSON.parseArray(params, ParamOperation.class);
}
}
return null;
}

public PageRequest toPageRequest(Class<?> clazz) {
pageRequest.setCurrent(current);
pageRequest.setPageSize(pageSize);

ClassContent content = new ClassContent(clazz, pageRequest);

List<ParamOperation> loadParams = loadParamOperations();

String sort = request.getParameter("sort");
if (StringUtils.hasLength(sort)) {
sort = decode(sort);
Expand Down Expand Up @@ -169,7 +210,19 @@ public PageRequest toPageRequest(Class<?> clazz) {
if (!removeKeys.contains(key)) {
String value = request.getParameter(key);
if (StringUtils.hasLength(value)) {
content.addFilter(key, value);
if (loadParams != null) {
ParamOperation operation = loadParams.stream()
.filter(paramOperation -> paramOperation.getKey().equals(key))
.findFirst()
.orElse(null);
if (operation != null) {
content.addFilter(key, operation.getOperation(), value);
} else {
content.addFilter(key, value);
}
} else {
content.addFilter(key, value);
}
}
}
});
Expand Down

0 comments on commit 5c448ff

Please sign in to comment.