Version: 0.2.0.dev.260210
feat: 🗑️ 新增删除单个课程与解除安排日程接口 - 逻辑复杂,初版接口写完后才发现需求需要传切片 - 针对需求修改,通过大 for 循环和事务处理来解决问题 🔄 refactor: 🔧 移除部分冗余的用户 ID 验证逻辑 - sv/schedule.go 中,进来的 ID 已通过 redis 黑名单与 JWT 保护验证 - 去除重复的数据库查验,优化了代码流程 🛠️ refactor: 🔄 重构 API 层业务错误判断逻辑 - 抛弃了原有的手动比对方式,封装进 `respond` 包,简化判断流程 - 未来不再手动遍历数据链路,提升了开发效率 🧹 undo: ⚠️ 修复任务块添加到日程的接口问题(待修复) - 接口允许直接修改已经安排的任务时间,且重复执行时未被禁止 - 此逻辑存在问题,计划在下个版本修复 🔧 undo: ⚠️ 重测接口的幂等性与其他特性 - 当前接口幂等性等特性尚未专门测试,后续计划重测所有接口 - 测试不充分,待进一步完善 🔄 undo: ⚠️ 修复刷新 token 接口错误处理问题 - 当前接口将 token 本身的错误以 500 错误返回,需修复此问题 🛠️
This commit is contained in:
@@ -58,15 +58,10 @@ func (sa *CourseHandler) AddUserCourses(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
conflicts, err := sa.service.AddUserCourses(ctx, req, userIDInterface)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.WrongParamType), errors.Is(err, respond.WrongCourseInfo),
|
||||
errors.Is(err, respond.InsertCourseTwice):
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
case errors.Is(err, respond.ScheduleConflict):
|
||||
if errors.Is(err, respond.ScheduleConflict) {
|
||||
c.JSON(http.StatusConflict, respond.RespWithData(respond.ScheduleConflict, conflicts))
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
}
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
//4.返回成功响应
|
||||
|
||||
@@ -2,11 +2,11 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"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"
|
||||
@@ -31,14 +31,8 @@ func (s *ScheduleAPI) GetUserTodaySchedule(c *gin.Context) {
|
||||
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
|
||||
}
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
//3.返回日程安排数据给前端
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, todaySchedules))
|
||||
@@ -58,15 +52,30 @@ func (s *ScheduleAPI) GetUserWeeklySchedule(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
weeklySchedules, err := s.scheduleService.GetUserWeeklySchedule(ctx, userID, week)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.WrongUserID), errors.Is(err, respond.WeekOutOfRange):
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
return
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -11,7 +10,6 @@ import (
|
||||
"github.com/LoveLosita/smartflow/backend/respond"
|
||||
"github.com/LoveLosita/smartflow/backend/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TaskClassHandler struct {
|
||||
@@ -43,11 +41,8 @@ func (api *TaskClassHandler) UserAddTaskClass(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
err = api.svc.AddOrUpdateTaskClass(ctx, &req, userIDInterface, create, 0)
|
||||
if err != nil {
|
||||
if errors.Is(err, respond.WrongParamType) {
|
||||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.Ok)
|
||||
}
|
||||
@@ -59,7 +54,7 @@ func (api *TaskClassHandler) UserGetTaskClassInfos(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
resp, err := api.svc.GetUserTaskClassInfos(ctx, userIDInterface)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||||
@@ -83,11 +78,7 @@ func (api *TaskClassHandler) UserGetCompleteTaskClass(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
resp, err := api.svc.GetUserCompleteTaskClass(ctx, userIDInterface, intTaskClassID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
c.JSON(http.StatusNotFound, respond.UserTaskClassNotFound)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||||
@@ -109,11 +100,8 @@ func (api *TaskClassHandler) UserUpdateTaskClass(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
err = api.svc.AddOrUpdateTaskClass(ctx, &req, userIDInterface, update, intTaskClassID)
|
||||
if err != nil {
|
||||
if errors.Is(err, respond.WrongParamType) || errors.Is(err, respond.UserTaskClassForbidden) {
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.Ok)
|
||||
}
|
||||
@@ -138,13 +126,7 @@ func (api *TaskClassHandler) UserAddTaskClassItemIntoSchedule(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
err = api.svc.AddTaskClassItemIntoSchedule(ctx, &req, userIDInterface, intTaskID)
|
||||
if err != nil {
|
||||
if errors.Is(err, respond.TaskClassItemNotBelongToUser) || errors.Is(err, respond.CourseNotBelongToUser) ||
|
||||
errors.Is(err, respond.CourseAlreadyEmbeddedByOtherTaskBlock) || errors.Is(err, respond.CourseTimeNotMatch) ||
|
||||
errors.Is(err, respond.ScheduleConflict) || errors.Is(err, respond.WrongCourseID) {
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.Ok)
|
||||
|
||||
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -41,13 +40,8 @@ func (th *TaskHandler) AddTask(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
resp, err := th.svc.AddTask(ctx, &req, userID)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.InvalidPriority): //如果是无效刷新令牌或者无效claims或者无效签名方法
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
}
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
//3. 返回响应
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||||
@@ -62,14 +56,8 @@ func (th *TaskHandler) GetUserTasks(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
resp, err := th.svc.GetUserTasks(ctx, userID)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.UserTasksEmpty): //如果任务列表为空
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.UserTasksEmpty, []model.Task{})) //确实没错误,但是任务列表为空,返回自定义响应
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
return
|
||||
}
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
//3. 返回响应
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||||
|
||||
@@ -4,7 +4,6 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -40,15 +39,8 @@ func (api *UserHandler) UserRegister(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
retUser, err := api.svc.UserRegister(ctx, user)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.InvalidName), errors.Is(err, respond.MissingParam),
|
||||
errors.Is(err, respond.ParamTooLong): //如果是无效ID或者缺少参数的错误
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
return
|
||||
}
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, retUser))
|
||||
@@ -66,14 +58,8 @@ func (api *UserHandler) UserLogin(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
tokens, err := api.svc.UserLogin(ctx, &req)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.WrongName), errors.Is(err, respond.WrongPwd): //如果是无效ID或者缺少参数的错误
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
return
|
||||
}
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, tokens))
|
||||
}
|
||||
@@ -94,14 +80,8 @@ func (api *UserHandler) RefreshTokenHandler(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
tokens, err := api.svc.RefreshTokenHandler(ctx, requestBody.RefreshToken)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.InvalidRefreshToken), errors.Is(err, respond.InvalidClaims),
|
||||
errors.Is(err, respond.InvalidTokenSingingMethod), errors.Is(err, respond.UserLoggedOut): //如果是无效刷新令牌或者无效claims或者无效签名方法
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
}
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, tokens))
|
||||
}
|
||||
@@ -116,7 +96,7 @@ func (api *UserHandler) UserLogout(c *gin.Context) {
|
||||
defer cancel() // 记得释放资源
|
||||
err := api.svc.UserLogout(ctx, cl.Jti, cl.ExpiresAt.Time)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
respond.DealWithError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.Ok)
|
||||
|
||||
Reference in New Issue
Block a user