From ea5ba769df1d6a63cea44538d2e44cc1caf59398 Mon Sep 17 00:00:00 2001 From: Damillora Date: Sat, 16 Apr 2022 16:32:04 +0700 Subject: [PATCH] feat: add related tags --- pkg/app/tag_routes.go | 18 +++++++ pkg/services/tag.go | 39 ++++++++++++++ web/app/src/api.js | 5 ++ .../src/components/Post/PostGallery.svelte | 24 +++++++++ .../src/components/Tag/ViewTagPanel.svelte | 34 ++++++++++++ web/app/src/main.scss | 26 +++++---- web/app/src/routes/Post/Posts.svelte | 53 +++++++++++-------- web/app/src/routes/Tags/Tag.svelte | 15 +++++- 8 files changed, 179 insertions(+), 35 deletions(-) create mode 100644 web/app/src/components/Post/PostGallery.svelte diff --git a/pkg/app/tag_routes.go b/pkg/app/tag_routes.go index 969b166..e548c9b 100644 --- a/pkg/app/tag_routes.go +++ b/pkg/app/tag_routes.go @@ -19,6 +19,10 @@ func InitializeTagRoutes(g *gin.Engine) { unprotected.GET("/", tagGet) unprotected.GET("/:tag", tagGetOne) } + related := g.Group("/api/tag-related") + { + related.GET("/:tag", tagGetRelated) + } protected := g.Group("/api/tag").Use(middleware.AuthMiddleware()) { protected.PUT("/:tag/note", tagUpdateNote) @@ -51,6 +55,20 @@ func tagGetOne(c *gin.Context) { }) } +func tagGetRelated(c *gin.Context) { + tag := c.Param("tag") + tagObj, err := services.GetRelatedTags(tag) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse{ + Code: http.StatusBadRequest, + Message: err.Error(), + }) + c.Abort() + } + + c.JSON(http.StatusOK, tagObj) +} + func tagAutocomplete(c *gin.Context) { tags := services.GetTagAutocomplete() c.JSON(http.StatusOK, tags) diff --git a/pkg/services/tag.go b/pkg/services/tag.go index d81f6ec..9a97ed6 100644 --- a/pkg/services/tag.go +++ b/pkg/services/tag.go @@ -2,6 +2,7 @@ package services import ( "errors" + "fmt" "strings" "github.com/Damillora/Shioriko/pkg/database" @@ -225,3 +226,41 @@ func UpdateTag(tagString string, model models.TagUpdateModel) error { return nil } + +func GetRelatedTags(tagSyntax string) ([]models.TagListItem, error) { + tags, err := FindTag(tagSyntax) + + if err != nil { + return nil, err + } + var postIds []string + database.DB. + Model(&tags). + Joins("join post_tags on post_tags.tag_id = tags.id"). + Select("post_tags.post_id"). + Where("post_tags.tag_id = ?", tags.ID). + Find(&postIds) + + var tagIds []string + database.DB. + Model(&tags). + Joins("join post_tags on post_tags.tag_id = tags.id"). + Select("post_tags.tag_id"). + Where("post_tags.post_id IN ?", postIds). + Find(&tagIds) + fmt.Printf("%+v", tags) + fmt.Println() + fmt.Printf("%v", tagIds) + fmt.Println() + + var tagInfo []models.TagListItem + database.DB.Model(&tags). + 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(&tagInfo, tagIds) + + return tagInfo, nil +} diff --git a/web/app/src/api.js b/web/app/src/api.js index b9bdd4d..314d49c 100644 --- a/web/app/src/api.js +++ b/web/app/src/api.js @@ -48,6 +48,11 @@ export async function getTag({ tag }) { return response.data; } +export async function getRelatedTags({ tag }) { + const endpoint = url + "/api/tag-related/" + tag; + const response = await axios.get(endpoint); + return response.data; +} export async function getTagAutocomplete() { const endpoint = url + "/api/tag-autocomplete"; const response = await axios.get(endpoint); diff --git a/web/app/src/components/Post/PostGallery.svelte b/web/app/src/components/Post/PostGallery.svelte new file mode 100644 index 0000000..b77279c --- /dev/null +++ b/web/app/src/components/Post/PostGallery.svelte @@ -0,0 +1,24 @@ + + +
+ {#each posts as post, i (post.id)} +
+
+
+
+
+ + {post.id} + +
+
+
+
+
+ {/each} +
diff --git a/web/app/src/components/Tag/ViewTagPanel.svelte b/web/app/src/components/Tag/ViewTagPanel.svelte index d2febc1..cc73422 100644 --- a/web/app/src/components/Tag/ViewTagPanel.svelte +++ b/web/app/src/components/Tag/ViewTagPanel.svelte @@ -1,10 +1,24 @@
@@ -29,6 +43,26 @@ {data.postCount} (Browse)
+
+
+ Related Tags: +
+
+ +
+
+ {#if tagInfo} +
+

+ Tag: + {tagInfo.tagName.split("_").join(" ")} +

+ {#if tagInfo.tagNote} +
+
+ {tagInfo.tagNote} +
+
+ {/if} +
+ View Tag +
+
+ {/if}
Tags
@@ -127,28 +155,7 @@
-
- {#each posts as post, i (post.id)} -
-
-
-
-
- - {post.id} - -
-
-
-
-
- {/each} -
+
diff --git a/web/app/src/routes/Tags/Tag.svelte b/web/app/src/routes/Tags/Tag.svelte index 915341c..5e4222f 100644 --- a/web/app/src/routes/Tags/Tag.svelte +++ b/web/app/src/routes/Tags/Tag.svelte @@ -1,18 +1,29 @@