feat: 🗑️ 新增删除单个课程与解除安排日程接口 - 逻辑复杂,初版接口写完后才发现需求需要传切片 - 针对需求修改,通过大 for 循环和事务处理来解决问题 🔄 refactor: 🔧 移除部分冗余的用户 ID 验证逻辑 - sv/schedule.go 中,进来的 ID 已通过 redis 黑名单与 JWT 保护验证 - 去除重复的数据库查验,优化了代码流程 🛠️ refactor: 🔄 重构 API 层业务错误判断逻辑 - 抛弃了原有的手动比对方式,封装进 `respond` 包,简化判断流程 - 未来不再手动遍历数据链路,提升了开发效率 🧹 undo: ⚠️ 修复任务块添加到日程的接口问题(待修复) - 接口允许直接修改已经安排的任务时间,且重复执行时未被禁止 - 此逻辑存在问题,计划在下个版本修复 🔧 undo: ⚠️ 重测接口的幂等性与其他特性 - 当前接口幂等性等特性尚未专门测试,后续计划重测所有接口 - 测试不充分,待进一步完善 🔄 undo: ⚠️ 修复刷新 token 接口错误处理问题 - 当前接口将 token 本身的错误以 500 错误返回,需修复此问题 🛠️
82 lines
2.5 KiB
Go
82 lines
2.5 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"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 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 {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
//3.返回日程安排数据给前端
|
|
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, todaySchedules))
|
|
}
|
|
|
|
func (s *ScheduleAPI) GetUserWeeklySchedule(c *gin.Context) {
|
|
// 1. 从请求上下文中获取用户ID
|
|
userID := c.GetInt("user_id")
|
|
// 2. 从查询参数中获取 week 参数
|
|
week, err := strconv.Atoi(c.Query("week"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
//3.调用服务层方法获取用户当周的日程安排
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
|
defer cancel() // 记得释放资源
|
|
weeklySchedules, err := s.scheduleService.GetUserWeeklySchedule(ctx, userID, week)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
//4.返回日程安排数据给前端
|
|
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, weeklySchedules))
|
|
}
|
|
|
|
func (s *ScheduleAPI) DeleteScheduleEvent(c *gin.Context) {
|
|
// 1. 从请求上下文中获取用户ID
|
|
userID := c.GetInt("user_id")
|
|
// 2. 从请求体中获取要删除的日程事件信息
|
|
var req []model.UserDeleteScheduleEvent
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
//3.调用服务层方法删除指定的日程事件
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
|
defer cancel() // 记得释放资源
|
|
err := s.scheduleService.DeleteScheduleEvent(ctx, req, userID)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
//4.返回删除成功的响应给前端
|
|
c.JSON(http.StatusOK, respond.Ok)
|
|
}
|