UserController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package space.anyi.controller;
  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.Parameter;
  4. import io.swagger.v3.oas.annotations.media.Content;
  5. import io.swagger.v3.oas.annotations.media.ExampleObject;
  6. import io.swagger.v3.oas.annotations.media.Schema;
  7. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  8. import io.swagger.v3.oas.annotations.responses.ApiResponses;
  9. import io.swagger.v3.oas.annotations.tags.Tag;
  10. import io.swagger.v3.oas.annotations.parameters.RequestBody;
  11. import jakarta.validation.Valid;
  12. import org.springframework.web.bind.annotation.DeleteMapping;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.PathVariable;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.PutMapping;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import space.anyi.common.Result;
  20. import space.anyi.dto.UserRequest;
  21. import space.anyi.entity.User;
  22. import space.anyi.service.UserService;
  23. import space.anyi.vo.UserVO;
  24. import java.util.List;
  25. @RestController
  26. @RequestMapping("/api/users")
  27. @Tag(name = "User Management", description = "Endpoints for managing users")
  28. public class UserController {
  29. private final UserService userService;
  30. public UserController(UserService userService) {
  31. this.userService = userService;
  32. }
  33. @GetMapping
  34. @Operation(summary = "List all users", description = "Returns the full list of users stored in the system.")
  35. @ApiResponses({
  36. @ApiResponse(responseCode = "200", description = "A list of users"),
  37. @ApiResponse(responseCode = "500", description = "Internal server error")
  38. })
  39. public Result<List<UserVO>> findAll() {
  40. List<UserVO> users = userService.findAll().stream().map(UserVO::from).toList();
  41. return Result.success(users);
  42. }
  43. @GetMapping("/{id}")
  44. @Operation(summary = "Get a user by id", description = "Returns a single user matching the given id.")
  45. @ApiResponses({
  46. @ApiResponse(responseCode = "200", description = "The requested user"),
  47. @ApiResponse(responseCode = "404", description = "User not found")
  48. })
  49. public Result<UserVO> findById(
  50. @Parameter(description = "Id of the user to retrieve", example = "1")
  51. @PathVariable Long id) {
  52. return Result.success(UserVO.from(userService.findById(id)));
  53. }
  54. @PostMapping
  55. @Operation(summary = "Create a user", description = "Creates a new user. The account must be unique.")
  56. @ApiResponses({
  57. @ApiResponse(responseCode = "200", description = "User created"),
  58. @ApiResponse(responseCode = "400", description = "Invalid request body"),
  59. @ApiResponse(responseCode = "409", description = "Account already exists")
  60. })
  61. public Result<UserVO> create(
  62. @RequestBody(description = "User data to create", required = true,
  63. content = @Content(schema = @Schema(implementation = UserRequest.class),
  64. examples = @ExampleObject(name = "user", value = """
  65. {
  66. "name": "Alice",
  67. "account": "alice01",
  68. "sex": "female"
  69. }
  70. """)))
  71. @org.springframework.web.bind.annotation.RequestBody @Valid UserRequest request) {
  72. return Result.success(UserVO.from(userService.create(toEntity(request))));
  73. }
  74. @PutMapping("/{id}")
  75. @Operation(summary = "Update a user", description = "Updates an existing user by id. The account must be unique.")
  76. @ApiResponses({
  77. @ApiResponse(responseCode = "200", description = "User updated"),
  78. @ApiResponse(responseCode = "400", description = "Invalid request body"),
  79. @ApiResponse(responseCode = "404", description = "User not found"),
  80. @ApiResponse(responseCode = "409", description = "Account already exists")
  81. })
  82. public Result<UserVO> update(
  83. @Parameter(description = "Id of the user to update", example = "1")
  84. @PathVariable Long id,
  85. @RequestBody(description = "Updated user data", required = true,
  86. content = @Content(schema = @Schema(implementation = UserRequest.class),
  87. examples = @ExampleObject(name = "user", value = """
  88. {
  89. "name": "Alice",
  90. "account": "alice_new",
  91. "sex": "female"
  92. }
  93. """)))
  94. @org.springframework.web.bind.annotation.RequestBody @Valid UserRequest request) {
  95. return Result.success(UserVO.from(userService.update(id, toEntity(request))));
  96. }
  97. @DeleteMapping("/{id}")
  98. @Operation(summary = "Delete a user", description = "Deletes the user matching the given id.")
  99. @ApiResponses({
  100. @ApiResponse(responseCode = "200", description = "User deleted"),
  101. @ApiResponse(responseCode = "404", description = "User not found")
  102. })
  103. public Result<Void> delete(
  104. @Parameter(description = "Id of the user to delete", example = "1")
  105. @PathVariable Long id) {
  106. userService.delete(id);
  107. return Result.success();
  108. }
  109. private User toEntity(UserRequest request) {
  110. return new User(request.getName(), request.getAccount(), request.getSex());
  111. }
  112. }