package com.lovi.um.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import com.lovi.puppy.annotation.Controller;
import com.lovi.puppy.annotation.ModelAttribute;
import com.lovi.puppy.annotation.PathVariable;
import com.lovi.puppy.annotation.RequestMapping;
import com.lovi.puppy.annotation.ResponseBody;
import com.lovi.puppy.annotation.enums.HttpMethod;
import com.lovi.puppy.exceptions.ServiceCallerException;
import com.lovi.puppy.future.HttpResponseResult;
import com.lovi.puppy.message.FailResult;
import com.lovi.puppy.message.Result;
import com.lovi.puppy.message.ServiceCaller;
import com.lovi.um.common.ResponseMessage;
import com.lovi.um.model.User;

@Controller
@RequestMapping("/users")
public class UserController {

	@Autowired
	private ServiceCaller serviceCaller;
	
	@ResponseBody
	@RequestMapping(produce="application/json")
	public void findAll(HttpResponseResult responseResult) throws ServiceCallerException{
		
		Result<List<User>> result = Result.create();
		FailResult failResult = FailResult.create();
		
		serviceCaller.call("UserService.findAll", result);
		
		result.process(r->{
			responseResult.complete(new ResponseMessage(1, r));
		}, failResult);
		
		failResult.setHandler(fail->{
			responseResult.complete(new ResponseMessage(-1, fail.getMessage()),500);
		});
	}
	
	@ResponseBody
	@RequestMapping(value="/:userId",produce="application/json")
	public void findByUserId(@PathVariable("userId") String id, HttpResponseResult responseResult) throws ServiceCallerException{
		
		Result<User> result = Result.create();
		FailResult failResult = FailResult.create();
		
		serviceCaller.call("UserService.findByUserId", result, id);
		
		result.process(r->{
			responseResult.complete(new ResponseMessage(1, r));
		}, failResult);
		
		failResult.setHandler(fail->{
			responseResult.complete(new ResponseMessage(-1, fail.getMessage()),500);
		});
	}
	
	@ResponseBody
	@RequestMapping(method=HttpMethod.POST,produce="application/json")
	public void insert(@ModelAttribute User user,HttpResponseResult responseResult) throws ServiceCallerException{
		
		serviceCaller.call("UserService.insert", user);
		responseResult.complete(new ResponseMessage(1, "do insert"),200);
	
	}
}

@@ -303,16 +303,62 @@
 apping;%0A
+import com.lovi.puppy.annotation.RequestParm;%0A
 import c
@@ -447,16 +447,59 @@
 Method;%0A
+import com.lovi.puppy.async.AsyncExecutor;%0A
 import c
@@ -2338,10 +2338,803 @@
 );%0A%09%0A%09%7D%0A
+%09%0A%09@ResponseBody%0A%09@RequestMapping(value=%22/async%22, produce=%22application/json%22)%0A%09public void asyncTest(@RequestParm(value = %22q%22, defaultValue=%220%22) Integer q, HttpResponseResult responseResult) throws ServiceCallerException%7B%0A%09%09%0A%09%09AsyncExecutor%3CString%3E asyncExecutor = AsyncExecutor.create();%0A%09%09asyncExecutor.run(()-%3E%7B%0A%09%09%09%0A%09%09%09if(q == 0)%7B%0A%09%09%09%09System.out.println(%22I am goint to wait....%22);%0A%09%09%09%09try %7B%0A%09%09%09%09%09Thread.sleep(5000);%0A%09%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09return %22Hello World%22;%0A%09%09%09%0A%09%09%7D, r-%3E%7B%0A%09%09%09%0A%09%09%09String message = %22I am done. Parameter q = %22 + q + %22 .Result = %22 + r;%0A%09%09%09responseResult.complete(new ResponseMessage(1, message), 200);%0A%09%09%09%0A%09%09%7D, fail-%3E%7B%0A%09%09%09%0A%09%09%09responseResult.complete(new ResponseMessage(-1, fail.getMessage()), 500);%0A%09%09%09%0A%09%09%7D);%0A%09%09%0A%09%09%0A%09%0A%09%7D%0A%09%0A%09%0A
 %7D%0A

add async test