TestController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.hz.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.hz.common.core.controller.BaseController;
  15. import com.hz.common.core.domain.AjaxResult;
  16. import com.hz.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 user
  27. */
  28. @RestController
  29. @RequestMapping("/test/user")
  30. public class TestController extends BaseController {
  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. /**
  37. * 获取用户列表
  38. */
  39. @GetMapping("/list")
  40. public AjaxResult userList() {
  41. List<UserEntity> userList = new ArrayList<UserEntity>(USERS.values());
  42. return AjaxResult.success(userList);
  43. }
  44. /**
  45. * 获取用户详细
  46. */
  47. @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
  48. @GetMapping("/{userId}")
  49. public AjaxResult getUser(@PathVariable Integer userId) {
  50. if (!USERS.isEmpty() && USERS.containsKey(userId)) {
  51. return AjaxResult.success(USERS.get(userId));
  52. } else {
  53. return error("用户不存在");
  54. }
  55. }
  56. /**
  57. * 新增用户
  58. */
  59. @ApiImplicitParams({
  60. @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer"),
  61. @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String"),
  62. @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String"),
  63. @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String")
  64. })
  65. @PostMapping("/save")
  66. public AjaxResult save(UserEntity user) {
  67. if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
  68. return error("用户ID不能为空");
  69. }
  70. return AjaxResult.success(USERS.put(user.getUserId(), user));
  71. }
  72. /**
  73. * 更新用户
  74. */
  75. @PutMapping("/update")
  76. public AjaxResult update(@RequestBody UserEntity user) {
  77. if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
  78. return error("用户ID不能为空");
  79. }
  80. if (USERS.isEmpty() || !USERS.containsKey(user.getUserId())) {
  81. return error("用户不存在");
  82. }
  83. USERS.remove(user.getUserId());
  84. return AjaxResult.success(USERS.put(user.getUserId(), user));
  85. }
  86. /**
  87. * 删除用户信息
  88. */
  89. @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
  90. @DeleteMapping("/{userId}")
  91. public AjaxResult delete(@PathVariable Integer userId) {
  92. if (!USERS.isEmpty() && USERS.containsKey(userId)) {
  93. USERS.remove(userId);
  94. return success();
  95. } else {
  96. return error("用户不存在");
  97. }
  98. }
  99. }
  100. @ApiModel(value = "UserEntity", description = "用户实体")
  101. class UserEntity {
  102. @ApiModelProperty("用户ID")
  103. private Integer userId;
  104. @ApiModelProperty("用户名称")
  105. private String username;
  106. @ApiModelProperty("用户密码")
  107. private String password;
  108. @ApiModelProperty("用户手机")
  109. private String mobile;
  110. public UserEntity() {
  111. }
  112. public UserEntity(Integer userId, String username, String password, String mobile) {
  113. this.userId = userId;
  114. this.username = username;
  115. this.password = password;
  116. this.mobile = mobile;
  117. }
  118. public Integer getUserId() {
  119. return userId;
  120. }
  121. public void setUserId(Integer userId) {
  122. this.userId = userId;
  123. }
  124. public String getUsername() {
  125. return username;
  126. }
  127. public void setUsername(String username) {
  128. this.username = username;
  129. }
  130. public String getPassword() {
  131. return password;
  132. }
  133. public void setPassword(String password) {
  134. this.password = password;
  135. }
  136. public String getMobile() {
  137. return mobile;
  138. }
  139. public void setMobile(String mobile) {
  140. this.mobile = mobile;
  141. }
  142. }