diff --git a/pom.xml b/pom.xml
index dd1c0b2..3e0b319 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
cn.cloudowr
sdk
- 1.2.4
+ 1.3.4
diff --git a/src/main/java/cn/cloudowr/sdk/jfinal/AbsController.java b/src/main/java/cn/cloudowr/sdk/jfinal/AbsController.java
new file mode 100644
index 0000000..31733b2
--- /dev/null
+++ b/src/main/java/cn/cloudowr/sdk/jfinal/AbsController.java
@@ -0,0 +1,202 @@
+package cn.cloudowr.sdk.jfinal;
+
+import com.jfinal.aop.Before;
+import com.mongodb.client.FindIterable;
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.model.Filters;
+import com.mongodb.client.model.Projections;
+import com.mongodb.client.model.Sorts;
+import com.mongodb.client.model.Updates;
+import com.mongodb.client.result.DeleteResult;
+import com.mongodb.client.result.UpdateResult;
+import org.bson.Document;
+import org.bson.conversions.Bson;
+import org.bson.types.ObjectId;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * Created by lyf66 on 2017/2/20.
+ */
+public abstract class AbsController extends cn.cloudowr.sdk.jfinal.Controller implements RestfulStyle {
+
+ @Override
+ public void index() {
+ int pageSize = getParaToInt("pageSize", 20);
+ int pageNum = getParaToInt("pageNum", 1);
+ String json = getPara("json");
+ Document document = null;
+ if (json != null) {
+ try {
+ document = Document.parse(json);
+ } catch (Exception e) {
+ renderError(400, "invalid json");
+ return;
+ }
+ }
+ String projStr = getPara("proj");
+ Document projDoc = null;
+ if (projStr != null) {
+ try {
+ projDoc = Document.parse(projStr);
+ } catch (Exception e) {
+ renderError(400, "invalid proj json");
+ return;
+ }
+ }
+ String sortStr = getPara("sort");
+ Document sortDoc = null;
+ if (sortStr != null) {
+ try {
+ sortDoc = Document.parse(sortStr);
+ } catch (Exception e) {
+ renderError(400, "invalid sort json");
+ return;
+ }
+ }
+
+ List filter = new ArrayList<>();
+ if (document != null) {
+ document.forEach((key, value) -> {
+ if ("$ex".equals(value)) {
+ filter.add(Filters.exists(key));
+ } else if ("$nex".equals(value)) {
+ filter.add(Filters.exists(key, false));
+ } else if (value != null && value.toString().startsWith("$re")) {
+ String s = value.toString();
+ filter.add(Filters.regex(key, Pattern.compile(s.substring(3, s.length()))));
+ } else {
+ filter.add(Filters.eq(key, value));
+ }
+ });
+ }
+
+ List documents = new ArrayList<>();
+ MongoCollection collection = getCollection();
+ long total;
+ FindIterable findIterable;
+ if (filter.size() != 0) {
+ total = collection.count(Filters.and(filter));
+ findIterable = collection.find(Filters.and(filter));
+ } else {
+ total = collection.count();
+ findIterable = collection.find();
+ }
+ int mod = (int) (total % pageSize);
+ long totalPage = mod == 0 ? total / pageSize : total / pageSize + 1;
+ findIterable.skip(pageSize * (pageNum - 1)).limit(pageSize);
+ if (projDoc != null) {
+ List projs = new ArrayList<>();
+ projDoc.forEach((key, val) -> {
+ if ("id".equals(key)) {
+ projs.add(Projections.excludeId());
+ } else if (1 == (int) val) {
+ projs.add(Projections.include(key));
+ } else if (0 == (int) val) {
+ projs.add(Projections.exclude(key));
+ }
+ });
+ findIterable.projection(Projections.fields(projs));
+ }
+ if (sortDoc != null) {
+ List sorts = new ArrayList<>();
+ sortDoc.forEach((key, val) -> {
+ if ("id".equals(key)) key = "_id";
+ if ("asc".equals(val)) {
+ sorts.add(Sorts.ascending(key));
+ } else if ("desc".equals(val)) {
+ sorts.add(Sorts.descending(key));
+ }
+ });
+ findIterable.sort(Sorts.orderBy(sorts));
+ }
+
+ findIterable.into(documents);
+ Document result = new Document();
+ result.put("documents", documents);
+ result.put("total", total);
+ result.put("totalPage", totalPage);
+ renderJson(result);
+ }
+
+ @Override
+ @Before(IdInterceptor.class)
+ public void show() {
+ Document document = getCollection().find(Filters.eq("_id", new ObjectId(getPara("id")))).first();
+ renderJson(document);
+ }
+
+ @Override
+ public void save() {
+ String json = getPara("json");
+ Document document = null;
+ try {
+ document = Document.parse(json);
+ } catch (Exception e) {
+ renderError(400, "invalid json");
+ return;
+ }
+ getCollection().insertOne(document);
+ renderJson(document);
+ }
+
+ @Override
+ @Before(IdInterceptor.class)
+ public void update() {
+ String id = getPara("id");
+ String json = getPara("json");
+
+ Document document = null;
+ try {
+ document = Document.parse(json);
+ } catch (Exception e) {
+ renderError(400, "invalid json");
+ return;
+ }
+
+ List updateOps = new ArrayList<>();
+ Document $set = document.get("$set", Document.class);
+ if ($set != null) {
+ $set.forEach((key, value) -> {
+ updateOps.add(Updates.set(key, value));
+ });
+ }
+ List $unset = document.get("$unset", List.class);
+ if ($unset != null) {
+ for (Object o : $unset) {
+ updateOps.add(Updates.unset(o.toString()));
+ }
+ }
+ Document $rename = document.get("$rename", Document.class);
+ if ($rename != null) {
+ $rename.forEach((oldKey, newKey) -> {
+ updateOps.add(Updates.rename(oldKey, newKey.toString()));
+ });
+ }
+ Bson combine = Updates.combine(updateOps);
+ UpdateResult updateResult = getCollection().updateOne(
+ Filters.eq("_id", new ObjectId(id)),
+ combine
+ );
+ renderJson("result", updateResult.getMatchedCount() >= 1);
+ }
+
+ @Override
+ @Before(IdInterceptor.class)
+ public void delete() {
+ String id = getPara("id");
+
+ DeleteResult deleteResult = getCollection().deleteOne(
+ Filters.eq("_id", new ObjectId(id))
+ );
+ renderJson("result", deleteResult.getDeletedCount() == 1);
+ }
+
+ public void ping() {
+ renderText("dict pong");
+ }
+
+ public abstract MongoCollection getCollection();
+}