123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package com.hz.web.controller.tool;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import org.springframework.web.bind.annotation.DeleteMapping;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.PutMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import com.hz.common.core.controller.BaseController;
- import com.hz.common.core.domain.AjaxResult;
- import com.hz.common.utils.StringUtils;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiImplicitParams;
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
- import io.swagger.annotations.ApiOperation;
- /**
- * swagger 用户测试方法
- *
- * @author user
- */
- @RestController
- @RequestMapping("/test/user")
- public class TestController extends BaseController {
- private final static Map<Integer, UserEntity> USERS = new LinkedHashMap<Integer, UserEntity>();
- {
- USERS.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
- USERS.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
- }
- /**
- * 获取用户列表
- */
- @GetMapping("/list")
- public AjaxResult userList() {
- List<UserEntity> userList = new ArrayList<UserEntity>(USERS.values());
- return AjaxResult.success(userList);
- }
- /**
- * 获取用户详细
- */
- @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
- @GetMapping("/{userId}")
- public AjaxResult getUser(@PathVariable Integer userId) {
- if (!USERS.isEmpty() && USERS.containsKey(userId)) {
- return AjaxResult.success(USERS.get(userId));
- } else {
- return error("用户不存在");
- }
- }
- /**
- * 新增用户
- */
- @ApiImplicitParams({
- @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer"),
- @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String"),
- @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String"),
- @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String")
- })
- @PostMapping("/save")
- public AjaxResult save(UserEntity user) {
- if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
- return error("用户ID不能为空");
- }
- return AjaxResult.success(USERS.put(user.getUserId(), user));
- }
- /**
- * 更新用户
- */
- @PutMapping("/update")
- public AjaxResult update(@RequestBody UserEntity user) {
- if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
- return error("用户ID不能为空");
- }
- if (USERS.isEmpty() || !USERS.containsKey(user.getUserId())) {
- return error("用户不存在");
- }
- USERS.remove(user.getUserId());
- return AjaxResult.success(USERS.put(user.getUserId(), user));
- }
- /**
- * 删除用户信息
- */
- @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
- @DeleteMapping("/{userId}")
- public AjaxResult delete(@PathVariable Integer userId) {
- if (!USERS.isEmpty() && USERS.containsKey(userId)) {
- USERS.remove(userId);
- return success();
- } else {
- return error("用户不存在");
- }
- }
- }
- @ApiModel(value = "UserEntity", description = "用户实体")
- class UserEntity {
- @ApiModelProperty("用户ID")
- private Integer userId;
- @ApiModelProperty("用户名称")
- private String username;
- @ApiModelProperty("用户密码")
- private String password;
- @ApiModelProperty("用户手机")
- private String mobile;
- public UserEntity() {
- }
- public UserEntity(Integer userId, String username, String password, String mobile) {
- this.userId = userId;
- this.username = username;
- this.password = password;
- this.mobile = mobile;
- }
- public Integer getUserId() {
- return userId;
- }
- public void setUserId(Integer userId) {
- this.userId = userId;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getMobile() {
- return mobile;
- }
- public void setMobile(String mobile) {
- this.mobile = mobile;
- }
- }
|