TestController.java 4.2 KB

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