feat: add controller requirements
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package icu.sunway.ai_spring_example.Common;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 通用响应实体类
|
||||
* 用于统一API接口的响应格式
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ResponseEntity<T> {
|
||||
|
||||
/**
|
||||
* 响应状态码
|
||||
* 200: 成功
|
||||
* 400: 请求参数错误
|
||||
* 401: 未授权
|
||||
* 403: 拒绝访问
|
||||
* 404: 资源不存在
|
||||
* 500: 服务器内部错误
|
||||
*/
|
||||
private int code;
|
||||
|
||||
/**
|
||||
* 响应消息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
/**
|
||||
* 响应数据
|
||||
*/
|
||||
private T data;
|
||||
|
||||
/**
|
||||
* 响应时间戳
|
||||
*/
|
||||
private String timestamp;
|
||||
|
||||
/**
|
||||
* 时间格式化器
|
||||
*/
|
||||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/**
|
||||
* 创建成功响应
|
||||
* @param <T> 数据类型
|
||||
* @return 响应实体
|
||||
*/
|
||||
public static <T> ResponseEntity<T> success() {
|
||||
return new ResponseEntity<>(200, "操作成功", null, LocalDateTime.now().format(FORMATTER));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建成功响应并携带数据
|
||||
* @param data 响应数据
|
||||
* @param <T> 数据类型
|
||||
* @return 响应实体
|
||||
*/
|
||||
public static <T> ResponseEntity<T> success(T data) {
|
||||
return new ResponseEntity<>(200, "操作成功", data, LocalDateTime.now().format(FORMATTER));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建成功响应并自定义消息
|
||||
* @param message 响应消息
|
||||
* @param data 响应数据
|
||||
* @param <T> 数据类型
|
||||
* @return 响应实体
|
||||
*/
|
||||
public static <T> ResponseEntity<T> success(String message, T data) {
|
||||
return new ResponseEntity<>(200, message, data, LocalDateTime.now().format(FORMATTER));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建错误响应
|
||||
* @param message 错误消息
|
||||
* @param <T> 数据类型
|
||||
* @return 响应实体
|
||||
*/
|
||||
public static <T> ResponseEntity<T> error(String message) {
|
||||
return new ResponseEntity<>(500, message, null, LocalDateTime.now().format(FORMATTER));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建错误响应并自定义状态码
|
||||
* @param code 错误状态码
|
||||
* @param message 错误消息
|
||||
* @param <T> 数据类型
|
||||
* @return 响应实体
|
||||
*/
|
||||
public static <T> ResponseEntity<T> error(int code, String message) {
|
||||
return new ResponseEntity<>(code, message, null, LocalDateTime.now().format(FORMATTER));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建错误响应并携带数据
|
||||
* @param code 错误状态码
|
||||
* @param message 错误消息
|
||||
* @param data 响应数据
|
||||
* @param <T> 数据类型
|
||||
* @return 响应实体
|
||||
*/
|
||||
public static <T> ResponseEntity<T> error(int code, String message, T data) {
|
||||
return new ResponseEntity<>(code, message, data, LocalDateTime.now().format(FORMATTER));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
package icu.sunway.ai_spring_example.Controllers;
|
||||
|
||||
import java.util.HashMap;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import icu.sunway.ai_spring_example.Common.ResponseEntity;
|
||||
|
||||
/**
|
||||
* 用户管理后台接口
|
||||
* 提供用户管理相关的功能
|
||||
*/
|
||||
@RestController
|
||||
public class Admin {
|
||||
|
||||
/**
|
||||
* 获取用户列表
|
||||
*
|
||||
* @param params 查询参数,包含page、size、username、status等字段
|
||||
* @return 用户列表数据
|
||||
*/
|
||||
@GetMapping("/admin/users")
|
||||
public ResponseEntity<?> getUserList(@RequestParam Map<String, Object> params) {
|
||||
// 从Map中获取查询参数
|
||||
Integer page = Integer.valueOf((String) params.getOrDefault("page", "1"));
|
||||
Integer size = Integer.valueOf((String) params.getOrDefault("size", "10"));
|
||||
String username = (String) params.get("username");
|
||||
Integer status = params.get("status") != null ? Integer.valueOf((String) params.get("status")) : null;
|
||||
|
||||
// TODO: 实现查询逻辑
|
||||
// 1. 根据条件查询用户列表
|
||||
// 2. 处理分页
|
||||
|
||||
// 模拟返回用户列表
|
||||
List<Map<String, Object>> userList = List.of(
|
||||
Map.of("id", 1, "username", "admin", "email", "admin@example.com", "status", 1, "role", "admin",
|
||||
"createTime", "2023-01-01 10:00:00"),
|
||||
Map.of("id", 2, "username", "user1", "email", "user1@example.com", "status", 1, "role", "user",
|
||||
"createTime", "2023-01-02 10:00:00"));
|
||||
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("list", userList);
|
||||
responseData.put("total", 2);
|
||||
responseData.put("page", page);
|
||||
responseData.put("size", size);
|
||||
|
||||
return ResponseEntity.success("获取用户列表成功", responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户详情
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 用户详细信息
|
||||
*/
|
||||
@GetMapping("/admin/users/{userId}")
|
||||
public ResponseEntity<?> getUserDetail(@PathVariable Long userId) {
|
||||
// 参数验证
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现查询逻辑
|
||||
// 1. 根据用户ID查询用户信息
|
||||
// 2. 构建响应数据
|
||||
|
||||
// 模拟返回用户详情
|
||||
Map<String, Object> userInfo = Map.of(
|
||||
"id", userId, "username", "admin", "email", "admin@example.com", "status", 1,
|
||||
"role", "admin", "createTime", "2023-01-01 10:00:00", "updateTime", "2023-01-02 10:00:00");
|
||||
|
||||
return ResponseEntity.success("获取用户详情成功", userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新用户
|
||||
*
|
||||
* @param userMap 用户信息,包含username、password、email、role等字段
|
||||
* @return 创建结果
|
||||
*/
|
||||
@PostMapping("/admin/users")
|
||||
public ResponseEntity<?> createUser(@RequestBody Map<String, Object> userMap) {
|
||||
// 从Map中获取用户信息
|
||||
String username = (String) userMap.get("username");
|
||||
String password = (String) userMap.get("password");
|
||||
String email = (String) userMap.get("email");
|
||||
String role = (String) userMap.get("role");
|
||||
Integer status = (Integer) userMap.get("status");
|
||||
|
||||
// 参数验证
|
||||
if (username == null || username.isEmpty()) {
|
||||
return ResponseEntity.error(400, "用户名不能为空");
|
||||
}
|
||||
if (password == null || password.isEmpty()) {
|
||||
return ResponseEntity.error(400, "密码不能为空");
|
||||
}
|
||||
if (email == null || email.isEmpty()) {
|
||||
return ResponseEntity.error(400, "邮箱不能为空");
|
||||
}
|
||||
if (role == null || role.isEmpty()) {
|
||||
return ResponseEntity.error(400, "角色不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现创建逻辑
|
||||
// 1. 检查用户名是否已存在
|
||||
// 2. 检查邮箱是否已存在
|
||||
// 3. 密码加密
|
||||
// 4. 保存用户信息
|
||||
|
||||
// 模拟创建成功
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("id", 3);
|
||||
responseData.put("username", username);
|
||||
responseData.put("email", email);
|
||||
|
||||
return ResponseEntity.success("创建用户成功", responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param userMap 用户更新信息
|
||||
* @return 更新结果
|
||||
*/
|
||||
@PutMapping("/admin/users/{userId}")
|
||||
public ResponseEntity<?> updateUser(@PathVariable Long userId, @RequestBody Map<String, Object> userMap) {
|
||||
// 参数验证
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
}
|
||||
|
||||
// 从Map中获取更新信息
|
||||
String email = (String) userMap.get("email");
|
||||
String role = (String) userMap.get("role");
|
||||
Integer status = (Integer) userMap.get("status");
|
||||
|
||||
// TODO: 实现更新逻辑
|
||||
// 1. 检查用户是否存在
|
||||
// 2. 更新用户信息
|
||||
|
||||
// 模拟更新成功
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("id", userId);
|
||||
responseData.put("email", email);
|
||||
responseData.put("role", role);
|
||||
responseData.put("status", status);
|
||||
|
||||
return ResponseEntity.success("更新用户信息成功", responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 删除结果
|
||||
*/
|
||||
@DeleteMapping("/admin/users/{userId}")
|
||||
public ResponseEntity<?> deleteUser(@PathVariable Long userId) {
|
||||
// 参数验证
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现删除逻辑
|
||||
// 1. 检查用户是否存在
|
||||
// 2. 删除用户信息
|
||||
|
||||
// 模拟删除成功
|
||||
return ResponseEntity.success("删除用户成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改用户密码
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param passwordMap 密码信息,包含oldPassword和newPassword字段
|
||||
* @return 修改结果
|
||||
*/
|
||||
@PutMapping("/admin/users/{userId}/password")
|
||||
public ResponseEntity<?> changeUserPassword(@PathVariable Long userId,
|
||||
@RequestBody Map<String, Object> passwordMap) {
|
||||
// 参数验证
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
}
|
||||
|
||||
// 从Map中获取密码信息
|
||||
String oldPassword = (String) passwordMap.get("oldPassword");
|
||||
String newPassword = (String) passwordMap.get("newPassword");
|
||||
|
||||
// 参数验证
|
||||
if (oldPassword == null || oldPassword.isEmpty()) {
|
||||
return ResponseEntity.error(400, "旧密码不能为空");
|
||||
}
|
||||
if (newPassword == null || newPassword.isEmpty()) {
|
||||
return ResponseEntity.error(400, "新密码不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现修改密码逻辑
|
||||
// 1. 检查用户是否存在
|
||||
// 2. 验证旧密码
|
||||
// 3. 更新新密码
|
||||
|
||||
// 模拟修改密码成功
|
||||
return ResponseEntity.success("修改密码成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户状态
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param statusMap 状态信息,包含status字段
|
||||
* @return 更新结果
|
||||
*/
|
||||
@PutMapping("/admin/users/{userId}/status")
|
||||
public ResponseEntity<?> updateUserStatus(@PathVariable Long userId, @RequestBody Map<String, Object> statusMap) {
|
||||
// 参数验证
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
}
|
||||
|
||||
// 从Map中获取状态信息
|
||||
Integer status = (Integer) statusMap.get("status");
|
||||
|
||||
// 参数验证
|
||||
if (status == null) {
|
||||
return ResponseEntity.error(400, "状态不能为空");
|
||||
}
|
||||
if (status != 0 && status != 1) {
|
||||
return ResponseEntity.error(400, "状态值只能是0或1");
|
||||
}
|
||||
|
||||
// TODO: 实现更新状态逻辑
|
||||
// 1. 检查用户是否存在
|
||||
// 2. 更新用户状态
|
||||
|
||||
// 模拟更新状态成功
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("id", userId);
|
||||
responseData.put("status", status);
|
||||
|
||||
return ResponseEntity.success("更新用户状态成功", responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除用户
|
||||
*
|
||||
* @param deleteMap 批量删除信息,包含ids字段
|
||||
* @return 删除结果
|
||||
*/
|
||||
@DeleteMapping("/admin/users/batch")
|
||||
public ResponseEntity<?> batchDeleteUsers(@RequestBody Map<String, Object> deleteMap) {
|
||||
// 从Map中获取删除信息
|
||||
List<Long> userIds = (List<Long>) deleteMap.get("ids");
|
||||
|
||||
// 参数验证
|
||||
if (userIds == null || userIds.isEmpty()) {
|
||||
return ResponseEntity.error(400, "用户ID列表不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现批量删除逻辑
|
||||
// 1. 检查用户是否存在
|
||||
// 2. 批量删除用户
|
||||
|
||||
// 模拟批量删除成功
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("deletedCount", userIds.size());
|
||||
responseData.put("deletedIds", userIds);
|
||||
|
||||
return ResponseEntity.success("批量删除用户成功", responseData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package icu.sunway.ai_spring_example.Controllers;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import icu.sunway.ai_spring_example.Common.ResponseEntity;
|
||||
|
||||
/**
|
||||
* 认证控制器
|
||||
* 提供登录和注册接口
|
||||
*/
|
||||
@RestController
|
||||
public class Auth {
|
||||
|
||||
/**
|
||||
* 登录接口
|
||||
*
|
||||
* @param loginMap 登录请求参数,包含username和password字段
|
||||
* @return 登录成功返回 JWT 令牌,失败返回错误信息
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<?> login(@RequestBody Map<String, Object> loginMap) {
|
||||
// 从Map中获取登录参数
|
||||
String username = (String) loginMap.get("username");
|
||||
String password = (String) loginMap.get("password");
|
||||
|
||||
// 参数验证
|
||||
if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
|
||||
return ResponseEntity.error(400, "用户名和密码不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现实际的登录逻辑
|
||||
// 1. 根据用户名查询用户
|
||||
// 2. 验证密码
|
||||
// 3. 生成JWT令牌
|
||||
|
||||
// 模拟登录成功
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("token", "mock-jwt-token-123456");
|
||||
responseData.put("username", username);
|
||||
responseData.put("role", "user");
|
||||
|
||||
return ResponseEntity.success("登录成功", responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册接口
|
||||
*
|
||||
* @param registerMap 注册请求参数,包含username、password、email等字段
|
||||
* @return 注册成功返回成功信息,失败返回错误信息
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<?> register(@RequestBody Map<String, Object> registerMap) {
|
||||
// 从Map中获取注册参数
|
||||
String username = (String) registerMap.get("username");
|
||||
String password = (String) registerMap.get("password");
|
||||
String email = (String) registerMap.get("email");
|
||||
|
||||
// 参数验证
|
||||
if (username == null || username.isEmpty()) {
|
||||
return ResponseEntity.error(400, "用户名不能为空");
|
||||
}
|
||||
if (password == null || password.isEmpty()) {
|
||||
return ResponseEntity.error(400, "密码不能为空");
|
||||
}
|
||||
if (email == null || email.isEmpty()) {
|
||||
return ResponseEntity.error(400, "邮箱不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现实际的注册逻辑
|
||||
// 1. 检查用户名是否已存在
|
||||
// 2. 检查邮箱是否已存在
|
||||
// 3. 密码加密
|
||||
// 4. 保存用户信息
|
||||
|
||||
// 模拟注册成功
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("username", username);
|
||||
responseData.put("email", email);
|
||||
|
||||
return ResponseEntity.success("注册成功", responseData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user