style(controller): 格式化测试控制器代码风格

This commit is contained in:
2026-02-05 16:02:17 +08:00
parent 7af19439b0
commit 60bcac32c5

View File

@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@@ -30,9 +31,9 @@ import io.swagger.annotations.ApiOperation;
@Api("用户信息管理") @Api("用户信息管理")
@RestController @RestController
@RequestMapping("/test/user") @RequestMapping("/test/user")
public class TestController extends BaseController public class TestController extends BaseController {
{
private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>(); private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
{ {
users.put(1, new UserEntity(1, "admin", "admin123", "15888888888")); users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
users.put(2, new UserEntity(2, "ry", "admin123", "15666666666")); users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
@@ -40,8 +41,7 @@ public class TestController extends BaseController
@ApiOperation("获取用户列表") @ApiOperation("获取用户列表")
@GetMapping("/list") @GetMapping("/list")
public R<List<UserEntity>> userList() public R<List<UserEntity>> userList() {
{
List<UserEntity> userList = new ArrayList<UserEntity>(users.values()); List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
return R.ok(userList); return R.ok(userList);
} }
@@ -49,14 +49,10 @@ public class TestController extends BaseController
@ApiOperation("获取用户详细") @ApiOperation("获取用户详细")
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class) @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
@GetMapping("/{userId}") @GetMapping("/{userId}")
public R<UserEntity> getUser(@PathVariable Integer userId) public R<UserEntity> getUser(@PathVariable Integer userId) {
{ if (!users.isEmpty() && users.containsKey(userId)) {
if (!users.isEmpty() && users.containsKey(userId))
{
return R.ok(users.get(userId)); return R.ok(users.get(userId));
} } else {
else
{
return R.fail("用户不存在"); return R.fail("用户不存在");
} }
} }
@@ -69,10 +65,8 @@ public class TestController extends BaseController
@ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class) @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class)
}) })
@PostMapping("/save") @PostMapping("/save")
public R<String> save(UserEntity user) public R<String> save(UserEntity user) {
{ if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
{
return R.fail("用户ID不能为空"); return R.fail("用户ID不能为空");
} }
users.put(user.getUserId(), user); users.put(user.getUserId(), user);
@@ -81,14 +75,11 @@ public class TestController extends BaseController
@ApiOperation("更新用户") @ApiOperation("更新用户")
@PutMapping("/update") @PutMapping("/update")
public R<String> update(@RequestBody UserEntity user) public R<String> update(@RequestBody UserEntity user) {
{ if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
{
return R.fail("用户ID不能为空"); return R.fail("用户ID不能为空");
} }
if (users.isEmpty() || !users.containsKey(user.getUserId())) if (users.isEmpty() || !users.containsKey(user.getUserId())) {
{
return R.fail("用户不存在"); return R.fail("用户不存在");
} }
users.remove(user.getUserId()); users.remove(user.getUserId());
@@ -99,23 +90,18 @@ public class TestController extends BaseController
@ApiOperation("删除用户信息") @ApiOperation("删除用户信息")
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class) @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
@DeleteMapping("/{userId}") @DeleteMapping("/{userId}")
public R<String> delete(@PathVariable Integer userId) public R<String> delete(@PathVariable Integer userId) {
{ if (!users.isEmpty() && users.containsKey(userId)) {
if (!users.isEmpty() && users.containsKey(userId))
{
users.remove(userId); users.remove(userId);
return R.ok(); return R.ok();
} } else {
else
{
return R.fail("用户不存在"); return R.fail("用户不存在");
} }
} }
} }
@ApiModel(value = "UserEntity", description = "用户实体") @ApiModel(value = "UserEntity", description = "用户实体")
class UserEntity class UserEntity {
{
@ApiModelProperty("用户ID") @ApiModelProperty("用户ID")
private Integer userId; private Integer userId;
@@ -128,56 +114,46 @@ class UserEntity
@ApiModelProperty("用户手机") @ApiModelProperty("用户手机")
private String mobile; private String mobile;
public UserEntity() public UserEntity() {
{
} }
public UserEntity(Integer userId, String username, String password, String mobile) public UserEntity(Integer userId, String username, String password, String mobile) {
{
this.userId = userId; this.userId = userId;
this.username = username; this.username = username;
this.password = password; this.password = password;
this.mobile = mobile; this.mobile = mobile;
} }
public Integer getUserId() public Integer getUserId() {
{
return userId; return userId;
} }
public void setUserId(Integer userId) public void setUserId(Integer userId) {
{
this.userId = userId; this.userId = userId;
} }
public String getUsername() public String getUsername() {
{
return username; return username;
} }
public void setUsername(String username) public void setUsername(String username) {
{
this.username = username; this.username = username;
} }
public String getPassword() public String getPassword() {
{
return password; return password;
} }
public void setPassword(String password) public void setPassword(String password) {
{
this.password = password; this.password = password;
} }
public String getMobile() public String getMobile() {
{
return mobile; return mobile;
} }
public void setMobile(String mobile) public void setMobile(String mobile) {
{
this.mobile = mobile; this.mobile = mobile;
} }
} }