TestController.java 5.2 KB

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