diff --git a/pkg/app/tag_routes.go b/pkg/app/tag_routes.go index 4c10029..8fe25c2 100644 --- a/pkg/app/tag_routes.go +++ b/pkg/app/tag_routes.go @@ -3,7 +3,6 @@ package app import ( "net/http" - "github.com/Damillora/Shioriko/pkg/models" "github.com/Damillora/Shioriko/pkg/services" "github.com/gin-gonic/gin" ) @@ -17,13 +16,5 @@ func InitializeTagRoutes(g *gin.Engine) { func tagGet(c *gin.Context) { tags := services.GetTagAll() - var tagResult []models.TagListItem - for _, tag := range tags { - tagResult = append(tagResult, models.TagListItem{ - ID: tag.ID, - Name: tag.Name, - TagType: tag.TagType.Name, - }) - } - c.JSON(http.StatusOK, tagResult) + c.JSON(http.StatusOK, tags) } diff --git a/pkg/models/item.go b/pkg/models/item.go index 6c5796d..ca6d6b3 100644 --- a/pkg/models/item.go +++ b/pkg/models/item.go @@ -6,9 +6,10 @@ type TagTypeListItem struct { } type TagListItem struct { - ID string `json:"id"` - Name string `json:"name"` - TagType string `json:"tagType"` + TagID string `json:"tagId"` + TagName string `json:"tagName"` + TagType string `json:"tagType"` + PostCount int `json:"postCount"` } type PostListItem struct { diff --git a/pkg/services/tag.go b/pkg/services/tag.go index 7a45171..f9f6c5c 100644 --- a/pkg/services/tag.go +++ b/pkg/services/tag.go @@ -5,12 +5,19 @@ import ( "strings" "github.com/Damillora/Shioriko/pkg/database" + "github.com/Damillora/Shioriko/pkg/models" "github.com/google/uuid" ) -func GetTagAll() []database.Tag { - var tags []database.Tag - database.DB.Joins("TagType").Find(&tags) +func GetTagAll() []models.TagListItem { + var tags []models.TagListItem + database.DB.Model(&database.Tag{}). + 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) return tags } diff --git a/web/app/src/Navbar.svelte b/web/app/src/Navbar.svelte index 3a8a240..36e3b6f 100644 --- a/web/app/src/Navbar.svelte +++ b/web/app/src/Navbar.svelte @@ -35,6 +35,7 @@