Feat: task list in dashboard

pull/247/head
HFO4 5 years ago
parent 3b5af8da47
commit 809af5bf4e

@ -389,3 +389,69 @@ func AdminDeleteShare(c *gin.Context) {
c.JSON(200, ErrorResponse(err))
}
}
// AdminListOrder 列出订单
func AdminListOrder(c *gin.Context) {
var service admin.AdminListService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Orders()
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// AdminDeleteOrder 批量删除订单
func AdminDeleteOrder(c *gin.Context) {
var service admin.OrderBatchService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Delete(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// AdminListDownload 列出离线下载任务
func AdminListDownload(c *gin.Context) {
var service admin.AdminListService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Downloads()
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// AdminDeleteDownload 批量删除任务
func AdminDeleteDownload(c *gin.Context) {
var service admin.TaskBatchService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Delete(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// AdminListTask 列出任务
func AdminListTask(c *gin.Context) {
var service admin.AdminListService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Tasks()
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// AdminDeleteTask 批量删除任务
func AdminDeleteTask(c *gin.Context) {
var service admin.TaskBatchService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.DeleteGeneral(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}

@ -397,6 +397,30 @@ func InitMasterRouter() *gin.Engine {
share.POST("delete", controllers.AdminDeleteShare)
}
order := admin.Group("order")
{
// 列出订单
order.POST("list", controllers.AdminListOrder)
// 删除
order.POST("delete", controllers.AdminDeleteOrder)
}
download := admin.Group("download")
{
// 列出任务
download.POST("list", controllers.AdminListDownload)
// 删除
download.POST("delete", controllers.AdminDeleteDownload)
}
task := admin.Group("task")
{
// 列出任务
task.POST("list", controllers.AdminListTask)
// 删除
task.POST("delete", controllers.AdminDeleteTask)
}
}
// 用户

@ -0,0 +1,75 @@
package admin
import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
"strings"
)
// OrderBatchService 订单批量操作服务
type OrderBatchService struct {
ID []uint `json:"id" binding:"min=1"`
}
// Delete 删除订单
func (service *OrderBatchService) Delete(c *gin.Context) serializer.Response {
if err := model.DB.Where("id in (?)", service.ID).Delete(&model.Order{}).Error; err != nil {
return serializer.DBErr("无法删除订单", err)
}
return serializer.Response{}
}
// Orders 列出订单
func (service *AdminListService) Orders() serializer.Response {
var res []model.Order
total := 0
tx := model.DB.Model(&model.Order{})
if service.OrderBy != "" {
tx = tx.Order(service.OrderBy)
}
for k, v := range service.Conditions {
tx = tx.Where(k+" = ?", v)
}
if len(service.Searches) > 0 {
search := ""
for k, v := range service.Searches {
search += k + " like '%" + v + "%' OR "
}
search = strings.TrimSuffix(search, " OR ")
tx = tx.Where(search)
}
// 计算总数用于分页
tx.Count(&total)
// 查询记录
tx.Limit(service.PageSize).Offset((service.Page - 1) * service.PageSize).Find(&res)
// 查询对应用户同时计算HashID
users := make(map[uint]model.User)
for _, file := range res {
users[file.UserID] = model.User{}
}
userIDs := make([]uint, 0, len(users))
for k := range users {
userIDs = append(userIDs, k)
}
var userList []model.User
model.DB.Where("id in (?)", userIDs).Find(&userList)
for _, v := range userList {
users[v.ID] = v
}
return serializer.Response{Data: map[string]interface{}{
"total": total,
"items": res,
"users": users,
}}
}

@ -0,0 +1,137 @@
package admin
import (
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
"strings"
)
// TaskBatchService 任务批量操作服务
type TaskBatchService struct {
ID []uint `json:"id" binding:"min=1"`
}
// Delete 删除任务
func (service *TaskBatchService) Delete(c *gin.Context) serializer.Response {
if err := model.DB.Where("id in (?)", service.ID).Delete(&model.Download{}).Error; err != nil {
return serializer.DBErr("无法删除任务", err)
}
return serializer.Response{}
}
// DeleteGeneral 删除常规任务
func (service *TaskBatchService) DeleteGeneral(c *gin.Context) serializer.Response {
if err := model.DB.Where("id in (?)", service.ID).Delete(&model.Task{}).Error; err != nil {
return serializer.DBErr("无法删除任务", err)
}
return serializer.Response{}
}
// Tasks 列出常规任务
func (service *AdminListService) Tasks() serializer.Response {
var res []model.Task
total := 0
tx := model.DB.Model(&model.Task{})
if service.OrderBy != "" {
tx = tx.Order(service.OrderBy)
}
for k, v := range service.Conditions {
tx = tx.Where(k+" = ?", v)
}
if len(service.Searches) > 0 {
search := ""
for k, v := range service.Searches {
search += k + " like '%" + v + "%' OR "
}
search = strings.TrimSuffix(search, " OR ")
tx = tx.Where(search)
}
// 计算总数用于分页
tx.Count(&total)
// 查询记录
tx.Limit(service.PageSize).Offset((service.Page - 1) * service.PageSize).Find(&res)
// 查询对应用户同时计算HashID
users := make(map[uint]model.User)
for _, file := range res {
users[file.UserID] = model.User{}
}
userIDs := make([]uint, 0, len(users))
for k := range users {
userIDs = append(userIDs, k)
}
var userList []model.User
model.DB.Where("id in (?)", userIDs).Find(&userList)
for _, v := range userList {
users[v.ID] = v
}
return serializer.Response{Data: map[string]interface{}{
"total": total,
"items": res,
"users": users,
}}
}
// Downloads 列出离线下载任务
func (service *AdminListService) Downloads() serializer.Response {
var res []model.Download
total := 0
tx := model.DB.Model(&model.Download{})
if service.OrderBy != "" {
tx = tx.Order(service.OrderBy)
}
for k, v := range service.Conditions {
tx = tx.Where(k+" = ?", v)
}
if len(service.Searches) > 0 {
search := ""
for k, v := range service.Searches {
search += k + " like '%" + v + "%' OR "
}
search = strings.TrimSuffix(search, " OR ")
tx = tx.Where(search)
}
// 计算总数用于分页
tx.Count(&total)
// 查询记录
tx.Limit(service.PageSize).Offset((service.Page - 1) * service.PageSize).Find(&res)
// 查询对应用户同时计算HashID
users := make(map[uint]model.User)
for _, file := range res {
users[file.UserID] = model.User{}
}
userIDs := make([]uint, 0, len(users))
for k := range users {
userIDs = append(userIDs, k)
}
var userList []model.User
model.DB.Where("id in (?)", userIDs).Find(&userList)
for _, v := range userList {
users[v.ID] = v
}
return serializer.Response{Data: map[string]interface{}{
"total": total,
"items": res,
"users": users,
}}
}
Loading…
Cancel
Save