feat: add course requirements
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
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.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 Course {
|
||||
|
||||
/**
|
||||
* 获取课程列表
|
||||
*
|
||||
* @param params 查询参数,包含page、size、name、category、status等字段
|
||||
* @return 课程列表数据
|
||||
*/
|
||||
@GetMapping("/courses")
|
||||
public ResponseEntity<?> getCourseList(@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 name = (String) params.get("name");
|
||||
String category = (String) params.get("category");
|
||||
Integer status = params.get("status") != null ? Integer.valueOf((String) params.get("status")) : null;
|
||||
|
||||
// TODO: 实现查询逻辑
|
||||
// 1. 根据条件查询课程列表
|
||||
// 2. 处理分页
|
||||
|
||||
// 模拟返回课程列表
|
||||
List<Map<String, Object>> courseList = List.of(
|
||||
Map.of("id", 1, "name", "Java基础课程", "category", "编程", "teacher", "张老师", "price", 99.00, "students", 150,
|
||||
"status", 1, "description", "Java入门到精通课程"),
|
||||
Map.of("id", 2, "name", "Python数据分析", "category", "数据科学", "teacher", "李老师", "price", 199.00, "students",
|
||||
80, "status", 1, "description", "Python数据分析实战课程"));
|
||||
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("list", courseList);
|
||||
responseData.put("total", 2);
|
||||
responseData.put("page", page);
|
||||
responseData.put("size", size);
|
||||
|
||||
return ResponseEntity.success("获取课程列表成功", responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取课程详情
|
||||
*
|
||||
* @param courseId 课程ID
|
||||
* @return 课程详细信息
|
||||
*/
|
||||
@GetMapping("/courses/{courseId}")
|
||||
public ResponseEntity<?> getCourseDetail(@PathVariable Long courseId) {
|
||||
// 参数验证
|
||||
if (courseId == null) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现查询逻辑
|
||||
// 1. 根据课程ID查询课程信息
|
||||
// 2. 查询课程章节、评价等关联信息
|
||||
|
||||
// 模拟返回课程详情
|
||||
Map<String, Object> courseInfo = Map.of(
|
||||
"id", courseId, "name", "Java基础课程", "category", "编程", "teacher", "张老师", "price", 99.00,
|
||||
"students", 150, "status", 1, "description", "Java入门到精通课程",
|
||||
"chapters", List.of(
|
||||
Map.of("id", 1, "name", "Java简介", "duration", "30分钟"),
|
||||
Map.of("id", 2, "name", "变量与数据类型", "duration", "45分钟")),
|
||||
"ratings", Map.of("avg", 4.8, "count", 50));
|
||||
|
||||
return ResponseEntity.success("获取课程详情成功", courseInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 课程报名
|
||||
*
|
||||
* @param enrollMap 报名信息,包含courseId、userId等字段
|
||||
* @return 报名结果
|
||||
*/
|
||||
@PostMapping("/courses/enroll")
|
||||
public ResponseEntity<?> enrollCourse(@RequestBody Map<String, Object> enrollMap) {
|
||||
// 从Map中获取报名信息
|
||||
Long courseId = (Long) enrollMap.get("courseId");
|
||||
Long userId = (Long) enrollMap.get("userId");
|
||||
|
||||
// 参数验证
|
||||
if (courseId == null) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空");
|
||||
}
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现报名逻辑
|
||||
// 1. 检查课程是否存在且可报名
|
||||
// 2. 检查用户是否已报名该课程
|
||||
// 3. 记录报名信息
|
||||
// 4. 更新课程报名人数
|
||||
|
||||
// 模拟报名成功
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("courseId", courseId);
|
||||
responseData.put("userId", userId);
|
||||
responseData.put("enrollTime", "2023-05-20 10:00:00");
|
||||
|
||||
return ResponseEntity.success("课程报名成功", responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户报名的课程列表
|
||||
*
|
||||
* @param params 查询参数,包含userId、page、size等字段
|
||||
* @return 用户报名的课程列表
|
||||
*/
|
||||
@GetMapping("/user/courses")
|
||||
public ResponseEntity<?> getUserCourses(@RequestParam Map<String, Object> params) {
|
||||
// 从Map中获取查询参数
|
||||
Long userId = Long.valueOf((String) params.get("userId"));
|
||||
Integer page = Integer.valueOf((String) params.getOrDefault("page", "1"));
|
||||
Integer size = Integer.valueOf((String) params.getOrDefault("size", "10"));
|
||||
|
||||
// 参数验证
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现查询逻辑
|
||||
// 1. 根据用户ID查询已报名课程
|
||||
// 2. 处理分页
|
||||
|
||||
// 模拟返回用户报名的课程列表
|
||||
List<Map<String, Object>> userCourseList = List.of(
|
||||
Map.of("id", 1, "courseId", 1, "courseName", "Java基础课程", "teacher", "张老师", "enrollTime",
|
||||
"2023-05-20 10:00:00", "status", "learning"),
|
||||
Map.of("id", 2, "courseId", 2, "courseName", "Python数据分析", "teacher", "李老师", "enrollTime",
|
||||
"2023-05-15 14:30:00", "status", "completed"));
|
||||
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("list", userCourseList);
|
||||
responseData.put("total", 2);
|
||||
responseData.put("page", page);
|
||||
responseData.put("size", size);
|
||||
|
||||
return ResponseEntity.success("获取用户课程列表成功", responseData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消课程报名
|
||||
*
|
||||
* @param courseId 课程ID
|
||||
* @param params 参数,包含userId字段
|
||||
* @return 取消报名结果
|
||||
*/
|
||||
@DeleteMapping("/courses/{courseId}/enroll")
|
||||
public ResponseEntity<?> cancelEnroll(@PathVariable Long courseId, @RequestParam Map<String, Object> params) {
|
||||
// 从Map中获取用户ID
|
||||
Long userId = Long.valueOf((String) params.get("userId"));
|
||||
|
||||
// 参数验证
|
||||
if (courseId == null) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空");
|
||||
}
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
}
|
||||
|
||||
// TODO: 实现取消报名逻辑
|
||||
// 1. 检查报名记录是否存在
|
||||
// 2. 删除报名记录
|
||||
// 3. 更新课程报名人数
|
||||
|
||||
// 模拟取消报名成功
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("courseId", courseId);
|
||||
responseData.put("userId", userId);
|
||||
|
||||
return ResponseEntity.success("取消课程报名成功", responseData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user