feat: fix filters

This commit is contained in:
Damillora 2022-04-16 17:41:50 +07:00
parent 9db1fa5bb8
commit a0868e15a1
4 changed files with 58 additions and 9 deletions

View File

@ -57,11 +57,11 @@ func postGet(c *gin.Context) {
}
var postResult []models.PostListItem
var tagStrings []string
var tagObjs []database.Tag
for _, post := range posts {
for _, tag := range post.Tags {
tagStrings = append(tagStrings, tag.TagType.Name+":"+tag.Name)
tagObjs = append(tagObjs, tag)
}
postResult = append(postResult, models.PostListItem{
@ -70,14 +70,14 @@ func postGet(c *gin.Context) {
ImagePath: "/data/" + post.Blob.FilePath,
})
}
tagObjs := services.GetTagFilterString(tagStrings)
tagFilters := services.GetTagFilter(tagObjs)
c.JSON(http.StatusOK, models.PostPaginationResponse{
CurrentPage: page,
TotalPage: totalPage,
PostCount: postPages,
Posts: postResult,
Tags: tagObjs,
Tags: tagFilters,
})
}

View File

@ -1,6 +1,8 @@
package services
import (
"fmt"
"github.com/Damillora/Shioriko/pkg/database"
"github.com/Damillora/Shioriko/pkg/models"
"github.com/google/uuid"
@ -20,8 +22,34 @@ func GetPostTags(page int, tagSyntax []string) []database.Post {
if err != nil {
return []database.Post{}
}
var tagIds []string
for _, tag := range tags {
tagIds = append(tagIds, tag.ID)
}
fmt.Printf("%v", tagIds)
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 IN ?", tagIds).
Group("post_tags.post_id").
Having("count(*) = ?", len(tagIds)).
Distinct().
Find(&postIds)
var posts []database.Post
database.DB.Model(&tags).Distinct().Joins("Blob").Preload("Tags").Preload("Tags.TagType").Order("created_at desc").Offset((page - 1) * perPage).Limit(20).Association("Posts").Find(&posts)
database.DB.
Joins("Blob").
Preload("Tags").
Preload("Tags.TagType").
Where("posts.id IN ?", postIds).
Order("created_at desc").
Offset((page - 1) * perPage).
Limit(20).
Find(&posts)
return posts
}
@ -95,8 +123,23 @@ func CountPostPagesTag(tagSyntax []string) int {
return 0
}
var tagIds []string
for _, tag := range tags {
tagIds = append(tagIds, tag.ID)
}
fmt.Printf("%v", tagIds)
var count int64
count = database.DB.Model(&tags).Distinct().Joins("Blob").Preload("Tags").Preload("Tags.TagType").Association("Posts").Count()
database.DB.
Model(&tags).
Distinct().
Joins("join post_tags on post_tags.tag_id = tags.id").
Select("post_tags.post_id").
Where("post_tags.tag_id IN ?", tagIds).
Group("post_tags.post_id").
Having("count(*) = ?", len(tagIds)).
Count(&count)
return int(count)
}

View File

@ -34,7 +34,7 @@
<div class="container has-text-centered">
<form on:submit|preventDefault={onSearch}>
<div class="field has-addons">
<div class="control is-expanded">
<div class="control has-text-left is-expanded">
<div class="control" id="tags">
<Tags
tags={searchTerms}

View File

@ -24,7 +24,13 @@
const data = await getPostSearchTag({ page, q: searchTerms.join("+") });
if (data.posts) {
posts = data.posts;
tags = data.tags.sort((a, b) => b.postCount - a.postCount);
tags = data.tags
.filter(
(x) =>
!searchTerms.includes(x.tagName) &&
!searchTerms.includes(x.tagType + ":" + x.tagName)
)
.sort((a, b) => b.postCount - a.postCount);
totalPages = data.totalPage;
postCount = data.postCount;
pagination = paginate(page, totalPages);
@ -113,7 +119,7 @@
{#if tagInfo}
<div class="panel is-info">
<p class="panel-heading">
Tag:
Tag:
{tagInfo.tagName.split("_").join(" ")}
</p>
{#if tagInfo.tagNote}