lyf666 2017-02-20 13:36:55 +08:00
parent 822e1b1ab7
commit 131c6c82b5
4 changed files with 155 additions and 4 deletions

View File

@ -74,7 +74,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
<version>1.2.24</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
@ -84,7 +84,7 @@
<dependency>
<groupId>cn.cloudowr</groupId>
<artifactId>sdk</artifactId>
<version>1.1.1</version>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>

View File

@ -1,7 +1,13 @@
package cn.cloudowr.dict;
import cn.cloudowr.sdk.JQueryParameterFilter;
import cn.cloudowr.sdk.jfinal.ErrorInterceptor;
import cn.cloudowr.sdk.jfinal.ErrorRender;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.config.*;
import com.jfinal.core.ActionException;
import com.jfinal.render.Render;
import com.jfinal.template.Engine;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
@ -40,11 +46,11 @@ public class Config extends com.jfinal.config.JFinalConfig{
@Override
public void configInterceptor(Interceptors me) {
me.add(new JQueryParameterFilter());
me.add(new ErrorInterceptor());
}
@Override
public void configHandler(Handlers me) {
}
@Override

View File

@ -2,6 +2,7 @@ package cn.cloudowr.dict;
import cn.cloudowr.sdk.FastJSONUtil;
import cn.cloudowr.sdk.IdInterceptor;
import cn.cloudowr.sdk.jfinal.RestfulStyle;
import com.jfinal.aop.Before;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
@ -21,11 +22,128 @@ import java.util.Map;
/**
* Created by lyf66 on 2017/2/16.
*/
public class Controller extends com.jfinal.core.Controller {
public class Controller 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) {
document = Document.parse(json);
}
List<Bson> filter = new ArrayList<>();
if (document != null) {
document.forEach((key, value) -> {
filter.add(Filters.eq(key, value));
});
}
List<Document> result = new ArrayList<>();
if (filter.size() != 0) {
getCollection().find(Filters.and(filter)).skip(pageSize * (pageNum - 1)).limit(pageSize).into(result);
} else {
getCollection().find().skip(pageSize * (pageNum - 1)).limit(pageSize).into(result);
}
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;
}
Document filterDoc = document.get("filter", Document.class);
List<Bson> filterOps = new ArrayList<>();
filterDoc.forEach((key, value) -> {
if ("id".equals(key) || "_id".equals(key)) return;
filterOps.add(Filters.eq(key, value));
});
Document updateDoc = document.get("update", Document.class);
List<Bson> updateOps = new ArrayList<>();
Document $set = updateDoc.get("$set", Document.class);
if ($set != null) {
$set.forEach((key, value) -> {
updateOps.add(Updates.set(key, value));
});
}
List $unset = updateDoc.get("$unset", List.class);
if ($unset != null) {
for (Object o : $unset) {
updateOps.add(Updates.unset(o.toString()));
}
}
Document $rename = updateDoc.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 void dict() {
Map<String, String[]> paraMap = getParaMap();
List<Bson> ops = new ArrayList<>();
@ -143,4 +261,5 @@ public class Controller extends com.jfinal.core.Controller {
MongoCollection<Document> collection = mongoDataBase.getCollection("dict");
return collection;
}
}

View File

@ -1,3 +1,29 @@
index (
pageSize optional default 20,
pageNum optional default 1,
json optional
) {
return dict list json
}
show (id required) {
return dict json
}
save (json required) {
return saved dict json with id
}
update (id required, json required) {
return {result : true or false}
}
delete (id required) {
return {result: true or false}
}
@Deprecated
dict (key-value pairs) {
return dict list json
}