mirror of
https://github.com/ctwj/urldb.git
synced 2025-11-25 19:37:33 +08:00
update:category
This commit is contained in:
@@ -52,23 +52,23 @@ func ToResourceResponseList(resources []entity.Resource) []dto.ResourceResponse
|
||||
}
|
||||
|
||||
// ToCategoryResponse 将Category实体转换为CategoryResponse
|
||||
func ToCategoryResponse(category *entity.Category, resourceCount int64, tagCount int64) dto.CategoryResponse {
|
||||
func ToCategoryResponse(category *entity.Category, resourceCount int64, tagNames []string) dto.CategoryResponse {
|
||||
return dto.CategoryResponse{
|
||||
ID: category.ID,
|
||||
Name: category.Name,
|
||||
Description: category.Description,
|
||||
ResourceCount: resourceCount,
|
||||
TagCount: tagCount,
|
||||
TagNames: tagNames,
|
||||
}
|
||||
}
|
||||
|
||||
// ToCategoryResponseList 将Category实体列表转换为CategoryResponse列表
|
||||
func ToCategoryResponseList(categories []entity.Category, resourceCounts map[uint]int64, tagCounts map[uint]int64) []dto.CategoryResponse {
|
||||
func ToCategoryResponseList(categories []entity.Category, resourceCounts map[uint]int64, tagNamesMap map[uint][]string) []dto.CategoryResponse {
|
||||
responses := make([]dto.CategoryResponse, len(categories))
|
||||
for i, category := range categories {
|
||||
resourceCount := resourceCounts[category.ID]
|
||||
tagCount := tagCounts[category.ID]
|
||||
responses[i] = ToCategoryResponse(&category, resourceCount, tagCount)
|
||||
tagNames := tagNamesMap[category.ID]
|
||||
responses[i] = ToCategoryResponse(&category, resourceCount, tagNames)
|
||||
}
|
||||
return responses
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ type CategoryResponse struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
ResourceCount int64 `json:"resource_count"`
|
||||
TagCount int64 `json:"tag_count"`
|
||||
TagNames []string `json:"tag_names"`
|
||||
}
|
||||
|
||||
// TagResponse 标签响应
|
||||
|
||||
@@ -14,6 +14,7 @@ type CategoryRepository interface {
|
||||
FindWithTags() ([]entity.Category, error)
|
||||
GetResourceCount(categoryID uint) (int64, error)
|
||||
GetTagCount(categoryID uint) (int64, error)
|
||||
GetTagNames(categoryID uint) ([]string, error)
|
||||
FindWithPagination(page, pageSize int) ([]entity.Category, int64, error)
|
||||
Search(query string, page, pageSize int) ([]entity.Category, int64, error)
|
||||
}
|
||||
@@ -68,6 +69,21 @@ func (r *CategoryRepositoryImpl) GetTagCount(categoryID uint) (int64, error) {
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetTagNames 获取分类下的标签名称列表
|
||||
func (r *CategoryRepositoryImpl) GetTagNames(categoryID uint) ([]string, error) {
|
||||
var tags []entity.Tag
|
||||
err := r.db.Model(&entity.Tag{}).Where("category_id = ?", categoryID).Select("name").Find(&tags).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
names := make([]string, len(tags))
|
||||
for i, tag := range tags {
|
||||
names[i] = tag.Name
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// FindWithPagination 分页查询分类
|
||||
func (r *CategoryRepositoryImpl) FindWithPagination(page, pageSize int) ([]entity.Category, int64, error) {
|
||||
var categories []entity.Category
|
||||
|
||||
@@ -35,9 +35,9 @@ func GetCategories(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取每个分类的资源数量和标签数量
|
||||
// 获取每个分类的资源数量和标签名称
|
||||
resourceCounts := make(map[uint]int64)
|
||||
tagCounts := make(map[uint]int64)
|
||||
tagNamesMap := make(map[uint][]string)
|
||||
for _, category := range categories {
|
||||
// 获取资源数量
|
||||
resourceCount, err := repoManager.CategoryRepository.GetResourceCount(category.ID)
|
||||
@@ -46,15 +46,15 @@ func GetCategories(c *gin.Context) {
|
||||
}
|
||||
resourceCounts[category.ID] = resourceCount
|
||||
|
||||
// 获取标签数量
|
||||
tagCount, err := repoManager.CategoryRepository.GetTagCount(category.ID)
|
||||
// 获取标签名称
|
||||
tagNames, err := repoManager.CategoryRepository.GetTagNames(category.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
tagCounts[category.ID] = tagCount
|
||||
tagNamesMap[category.ID] = tagNames
|
||||
}
|
||||
|
||||
responses := converter.ToCategoryResponseList(categories, resourceCounts, tagCounts)
|
||||
responses := converter.ToCategoryResponseList(categories, resourceCounts, tagNamesMap)
|
||||
|
||||
// 返回分页格式的响应
|
||||
SuccessResponse(c, gin.H{
|
||||
@@ -86,7 +86,7 @@ func CreateCategory(c *gin.Context) {
|
||||
|
||||
SuccessResponse(c, gin.H{
|
||||
"message": "分类创建成功",
|
||||
"category": converter.ToCategoryResponse(category, 0, 0),
|
||||
"category": converter.ToCategoryResponse(category, 0, []string{}),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">分类名称</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">描述</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">资源数量</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">创建时间</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">关联标签</th>
|
||||
<th class="px-4 py-3 text-left text-sm font-medium">操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -120,7 +120,10 @@
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-600 dark:text-gray-400">
|
||||
{{ formatTime(category.create_time) }}
|
||||
<span v-if="category.tag_names && category.tag_names.length > 0" class="text-gray-800 dark:text-gray-200">
|
||||
{{ category.tag_names.join(', ') }}
|
||||
</span>
|
||||
<span v-else class="text-gray-400 dark:text-gray-500 italic text-xs">无标签</span>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-sm">
|
||||
<div class="flex items-center gap-2">
|
||||
|
||||
Reference in New Issue
Block a user