Feat: list tasks
This commit is contained in:
parent
b4219927d6
commit
f91792bc64
7 changed files with 71 additions and 3 deletions
|
@ -54,3 +54,21 @@ func GetTasksByID(id interface{}) (*Task, error) {
|
|||
result := DB.Where("id = ?", id).First(task)
|
||||
return task, result.Error
|
||||
}
|
||||
|
||||
// ListTasks 列出用户所属的任务
|
||||
func ListTasks(uid uint, page, pageSize int, order string) ([]Task, int) {
|
||||
var (
|
||||
tasks []Task
|
||||
total int
|
||||
)
|
||||
dbChain := DB
|
||||
dbChain = dbChain.Where("user_id = ?", uid)
|
||||
|
||||
// 计算总数用于分页
|
||||
dbChain.Model(&Share{}).Count(&total)
|
||||
|
||||
// 查询记录
|
||||
dbChain.Limit(pageSize).Offset((page - 1) * pageSize).Order(order).Find(&tasks)
|
||||
|
||||
return tasks, total
|
||||
}
|
||||
|
|
|
@ -54,9 +54,8 @@ type User struct {
|
|||
|
||||
// UserOption 用户个性化配置字段
|
||||
type UserOption struct {
|
||||
ProfileOn int `json:"profile_on"`
|
||||
ProfileOff int `json:"profile_off,omitempty"`
|
||||
PreferredPolicy uint `json:"preferred_policy"`
|
||||
WebDAVKey string `json:"webdav_key"`
|
||||
PreferredTheme string `json:"preferred_theme"`
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,33 @@ type SiteConfig struct {
|
|||
User User `json:"user"`
|
||||
}
|
||||
|
||||
type task struct {
|
||||
Status int `json:"status"`
|
||||
Type int `json:"type"`
|
||||
CreateDate string `json:"create_date"`
|
||||
Progress int `json:"progress"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// BuildTaskList 构建任务列表响应
|
||||
func BuildTaskList(tasks []model.Task, total int) Response {
|
||||
res := make([]task, 0, len(tasks))
|
||||
for _, t := range tasks {
|
||||
res = append(res, task{
|
||||
Status: t.Status,
|
||||
Type: t.Type,
|
||||
CreateDate: t.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
Progress: t.Progress,
|
||||
Error: t.Error,
|
||||
})
|
||||
}
|
||||
|
||||
return Response{Data: map[string]interface{}{
|
||||
"total": total,
|
||||
"tasks": res,
|
||||
}}
|
||||
}
|
||||
|
||||
func checkSettingValue(setting map[string]string, key string) string {
|
||||
if v, ok := setting[key]; ok {
|
||||
return v
|
||||
|
|
|
@ -80,6 +80,7 @@ func (job *DecompressTask) Do() {
|
|||
}
|
||||
defer fs.Recycle()
|
||||
|
||||
job.TaskModel.SetProgress(DecompressingProgress)
|
||||
err = fs.Decompress(context.Background(), job.TaskProps.Src, job.TaskProps.Dst)
|
||||
if err != nil {
|
||||
job.SetErrorMsg("解压缩失败", err)
|
||||
|
|
|
@ -145,5 +145,15 @@ func UserAvailablePolicies(c *gin.Context) {
|
|||
} else {
|
||||
c.JSON(200, ErrorResponse(err))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// UserTasks 获取任务队列
|
||||
func UserTasks(c *gin.Context) {
|
||||
var service user.SettingListService
|
||||
if err := c.ShouldBindQuery(&service); err == nil {
|
||||
res := service.ListTasks(c, CurrentUser(c))
|
||||
c.JSON(200, res)
|
||||
} else {
|
||||
c.JSON(200, ErrorResponse(err))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -259,6 +259,8 @@ func InitMasterRouter() *gin.Engine {
|
|||
{
|
||||
// 获取用户可选存储策略
|
||||
setting.GET("policies", controllers.UserAvailablePolicies)
|
||||
// 任务队列
|
||||
setting.GET("tasks", controllers.UserTasks)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,17 @@ import (
|
|||
type SettingService struct {
|
||||
}
|
||||
|
||||
// SettingListService 通用设置列表服务
|
||||
type SettingListService struct {
|
||||
Page int `form:"page" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// ListTasks 列出任务
|
||||
func (service *SettingListService) ListTasks(c *gin.Context, user *model.User) serializer.Response {
|
||||
tasks, total := model.ListTasks(user.ID, service.Page, 10, "updated_at desc")
|
||||
return serializer.BuildTaskList(tasks, total)
|
||||
}
|
||||
|
||||
// Policy 获取用户存储策略设置
|
||||
func (service *SettingService) Policy(c *gin.Context, user *model.User) serializer.Response {
|
||||
// 取得用户可用存储策略
|
||||
|
|
Loading…
Add table
Reference in a new issue