TestController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.goldenwater.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.goldenwater.common.core.controller.BaseController;
  15. import com.goldenwater.common.core.domain.R;
  16. import com.goldenwater.common.utils.StringUtils;
  17. import io.swagger.v3.oas.annotations.Operation;
  18. import io.swagger.v3.oas.annotations.media.Schema;
  19. import io.swagger.v3.oas.annotations.tags.Tag;
  20. /**
  21. * swagger 用户测试方法
  22. *
  23. * @author goldenwater
  24. */
  25. @Tag(name = "用户信息管理")
  26. @RestController
  27. @RequestMapping("/test/user")
  28. public class TestController extends BaseController
  29. {
  30. private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
  31. {
  32. users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
  33. users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
  34. }
  35. @Operation(summary = "获取用户列表")
  36. @GetMapping("/list")
  37. public R<List<UserEntity>> userList()
  38. {
  39. List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
  40. return R.ok(userList);
  41. }
  42. @Operation(summary = "获取用户详细")
  43. @GetMapping("/{userId}")
  44. public R<UserEntity> getUser(@PathVariable(name = "userId")
  45. Integer userId)
  46. {
  47. if (!users.isEmpty() && users.containsKey(userId))
  48. {
  49. return R.ok(users.get(userId));
  50. }
  51. else
  52. {
  53. return R.fail("用户不存在");
  54. }
  55. }
  56. @Operation(summary = "新增用户")
  57. @PostMapping("/save")
  58. public R<String> save(UserEntity user)
  59. {
  60. if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
  61. {
  62. return R.fail("用户ID不能为空");
  63. }
  64. users.put(user.getUserId(), user);
  65. return R.ok();
  66. }
  67. @Operation(summary = "更新用户")
  68. @PutMapping("/update")
  69. public R<String> update(@RequestBody
  70. UserEntity user)
  71. {
  72. if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
  73. {
  74. return R.fail("用户ID不能为空");
  75. }
  76. if (users.isEmpty() || !users.containsKey(user.getUserId()))
  77. {
  78. return R.fail("用户不存在");
  79. }
  80. users.remove(user.getUserId());
  81. users.put(user.getUserId(), user);
  82. return R.ok();
  83. }
  84. @Operation(summary = "删除用户信息")
  85. @DeleteMapping("/{userId}")
  86. public R<String> delete(@PathVariable(name = "userId")
  87. Integer userId)
  88. {
  89. if (!users.isEmpty() && users.containsKey(userId))
  90. {
  91. users.remove(userId);
  92. return R.ok();
  93. }
  94. else
  95. {
  96. return R.fail("用户不存在");
  97. }
  98. }
  99. }
  100. @Schema(description = "用户实体")
  101. class UserEntity
  102. {
  103. @Schema(title = "用户ID")
  104. private Integer userId;
  105. @Schema(title = "用户名称")
  106. private String username;
  107. @Schema(title = "用户密码")
  108. private String password;
  109. @Schema(title = "用户手机")
  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. }