perf(task): merge requests of operating selected (#7637)

pull/7639/head
KirCute_ECT 2024-12-10 19:30:50 +08:00 committed by GitHub
parent a3908fd9a6
commit 7341846499
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 0 deletions

View File

@ -90,6 +90,31 @@ func getTargetedHandler[T task.TaskInfoWithCreator](manager *tache.Manager[T], c
}
}
func getBatchHandler[T task.TaskInfoWithCreator](manager *tache.Manager[T], callback func(task T)) gin.HandlerFunc {
return func(c *gin.Context) {
isAdmin, uid, ok := getUserInfo(c)
if !ok {
common.ErrorStrResp(c, "user invalid", 401)
return
}
var tids []string
if err := c.ShouldBind(&tids); err != nil {
common.ErrorStrResp(c, "invalid request format", 400)
return
}
retErrs := make(map[string]string)
for _, tid := range tids {
t, ok := manager.GetByID(tid)
if !ok || (!isAdmin && uid != t.GetCreator().ID) {
retErrs[tid] = "task not found"
continue
}
callback(t)
}
common.SuccessResp(c, retErrs)
}
}
func taskRoute[T task.TaskInfoWithCreator](g *gin.RouterGroup, manager *tache.Manager[T]) {
g.GET("/undone", func(c *gin.Context) {
isAdmin, uid, ok := getUserInfo(c)
@ -132,6 +157,15 @@ func taskRoute[T task.TaskInfoWithCreator](g *gin.RouterGroup, manager *tache.Ma
manager.Retry(task.GetID())
common.SuccessResp(c)
}))
g.POST("/cancel_some", getBatchHandler(manager, func(task T) {
manager.Cancel(task.GetID())
}))
g.POST("/delete_some", getBatchHandler(manager, func(task T) {
manager.Remove(task.GetID())
}))
g.POST("/retry_some", getBatchHandler(manager, func(task T) {
manager.Retry(task.GetID())
}))
g.POST("/clear_done", func(c *gin.Context) {
isAdmin, uid, ok := getUserInfo(c)
if !ok {