TestController.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.emato.web.controller.tool;
  2. import java.util.ArrayList;
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.springframework.web.bind.annotation.DeleteMapping;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.PutMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import com.emato.common.core.controller.BaseController;
  14. import com.emato.common.core.domain.AjaxResult;
  15. import com.emato.common.utils.StringUtils;
  16. import io.swagger.annotations.Api;
  17. import io.swagger.annotations.ApiImplicitParam;
  18. import io.swagger.annotations.ApiModel;
  19. import io.swagger.annotations.ApiModelProperty;
  20. import io.swagger.annotations.ApiOperation;
  21. /**
  22. * swagger 用户测试方法
  23. *
  24. * @author cadmin
  25. */
  26. @Api("用户信息管理")
  27. @RestController
  28. @RequestMapping("/test/user")
  29. public class TestController extends BaseController
  30. {
  31. private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
  32. {
  33. users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
  34. users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
  35. }
  36. @ApiOperation("获取用户列表")
  37. @GetMapping("/list")
  38. public AjaxResult userList()
  39. {
  40. List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
  41. return AjaxResult.success(userList);
  42. }
  43. @ApiOperation("获取用户详细")
  44. @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
  45. @GetMapping("/{userId}")
  46. public AjaxResult getUser(@PathVariable Integer userId)
  47. {
  48. if (!users.isEmpty() && users.containsKey(userId))
  49. {
  50. return AjaxResult.success(users.get(userId));
  51. }
  52. else
  53. {
  54. return AjaxResult.error("用户不存在");
  55. }
  56. }
  57. @ApiOperation("新增用户")
  58. @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
  59. @PostMapping("/save")
  60. public AjaxResult save(UserEntity user)
  61. {
  62. if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
  63. {
  64. return AjaxResult.error("用户ID不能为空");
  65. }
  66. return AjaxResult.success(users.put(user.getUserId(), user));
  67. }
  68. @ApiOperation("更新用户")
  69. @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
  70. @PutMapping("/update")
  71. public AjaxResult update(UserEntity user)
  72. {
  73. if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
  74. {
  75. return AjaxResult.error("用户ID不能为空");
  76. }
  77. if (users.isEmpty() || !users.containsKey(user.getUserId()))
  78. {
  79. return AjaxResult.error("用户不存在");
  80. }
  81. users.remove(user.getUserId());
  82. return AjaxResult.success(users.put(user.getUserId(), user));
  83. }
  84. @ApiOperation("删除用户信息")
  85. @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
  86. @DeleteMapping("/{userId}")
  87. public AjaxResult delete(@PathVariable Integer userId)
  88. {
  89. if (!users.isEmpty() && users.containsKey(userId))
  90. {
  91. users.remove(userId);
  92. return AjaxResult.success();
  93. }
  94. else
  95. {
  96. return AjaxResult.error("用户不存在");
  97. }
  98. }
  99. }
  100. @ApiModel("用户实体")
  101. class UserEntity
  102. {
  103. @ApiModelProperty("用户ID")
  104. private Integer userId;
  105. @ApiModelProperty("用户名称")
  106. private String username;
  107. @ApiModelProperty("用户密码")
  108. private String password;
  109. @ApiModelProperty("用户手机")
  110. private String mobile;
  111. public UserEntity()
  112. {
  113. }
  114. public UserEntity(Integer userId, String username, String password, String mobile)
  115. {
  116. this.userId = userId;
  117. this.username = username;
  118. this.password = password;
  119. this.mobile = mobile;
  120. }
  121. public Integer getUserId()
  122. {
  123. return userId;
  124. }
  125. public void setUserId(Integer userId)
  126. {
  127. this.userId = userId;
  128. }
  129. public String getUsername()
  130. {
  131. return username;
  132. }
  133. public void setUsername(String username)
  134. {
  135. this.username = username;
  136. }
  137. public String getPassword()
  138. {
  139. return password;
  140. }
  141. public void setPassword(String password)
  142. {
  143. this.password = password;
  144. }
  145. public String getMobile()
  146. {
  147. return mobile;
  148. }
  149. public void setMobile(String mobile)
  150. {
  151. this.mobile = mobile;
  152. }
  153. }