TestController.java 5.3 KB

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