feat: 📚 完成课程导入与校验接口(赶在 token 过期前…) - 实现课程导入接口 ✅ - 实现课程信息校验接口 🔍 - 测试通过 🧪 fix: 🐛 修了一堆 bug,15 分钟 accessToken 默默见证了时间的流逝 ⏳😭
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/LoveLosita/smartflow/backend/model"
|
|
"github.com/LoveLosita/smartflow/backend/respond"
|
|
"github.com/LoveLosita/smartflow/backend/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ScheduleHandler struct {
|
|
// 伸出手:准备接住 Service
|
|
service *service.ScheduleService
|
|
}
|
|
|
|
// NewScheduleHandler 创建 ScheduleHandler 实例
|
|
func NewScheduleHandler(service *service.ScheduleService) *ScheduleHandler {
|
|
return &ScheduleHandler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
func (sa *ScheduleHandler) 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 *ScheduleHandler) 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 方法添加课程
|
|
err = sa.service.AddUserCourses(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)
|
|
}
|