package com.feiyangedu.springcloud.petstore.account.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.feiyangedu.springcloud.petstore.account.bean.UserBean;
import com.feiyangedu.springcloud.petstore.account.domain.User;
import com.feiyangedu.springcloud.petstore.account.repository.UserRepository;
import com.feiyangedu.springcloud.petstore.common.service.AbstractService;
import com.feiyangedu.springcloud.petstore.common.util.BeanUtil;

@RestController
@RequestMapping("/api/account")
public class AccountService extends AbstractService {

	@Autowired
	UserRepository userRepository;

	/**
	 * Get all users.
	 * 
	 * @param pageIndex
	 */
	@GetMapping("/users")
	public Page<User> getUsers(@RequestParam(value = "page", defaultValue = "0") int pageIndex) {
		return userRepository.findAll(new PageRequest(pageIndex, DEFAULT_PAGE_SIZE,
				new Sort(new Sort.Order(Sort.Direction.DESC, "createdAt"))));
	}

	@GetMapping("/users/{id}")
	public User getUser(@PathVariable("id") String id) {
		return checkNonNull(userRepository.findOne(id));
	}

	@PostMapping("/users")
	public User createUser(@RequestBody UserBean bean) {
		User user = new User();
		BeanUtil.copyPublicFields(bean, user);
		userRepository.save(user);
		return user;
	}
}

@@ -896,16 +896,155 @@
 sitory;%0A
+import com.feiyangedu.springcloud.petstore.common.context.UserContext;%0Aimport com.feiyangedu.springcloud.petstore.common.context.UserInfo;%0A
 import c
@@ -1860,16 +1860,78 @@
 bean) %7B%0A
+%09%09try (UserContext ctx = new UserContext(UserInfo.SYSTEM)) %7B%0A%09
 %09%09User u
@@ -1948,16 +1948,17 @@
 User();%0A
+%09
 %09%09BeanUt
@@ -1992,16 +1992,17 @@
 ser);%0A%09%09
+%09
 userRepo
@@ -2020,16 +2020,17 @@
 (user);%0A
+%09
 %09%09return
@@ -2036,13 +2036,17 @@
 n user;%0A
+%09%09%7D%0A
 %09%7D%0A%7D%0A

fix usercontext when create user