| 11 | import java.util.Optional; |
| 12 | |
| 13 | @Service |
| 14 | public class UserServiceImpl implements UserService { |
| 15 | |
| 16 | @Autowired |
| 17 | UserRepository userRepository; |
| 18 | |
| 19 | @Override |
| 20 | public Response getUser(Integer userId) { |
| 21 | Optional<User> userOptional = userRepository.findById(userId); |
| 22 | return userOptional.map(user -> new Response("0", JSON.toJSONString(user))).orElseGet(() -> new Response("-1", "用户不存在")); |
| 23 | } |
| 24 | |
| 25 | @Override |
| 26 | public Response userRegister(String userName, String userPassword) { |
| 27 | if(CommonTools.isEmpty(userName)) |
| 28 | return new Response("-1","用户名不能为空"); |
| 29 | if(CommonTools.isEmpty(userPassword)) |
| 30 | return new Response("-1","用户密码不能为空"); |
| 31 | try { |
| 32 | if(userRepository.findByUserName(userName)!=null) |
| 33 | return new Response("-1","此用户名已经存在"); |
| 34 | User user = new User(); |
| 35 | user.setUserName(userName); |
| 36 | user.setUserPassword(userPassword); |
| 37 | user.setRegisterTime(CommonTools.getCurrentTime()); |
| 38 | user = userRepository.save(user); |
| 39 | user.setUserPassword(""); |
| 40 | return new Response("0", JSON.toJSONString(user)); |
| 41 | }catch (Exception e){ |
| 42 | return new Response("-1", "插入用户异常"); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public Response userLogin(String userName, String userPassword) { |
| 48 | if(CommonTools.isEmpty(userName)) |
| 49 | return new Response("-1","用户名不能为空"); |
| 50 | if(CommonTools.isEmpty(userPassword)) |
| 51 | return new Response("-1","用户密码不能为空"); |
| 52 | User user = userRepository.findByUserName(userName); |
| 53 | if(user != null){ |
| 54 | if(user.getUserPassword().equals(userPassword)) { |
| 55 | user.setUserPassword(""); |
| 56 | return new Response("0", JSON.toJSONString(user)); |
| 57 | } |
| 58 | else |
| 59 | return new Response("-1","密码错误"); |
| 60 | } |
| 61 | else |
| 62 | return new Response("-1","用户不存在"); |
| 63 | } |
| 64 | |
| 65 | } |
nothing calls this directly
no outgoing calls
no test coverage detected