Files
smartmate/backend/api/course.go
LoveLosita 132b7095ac Version:0.1.0.dev.260205
feat: 🆕 完善course模块功能并优化批量导入接口

- 调整 task-class 模型代码并增加注释,使其更简洁易读 ✍️
- 结构调整:将课程相关接口从 schedule 分类移至独立的 course 分类 🚀
  - 修改接口 URL 从 /schedule 更改为 /course 🔄

fix: 🐛 修复批量导入课程接口重复导入相同课程的 bug
- 通过在数据库添加唯一约束解决此问题 🔐
- 这只是初步修复,后续会在 sv 层增加重复/时间冲突检测逻辑 ⚠️
- 引导用户修改课表与任务块时间冲突的机制待实现 

refactor: 🔨 重构 schedule 表单结构
- 将节次管理策略从字符串存储改为原子化存储(如 1-2 节更改为单独两条记录)
- 为后续冲突检查与智能排课做准备 🧠

perf: 🚀 优化批量导入课程接口性能
- 通过数据暂存内存中减少数据插入 MySQL 的次数 
2026-02-05 16:51:15 +08:00

72 lines
1.9 KiB
Go

package api
import (
"context"
"errors"
"net/http"
"time"
"github.com/LoveLosita/smartflow/backend/model"
"github.com/LoveLosita/smartflow/backend/respond"
"github.com/LoveLosita/smartflow/backend/service"
"github.com/gin-gonic/gin"
)
type CourseHandler struct {
// 伸出手:准备接住 Service
service *service.CourseService
}
// NewCourseHandler 创建 CourseHandler 实例
func NewCourseHandler(service *service.CourseService) *CourseHandler {
return &CourseHandler{
service: service,
}
}
func (sa *CourseHandler) CheckUserCourse(c *gin.Context) {
//1.从请求中获取课程信息
var req model.UserCheckCourseRequest
err := c.ShouldBindJSON(&req)
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//2.调用 service 层的 CheckSingleCourse 方法进行校验
result := service.CheckSingleCourse(req)
//3.根据校验结果返回响应
if result {
c.JSON(http.StatusOK, respond.Ok)
} else {
c.JSON(http.StatusBadRequest, respond.WrongCourseInfo)
}
}
func (sa *CourseHandler) AddUserCourses(c *gin.Context) {
//1.从请求中获取课程信息
var req model.UserImportCoursesRequest
err := c.ShouldBindJSON(&req)
if err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
//2.从上下文获取用户ID
userIDInterface := c.GetInt("user_id")
//3.调用 service 层的 AddUserCourses 方法添加课程
// 创建一个带 1 秒超时的上下文
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
defer cancel() // 记得释放资源
err = sa.service.AddUserCourses(ctx, req, userIDInterface)
if err != nil {
switch {
case errors.Is(err, respond.WrongParamType), errors.Is(err, respond.WrongCourseInfo):
c.JSON(http.StatusBadRequest, err)
default:
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
}
return
}
//4.返回成功响应
c.JSON(http.StatusOK, respond.Ok)
}