2025-07-10 13:58:28 +08:00
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
2025-07-17 14:08:52 +08:00
|
|
|
|
"github.com/ctwj/panResManage/db/converter"
|
|
|
|
|
|
"github.com/ctwj/panResManage/db/dto"
|
|
|
|
|
|
"github.com/ctwj/panResManage/db/entity"
|
2025-07-10 13:58:28 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// GetResources 获取资源列表
|
|
|
|
|
|
func GetResources(c *gin.Context) {
|
|
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
2025-07-11 17:45:16 +08:00
|
|
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
|
|
categoryID := c.Query("category_id")
|
2025-07-15 12:50:24 +08:00
|
|
|
|
panID := c.Query("pan_id")
|
2025-07-11 17:45:16 +08:00
|
|
|
|
search := c.Query("search")
|
2025-07-10 13:58:28 +08:00
|
|
|
|
|
|
|
|
|
|
var resources []entity.Resource
|
2025-07-11 17:45:16 +08:00
|
|
|
|
var total int64
|
2025-07-10 13:58:28 +08:00
|
|
|
|
var err error
|
|
|
|
|
|
|
2025-07-11 18:37:28 +08:00
|
|
|
|
// 设置响应头,启用缓存
|
|
|
|
|
|
c.Header("Cache-Control", "public, max-age=300") // 5分钟缓存
|
|
|
|
|
|
|
2025-07-15 12:50:24 +08:00
|
|
|
|
if search != "" && panID != "" {
|
|
|
|
|
|
// 平台内搜索
|
|
|
|
|
|
panIDUint, _ := strconv.ParseUint(panID, 10, 32)
|
|
|
|
|
|
resources, total, err = repoManager.ResourceRepository.SearchByPanID(search, uint(panIDUint), page, pageSize)
|
|
|
|
|
|
} else if search != "" {
|
|
|
|
|
|
// 全局搜索
|
2025-07-11 17:45:16 +08:00
|
|
|
|
resources, total, err = repoManager.ResourceRepository.Search(search, nil, page, pageSize)
|
2025-07-15 12:50:24 +08:00
|
|
|
|
} else if panID != "" {
|
|
|
|
|
|
// 按平台筛选
|
|
|
|
|
|
panIDUint, _ := strconv.ParseUint(panID, 10, 32)
|
|
|
|
|
|
resources, total, err = repoManager.ResourceRepository.FindByPanIDPaginated(uint(panIDUint), page, pageSize)
|
2025-07-11 17:45:16 +08:00
|
|
|
|
} else if categoryID != "" {
|
2025-07-15 12:50:24 +08:00
|
|
|
|
// 按分类筛选
|
2025-07-11 17:45:16 +08:00
|
|
|
|
categoryIDUint, _ := strconv.ParseUint(categoryID, 10, 32)
|
|
|
|
|
|
resources, total, err = repoManager.ResourceRepository.FindByCategoryIDPaginated(uint(categoryIDUint), page, pageSize)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
} else {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
// 使用分页查询,避免加载所有数据
|
|
|
|
|
|
resources, total, err = repoManager.ResourceRepository.FindWithRelationsPaginated(page, pageSize)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, gin.H{
|
|
|
|
|
|
"resources": converter.ToResourceResponseList(resources),
|
|
|
|
|
|
"total": total,
|
2025-07-10 13:58:28 +08:00
|
|
|
|
"page": page,
|
2025-07-11 17:45:16 +08:00
|
|
|
|
"page_size": pageSize,
|
2025-07-10 13:58:28 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-12 21:23:23 +08:00
|
|
|
|
// GetResourceByID 根据ID获取资源
|
2025-07-10 13:58:28 +08:00
|
|
|
|
func GetResourceByID(c *gin.Context) {
|
|
|
|
|
|
idStr := c.Param("id")
|
|
|
|
|
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "无效的ID", http.StatusBadRequest)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resource, err := repoManager.ResourceRepository.FindByID(uint(id))
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "资源不存在", http.StatusNotFound)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response := converter.ToResourceResponse(resource)
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, response)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-12 21:23:23 +08:00
|
|
|
|
// CheckResourceExists 检查资源是否存在(测试FindExists函数)
|
|
|
|
|
|
func CheckResourceExists(c *gin.Context) {
|
|
|
|
|
|
url := c.Query("url")
|
|
|
|
|
|
if url == "" {
|
|
|
|
|
|
ErrorResponse(c, "URL参数不能为空", http.StatusBadRequest)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
excludeIDStr := c.Query("exclude_id")
|
|
|
|
|
|
var excludeID uint
|
|
|
|
|
|
if excludeIDStr != "" {
|
|
|
|
|
|
if id, err := strconv.ParseUint(excludeIDStr, 10, 32); err == nil {
|
|
|
|
|
|
excludeID = uint(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exists, err := repoManager.ResourceRepository.FindExists(url, excludeID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
ErrorResponse(c, "检查失败: "+err.Error(), http.StatusInternalServerError)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SuccessResponse(c, gin.H{
|
|
|
|
|
|
"url": url,
|
|
|
|
|
|
"exists": exists,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-10 13:58:28 +08:00
|
|
|
|
// CreateResource 创建资源
|
|
|
|
|
|
func CreateResource(c *gin.Context) {
|
|
|
|
|
|
var req dto.CreateResourceRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusBadRequest)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resource := &entity.Resource{
|
|
|
|
|
|
Title: req.Title,
|
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
|
URL: req.URL,
|
|
|
|
|
|
PanID: req.PanID,
|
|
|
|
|
|
QuarkURL: req.QuarkURL,
|
|
|
|
|
|
FileSize: req.FileSize,
|
|
|
|
|
|
CategoryID: req.CategoryID,
|
|
|
|
|
|
IsValid: req.IsValid,
|
|
|
|
|
|
IsPublic: req.IsPublic,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err := repoManager.ResourceRepository.Create(resource)
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理标签关联
|
|
|
|
|
|
if len(req.TagIDs) > 0 {
|
|
|
|
|
|
err = repoManager.ResourceRepository.UpdateWithTags(resource, req.TagIDs)
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, gin.H{
|
|
|
|
|
|
"message": "资源创建成功",
|
|
|
|
|
|
"resource": converter.ToResourceResponse(resource),
|
2025-07-10 13:58:28 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateResource 更新资源
|
|
|
|
|
|
func UpdateResource(c *gin.Context) {
|
|
|
|
|
|
idStr := c.Param("id")
|
|
|
|
|
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "无效的ID", http.StatusBadRequest)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req dto.UpdateResourceRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusBadRequest)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resource, err := repoManager.ResourceRepository.FindByID(uint(id))
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "资源不存在", http.StatusNotFound)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
// 更新资源信息
|
2025-07-10 13:58:28 +08:00
|
|
|
|
if req.Title != "" {
|
|
|
|
|
|
resource.Title = req.Title
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.Description != "" {
|
|
|
|
|
|
resource.Description = req.Description
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.URL != "" {
|
|
|
|
|
|
resource.URL = req.URL
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.PanID != nil {
|
|
|
|
|
|
resource.PanID = req.PanID
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.QuarkURL != "" {
|
|
|
|
|
|
resource.QuarkURL = req.QuarkURL
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.FileSize != "" {
|
|
|
|
|
|
resource.FileSize = req.FileSize
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.CategoryID != nil {
|
|
|
|
|
|
resource.CategoryID = req.CategoryID
|
|
|
|
|
|
}
|
|
|
|
|
|
resource.IsValid = req.IsValid
|
|
|
|
|
|
resource.IsPublic = req.IsPublic
|
|
|
|
|
|
|
|
|
|
|
|
// 处理标签关联
|
2025-07-11 17:45:16 +08:00
|
|
|
|
if len(req.TagIDs) > 0 {
|
2025-07-10 13:58:28 +08:00
|
|
|
|
err = repoManager.ResourceRepository.UpdateWithTags(resource, req.TagIDs)
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
err = repoManager.ResourceRepository.Update(resource)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, gin.H{"message": "资源更新成功"})
|
2025-07-10 13:58:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteResource 删除资源
|
|
|
|
|
|
func DeleteResource(c *gin.Context) {
|
|
|
|
|
|
idStr := c.Param("id")
|
|
|
|
|
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "无效的ID", http.StatusBadRequest)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = repoManager.ResourceRepository.Delete(uint(id))
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, gin.H{"message": "资源删除成功"})
|
2025-07-10 13:58:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SearchResources 搜索资源
|
|
|
|
|
|
func SearchResources(c *gin.Context) {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
query := c.Query("q")
|
2025-07-10 13:58:28 +08:00
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
2025-07-11 17:45:16 +08:00
|
|
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
2025-07-10 13:58:28 +08:00
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
var resources []entity.Resource
|
|
|
|
|
|
var total int64
|
|
|
|
|
|
var err error
|
2025-07-10 13:58:28 +08:00
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
if query == "" {
|
|
|
|
|
|
// 搜索关键词为空时,返回最新记录(分页)
|
|
|
|
|
|
resources, total, err = repoManager.ResourceRepository.FindWithRelationsPaginated(page, pageSize)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 有搜索关键词时,执行搜索
|
|
|
|
|
|
resources, total, err = repoManager.ResourceRepository.Search(query, nil, page, pageSize)
|
2025-07-10 21:14:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-10 13:58:28 +08:00
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
2025-07-10 13:58:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, gin.H{
|
|
|
|
|
|
"resources": converter.ToResourceResponseList(resources),
|
|
|
|
|
|
"total": total,
|
|
|
|
|
|
"page": page,
|
|
|
|
|
|
"page_size": pageSize,
|
2025-07-10 13:58:28 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|