我的架构演化笔记 5:增加发表博客功能

表弟:“博客分类已经有了,现在总可以来发表博客吧”

我“恩,没问题”

~~~~~~~~~~~~~

package controllers.project0.v1;

import com.fasterxml.jackson.databind.JsonNode;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

import play.mvc.Controller;
import play.mvc.Result;
import controllers.LoginSecurity;
import controllers.MongoConnectionPool;

public class Blog extends Controller {
	public static Result add() {
		// 校验登陆先
		// 校验登陆
		boolean login = LoginSecurity.verify();
		if (login == false) {
			return badRequest("{\"result\":\"fail\",\"desc\":\"please login firstly!\"}");
		}

		// 获取数据,插入到blog里去
		JsonNode body = request().body().asJson();
		String title = body.findPath("title").asText();
		String abst = body.findPath("abstract").asText();
		String content = body.findPath("content").asText();
		String tags = body.findPath("tags").asText();
		String category = body.findPath("category").asText();
		BasicDBObject object = new BasicDBObject();
		object.append("title", title);
		object.append("abstracct", abst);
		object.append("content", content);
		object.append("tags", tags);
		object.append("category", category);

		MongoClient con = MongoConnectionPool.getConnection();
		DB db = con.getDB("test");
		DBCollection collection = db.getCollection("blog");
		System.out.println(object.toString());
		WriteResult result = collection.insert(object, WriteConcern.SAFE);
		/*
		if(null == result) System.out.println("result is null");
		else{
			//System.out.println("affected---"+result.getN());
			//System.out.println(result.getUpsertedId());;
		}
		
		if (result != null && result.getUpsertedId()!=null) {
			System.out.println(result.getUpsertedId());
			return ok("{\"result\":\"succeed\",\"desc\":\"insert succeed\"}");
		} else {
			return badRequest("{\"result\":\"fail\",\"desc\":\"insert fail\"}");
		}
		*/
		return ok("{\"result\":\"succeed\",\"desc\":\"insert succeed\"}");
		
	}

}

 

执行:

POST /project0/v1/blog HTTP/1.1
Host: 192.168.56.1:9000
Cookie: AuthToken=f7e9f4e23b20bc424384e9e9b0488043;_id=1
Accept: */*
Accept-Encoding: * 
User-Agent: Dewmobile,Qiangzi-Brother
Connection: close
Content-type: application/json
Content-length: 213

{"title":"论那场风花雪月","abstract":"那一年的风花雪月","content":"2014年的第一场雪,我在屋里睡觉...:)","tags":"回忆 往事 唏嘘 时光荏苒","category":"538e0f556471a4b69790d4f8"}

try to read response header...
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Connection: close
Content-Length: 44

try to read response body...
{"result":"succeed","desc":"insert succeed"}
fubenji:PRIMARY> db.blog.find()
{ "_id" : ObjectId("538e8c7e86f63291cd8ca951"), "title" : "论那场风花雪月", "abstracct" : "那一年的风花雪月", "content" : "2014年的第一场雪,我在屋里睡觉...:)", "tags" : "回忆 往事 唏嘘 时光荏苒", "category" : "538e0f556471a4b69790d4f8" }
fubenji:PRIMARY> 
fubenji:SECONDARY> db.blog.find()
{ "_id" : ObjectId("538e8c7e86f63291cd8ca951"), "title" : "论那场风花雪月", "abstracct" : "那一年的风花雪月", "content" : "2014年的第一场雪,我在屋里睡觉...:)", "tags" : "回忆 往事 唏嘘 时光荏苒", "category" : "538e0f556471a4b69790d4f8" }

至于索引,暂时就不添加了,等后续有查询需求再添加,大功告成。

 

TODO: ElasticSearch搜索

下一页:http://my.oschina.net/qiangzigege/blog/274697

 

你可能感兴趣的:(架构)