2025-07-18 17:58:07 +08:00
|
|
|
package com.yundage.chat.controller;
|
|
|
|
|
|
|
|
|
|
import com.yundage.chat.entity.User;
|
|
|
|
|
import com.yundage.chat.mapper.UserMapper;
|
2025-07-20 14:45:43 +08:00
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
|
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|
|
|
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2025-07-18 17:58:07 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/users")
|
2025-07-20 14:45:43 +08:00
|
|
|
@Tag(name = "用户管理", description = "用户CRUD操作接口")
|
|
|
|
|
@SecurityRequirement(name = "Bearer Authentication")
|
2025-07-18 17:58:07 +08:00
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserMapper userMapper;
|
|
|
|
|
|
|
|
|
|
@GetMapping
|
2025-07-20 14:45:43 +08:00
|
|
|
@Operation(summary = "获取所有用户", description = "获取系统中所有用户列表")
|
|
|
|
|
@ApiResponse(responseCode = "200", description = "成功获取用户列表")
|
2025-07-18 17:58:07 +08:00
|
|
|
public List<User> getAllUsers() {
|
|
|
|
|
return userMapper.selectAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/{id}")
|
2025-07-20 14:45:43 +08:00
|
|
|
@Operation(summary = "根据ID获取用户", description = "根据用户ID获取用户信息")
|
|
|
|
|
@ApiResponses(value = {
|
|
|
|
|
@ApiResponse(responseCode = "200", description = "成功获取用户信息"),
|
|
|
|
|
@ApiResponse(responseCode = "404", description = "用户不存在")
|
|
|
|
|
})
|
|
|
|
|
public User getUserById(@Parameter(description = "用户ID") @PathVariable Long id) {
|
2025-07-18 17:58:07 +08:00
|
|
|
return userMapper.selectOneById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping
|
2025-07-20 14:45:43 +08:00
|
|
|
@Operation(summary = "创建用户", description = "创建新的用户")
|
|
|
|
|
@ApiResponses(value = {
|
|
|
|
|
@ApiResponse(responseCode = "200", description = "用户创建成功"),
|
|
|
|
|
@ApiResponse(responseCode = "400", description = "请求参数错误")
|
|
|
|
|
})
|
2025-07-18 17:58:07 +08:00
|
|
|
public User createUser(@RequestBody User user) {
|
2025-07-18 18:12:21 +08:00
|
|
|
user.setCreatedAt(LocalDateTime.now());
|
|
|
|
|
user.setUpdatedAt(LocalDateTime.now());
|
2025-07-18 17:58:07 +08:00
|
|
|
userMapper.insert(user);
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PutMapping("/{id}")
|
2025-07-20 14:45:43 +08:00
|
|
|
@Operation(summary = "更新用户", description = "更新指定用户的信息")
|
|
|
|
|
@ApiResponses(value = {
|
|
|
|
|
@ApiResponse(responseCode = "200", description = "用户更新成功"),
|
|
|
|
|
@ApiResponse(responseCode = "404", description = "用户不存在"),
|
|
|
|
|
@ApiResponse(responseCode = "400", description = "请求参数错误")
|
|
|
|
|
})
|
|
|
|
|
public User updateUser(@Parameter(description = "用户ID") @PathVariable Long id, @RequestBody User user) {
|
2025-07-18 17:58:07 +08:00
|
|
|
user.setId(id);
|
2025-07-18 18:12:21 +08:00
|
|
|
user.setUpdatedAt(LocalDateTime.now());
|
2025-07-18 17:58:07 +08:00
|
|
|
userMapper.update(user);
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/{id}")
|
2025-07-20 14:45:43 +08:00
|
|
|
@Operation(summary = "删除用户", description = "根据ID删除用户")
|
|
|
|
|
@ApiResponses(value = {
|
|
|
|
|
@ApiResponse(responseCode = "200", description = "用户删除成功"),
|
|
|
|
|
@ApiResponse(responseCode = "404", description = "用户不存在")
|
|
|
|
|
})
|
|
|
|
|
public void deleteUser(@Parameter(description = "用户ID") @PathVariable Long id) {
|
2025-07-18 17:58:07 +08:00
|
|
|
userMapper.deleteById(id);
|
|
|
|
|
}
|
|
|
|
|
}
|