|
|
@@ -0,0 +1,123 @@
|
|
|
+package space.anyi.controller;
|
|
|
+
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.Parameter;
|
|
|
+import io.swagger.v3.oas.annotations.media.Content;
|
|
|
+import io.swagger.v3.oas.annotations.media.ExampleObject;
|
|
|
+import io.swagger.v3.oas.annotations.media.Schema;
|
|
|
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
|
+import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
|
|
+import jakarta.validation.Valid;
|
|
|
+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.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import space.anyi.common.Result;
|
|
|
+import space.anyi.dto.UserRequest;
|
|
|
+import space.anyi.entity.User;
|
|
|
+import space.anyi.service.UserService;
|
|
|
+import space.anyi.vo.UserVO;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/users")
|
|
|
+@Tag(name = "User Management", description = "Endpoints for managing users")
|
|
|
+public class UserController {
|
|
|
+
|
|
|
+ private final UserService userService;
|
|
|
+
|
|
|
+ public UserController(UserService userService) {
|
|
|
+ this.userService = userService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping
|
|
|
+ @Operation(summary = "List all users", description = "Returns the full list of users stored in the system.")
|
|
|
+ @ApiResponses({
|
|
|
+@ApiResponse(responseCode = "200", description = "A list of users"),
|
|
|
+ @ApiResponse(responseCode = "500", description = "Internal server error")
|
|
|
+ })
|
|
|
+ public Result<List<UserVO>> findAll() {
|
|
|
+ List<UserVO> users = userService.findAll().stream().map(UserVO::from).toList();
|
|
|
+ return Result.success(users);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/{id}")
|
|
|
+ @Operation(summary = "Get a user by id", description = "Returns a single user matching the given id.")
|
|
|
+ @ApiResponses({
|
|
|
+ @ApiResponse(responseCode = "200", description = "The requested user"),
|
|
|
+ @ApiResponse(responseCode = "404", description = "User not found")
|
|
|
+ })
|
|
|
+ public Result<UserVO> findById(
|
|
|
+ @Parameter(description = "Id of the user to retrieve", example = "1")
|
|
|
+ @PathVariable Long id) {
|
|
|
+ return Result.success(UserVO.from(userService.findById(id)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping
|
|
|
+ @Operation(summary = "Create a user", description = "Creates a new user. The account must be unique.")
|
|
|
+ @ApiResponses({
|
|
|
+ @ApiResponse(responseCode = "200", description = "User created"),
|
|
|
+ @ApiResponse(responseCode = "400", description = "Invalid request body"),
|
|
|
+ @ApiResponse(responseCode = "409", description = "Account already exists")
|
|
|
+ })
|
|
|
+ public Result<UserVO> create(
|
|
|
+ @RequestBody(description = "User data to create", required = true,
|
|
|
+ content = @Content(schema = @Schema(implementation = UserRequest.class),
|
|
|
+ examples = @ExampleObject(name = "user", value = """
|
|
|
+ {
|
|
|
+ "name": "Alice",
|
|
|
+ "account": "alice01",
|
|
|
+ "sex": "female"
|
|
|
+ }
|
|
|
+ """)))
|
|
|
+ @org.springframework.web.bind.annotation.RequestBody @Valid UserRequest request) {
|
|
|
+ return Result.success(UserVO.from(userService.create(toEntity(request))));
|
|
|
+ }
|
|
|
+
|
|
|
+ @PutMapping("/{id}")
|
|
|
+ @Operation(summary = "Update a user", description = "Updates an existing user by id. The account must be unique.")
|
|
|
+ @ApiResponses({
|
|
|
+ @ApiResponse(responseCode = "200", description = "User updated"),
|
|
|
+ @ApiResponse(responseCode = "400", description = "Invalid request body"),
|
|
|
+ @ApiResponse(responseCode = "404", description = "User not found"),
|
|
|
+ @ApiResponse(responseCode = "409", description = "Account already exists")
|
|
|
+ })
|
|
|
+ public Result<UserVO> update(
|
|
|
+ @Parameter(description = "Id of the user to update", example = "1")
|
|
|
+ @PathVariable Long id,
|
|
|
+ @RequestBody(description = "Updated user data", required = true,
|
|
|
+ content = @Content(schema = @Schema(implementation = UserRequest.class),
|
|
|
+ examples = @ExampleObject(name = "user", value = """
|
|
|
+ {
|
|
|
+ "name": "Alice",
|
|
|
+ "account": "alice_new",
|
|
|
+ "sex": "female"
|
|
|
+ }
|
|
|
+ """)))
|
|
|
+ @org.springframework.web.bind.annotation.RequestBody @Valid UserRequest request) {
|
|
|
+ return Result.success(UserVO.from(userService.update(id, toEntity(request))));
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("/{id}")
|
|
|
+ @Operation(summary = "Delete a user", description = "Deletes the user matching the given id.")
|
|
|
+ @ApiResponses({
|
|
|
+ @ApiResponse(responseCode = "200", description = "User deleted"),
|
|
|
+ @ApiResponse(responseCode = "404", description = "User not found")
|
|
|
+ })
|
|
|
+ public Result<Void> delete(
|
|
|
+ @Parameter(description = "Id of the user to delete", example = "1")
|
|
|
+ @PathVariable Long id) {
|
|
|
+ userService.delete(id);
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ private User toEntity(UserRequest request) {
|
|
|
+ return new User(request.getName(), request.getAccount(), request.getSex());
|
|
|
+ }
|
|
|
+}
|