Files
smartmate/backend/api/schedule.go
LoveLosita 79b6be5f40 Version: 0.1.3.dev.260208
refactor: ♻️ 重命名部分接口接收器以避免与 dao 层包名冲突

- 调整部分接口接收器命名,避免与 dao 层包名重名 🧩

feat: 📅 新增获取用户今日日程接口并完成实现

fix: 🐛 修复现实日期与相对日期转换逻辑中的初始化时序问题

- 修复 conv/time.go 中日期转换函数的一个 bug ⏱️
- 解决 viper 在包级变量初始化时机过早的问题
- 避免因过早初始化导致无法读取配置中的学期开学/结束时间 📆
2026-02-08 19:09:40 +08:00

45 lines
1.2 KiB
Go

package api
import (
"context"
"errors"
"net/http"
"time"
"github.com/LoveLosita/smartflow/backend/respond"
"github.com/LoveLosita/smartflow/backend/service"
"github.com/gin-gonic/gin"
)
type ScheduleAPI struct {
scheduleService *service.ScheduleService
}
func NewScheduleAPI(scheduleService *service.ScheduleService) *ScheduleAPI {
return &ScheduleAPI{
scheduleService: scheduleService,
}
}
func (s *ScheduleAPI) GetUserTodaySchedule(c *gin.Context) {
// 1. 从请求上下文中获取用户ID
userID := c.GetInt("user_id")
//2.调用服务层方法获取用户当天的日程安排
// 创建一个带 1 秒超时的上下文
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
defer cancel() // 记得释放资源
todaySchedules, err := s.scheduleService.GetUserTodaySchedule(ctx, userID)
if err != nil {
switch {
case errors.Is(err, respond.WrongUserID):
c.JSON(http.StatusBadRequest, respond.WrongUserID)
return
default:
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
return
}
}
//3.返回日程安排数据给前端
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, todaySchedules))
}