

/**
 * create-user-info.js
 *
 * Create user info
 */

module.exports = factory;

/**
 * Export a factory function instead of middleware
 *
 * @return  MW
 */
function factory() {
	return middleware;
};

function *middleware(next) {
	yield next;

	var db = this.db;
	var User = db.col('users');

	var body = this.request.body;

	console.dir(body);
	if(body.uid == undefined || body == null){
		console.log("body is null.");
		var result = {ok:"0", msg: "invalid request"};
		this.body = result;
		return;
	}

	var profile = yield User.findOne({
		uid: body.uid
	});

	console.log("check before create user info: " + profile);
	console.dir(profile);


	if(profile == null || profile.uid == null){
		var result = yield User.insert({
			  uid: body.uid
			, name: body.name
			, title: body.title
			, info: body.info
			, location: body.location
			, image: body.image
		});

		console.log("create user info: " + result);
	}else{
		var result = {ok:"0", msg: "user is exist"};
	}
	this.body = result;
};

@@ -327,135 +327,77 @@
 y;%0A%0A
-%09console.dir(body);%0A
 %09if(
+!
 body
-.uid == undefined
  %7C%7C 
+!
 body
- == null)%7B%0A%09%09console.log(%22body is null.%22);%0A%09%09var result = %7Bok:%220%22, msg: %22i
+.uid)%7B%0A%09%09this.body = %7B%0A%09%09%09code: 400,%0A%09%09%09message: %22I
 nval
@@ -411,31 +411,13 @@
 est%22
-%7D;%0A%09%09this.body = result
+,%0A%09%09%7D
 ;%0A%09%09
@@ -491,137 +491,108 @@
 ;%0A%0A%09
-console.log(%22check before create user info: %22 + profile);%0A%09console.dir(profile);%0A%0A%0A%09if(profile == null %7C%7C profile.uid == null)%7B%0A%09
+if(profile)%7B%0A%09%09this.body = %7B%0A%09%09%09code: 409,%0A%09%09%09message: %22User is already exist.%22,%0A%09%09%7D;%0A%09%09return;%0A%09%7D%0A%0A
 %09var
@@ -621,17 +621,16 @@
 nsert(%7B%0A
-%09
 %09%09  uid:
@@ -641,17 +641,16 @@
 y.uid%0A%09%09
-%09
 , name: 
@@ -659,17 +659,16 @@
 dy.name%0A
-%09
 %09%09, titl
@@ -683,17 +683,16 @@
 title%0A%09%09
-%09
 , info: 
@@ -701,17 +701,16 @@
 dy.info%0A
-%09
 %09%09, loca
@@ -731,17 +731,16 @@
 ation%0A%09%09
-%09
 , image:
@@ -755,138 +755,194 @@
 age%0A
-%09
 %09%7D);%0A%0A%09
-%09console.log(%22create user info: %22 + result);%0A%09%7Delse%7B%0A%09%09var result = %7Bok:%220%22, msg: %22user is exist%22%7D;%0A%09%7D%0A%09this.body = result
+if(!result %7C%7C !result.ok %7C%7C result.ok !== 1)%7B%0A%09%09this.body = %7B%0A%09%09%09code: 500,%0A%09%09%09message: %22DB error.%22,%0A%09%09%7D;%0A%09%09return;%0A%09%7D%0A%0A%09this.body = %7B%0A%09%09code: 200,%0A%09%09message: %22%22,%0A%09%09data : body%0A%09%7D
 ;%0A%7D;

update json response object for creating user info