diff --git a/pkg/app/post_routes.go b/pkg/app/post_routes.go index e77cf2a..2315544 100644 --- a/pkg/app/post_routes.go +++ b/pkg/app/post_routes.go @@ -70,12 +70,14 @@ func postGet(c *gin.Context) { ImagePath: "/data/" + post.Blob.FilePath, }) } + tagObjs := services.GetTagFilter(tagStrings) + c.JSON(http.StatusOK, models.PostPaginationResponse{ CurrentPage: page, TotalPage: totalPage, PostCount: postPages, Posts: postResult, - Tags: tagStrings, + Tags: tagObjs, }) } diff --git a/pkg/models/responses.go b/pkg/models/responses.go index e0b8eb9..c102881 100644 --- a/pkg/models/responses.go +++ b/pkg/models/responses.go @@ -31,5 +31,5 @@ type PostPaginationResponse struct { TotalPage int `json:"totalPage"` PostCount int `json:"postCount"` Posts []PostListItem `json:"posts"` - Tags []string `json:"tags"` + Tags []TagListItem `json:"tags"` } diff --git a/pkg/services/tag.go b/pkg/services/tag.go index f4f616b..9f5fff2 100644 --- a/pkg/services/tag.go +++ b/pkg/services/tag.go @@ -21,6 +21,24 @@ func GetTagAll() []models.TagListItem { return tags } +func GetTagFilter(tagString []string) []models.TagListItem { + tagObjs, _ := ParseReadTags(tagString) + var tagIds []string + for _, val := range tagObjs { + tagIds = append(tagIds, val.ID) + } + + var tags []models.TagListItem + database.DB.Model(&tagObjs). + Joins("join tag_types on tag_types.id = tags.tag_type_id"). + Joins("left join post_tags on post_tags.tag_id = tags.id"). + Select("tags.id as tag_id, tags.name as tag_name, tag_types.name as tag_type, count(post_tags.post_id) as post_count"). + Group("tags.id, tags.name, tag_types.name"). + Order("post_count DESC"). + Find(&tags, tagIds) + return tags +} + func GetTagAutocomplete() []string { var tags []string result := database.DB.Model(&database.Tag{}). diff --git a/web/app/src/routes/Posts.svelte b/web/app/src/routes/Posts.svelte index da735ae..3d8b717 100644 --- a/web/app/src/routes/Posts.svelte +++ b/web/app/src/routes/Posts.svelte @@ -24,7 +24,7 @@ const data = await getPostSearchTag({ page, q: searchTerms.join("+") }); if (data.posts) { posts = data.posts; - tags = data.tags; + tags = data.tags.sort((a, b) => b.postCount - a.postCount); totalPages = data.totalPage; pagination = paginate(page, totalPages); } else { @@ -34,17 +34,6 @@ pagination = paginate(page, totalPages); } }; - $: { - let catTags = tags.reduce( - (acc, o) => ((acc[o] = (acc[o] || 0) + 1), acc), - {} - ); - categorizedTags = Object.entries(catTags).map(([k, v]) => ({ - tag: k, - num: v, - })); - categorizedTags = categorizedTags.sort((a, b) => b.num - a.num); - } let queryParams; const onTagChange = (value) => { @@ -112,12 +101,12 @@