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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GetCategories 获取分类列表
|
|
|
|
|
func GetCategories(c *gin.Context) {
|
2025-07-16 08:29:49 +08:00
|
|
|
// 获取分页参数
|
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
|
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
|
search := c.Query("search")
|
|
|
|
|
|
|
|
|
|
var categories []entity.Category
|
|
|
|
|
var total int64
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if search != "" {
|
|
|
|
|
// 搜索分类
|
|
|
|
|
categories, total, err = repoManager.CategoryRepository.Search(search, page, pageSize)
|
|
|
|
|
} else {
|
|
|
|
|
// 分页查询
|
|
|
|
|
categories, total, err = repoManager.CategoryRepository.FindWithPagination(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-16 11:03:55 +08:00
|
|
|
// 获取每个分类的资源数量和标签名称
|
2025-07-10 13:58:28 +08:00
|
|
|
resourceCounts := make(map[uint]int64)
|
2025-07-16 11:03:55 +08:00
|
|
|
tagNamesMap := make(map[uint][]string)
|
2025-07-10 13:58:28 +08:00
|
|
|
for _, category := range categories {
|
2025-07-16 10:54:00 +08:00
|
|
|
// 获取资源数量
|
|
|
|
|
resourceCount, err := repoManager.CategoryRepository.GetResourceCount(category.ID)
|
2025-07-10 13:58:28 +08:00
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2025-07-16 10:54:00 +08:00
|
|
|
resourceCounts[category.ID] = resourceCount
|
|
|
|
|
|
2025-07-16 11:03:55 +08:00
|
|
|
// 获取标签名称
|
|
|
|
|
tagNames, err := repoManager.CategoryRepository.GetTagNames(category.ID)
|
2025-07-16 10:54:00 +08:00
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2025-07-16 11:03:55 +08:00
|
|
|
tagNamesMap[category.ID] = tagNames
|
2025-07-10 13:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-16 11:03:55 +08:00
|
|
|
responses := converter.ToCategoryResponseList(categories, resourceCounts, tagNamesMap)
|
2025-07-16 08:29:49 +08:00
|
|
|
|
|
|
|
|
// 返回分页格式的响应
|
|
|
|
|
SuccessResponse(c, gin.H{
|
|
|
|
|
"items": responses,
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"page_size": pageSize,
|
|
|
|
|
})
|
2025-07-10 13:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateCategory 创建分类
|
|
|
|
|
func CreateCategory(c *gin.Context) {
|
|
|
|
|
var req dto.CreateCategoryRequest
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
category := &entity.Category{
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := repoManager.CategoryRepository.Create(category)
|
|
|
|
|
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-16 11:03:55 +08:00
|
|
|
"category": converter.ToCategoryResponse(category, 0, []string{}),
|
2025-07-10 13:58:28 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateCategory 更新分类
|
|
|
|
|
func UpdateCategory(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.UpdateCategoryRequest
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
category, err := repoManager.CategoryRepository.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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.Name != "" {
|
|
|
|
|
category.Name = req.Name
|
|
|
|
|
}
|
|
|
|
|
if req.Description != "" {
|
|
|
|
|
category.Description = req.Description
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = repoManager.CategoryRepository.Update(category)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteCategory 删除分类
|
|
|
|
|
func DeleteCategory(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.CategoryRepository.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
|
|
|
}
|