feat: add tag count to post detail as well

This commit is contained in:
Damillora 2022-04-15 00:37:03 +07:00
parent e73d3d0dda
commit 36db313d33
6 changed files with 25 additions and 32 deletions

View File

@ -70,7 +70,7 @@ func postGet(c *gin.Context) {
ImagePath: "/data/" + post.Blob.FilePath,
})
}
tagObjs := services.GetTagFilter(tagStrings)
tagObjs := services.GetTagFilterString(tagStrings)
c.JSON(http.StatusOK, models.PostPaginationResponse{
CurrentPage: page,
@ -90,17 +90,15 @@ func postGetOne(c *gin.Context) {
Message: err.Error(),
})
}
var tagStrings []string
for _, tag := range post.Tags {
tagStrings = append(tagStrings, tag.TagType.Name+":"+tag.Name)
}
tagObjs := services.GetTagFilter(post.Tags)
c.JSON(http.StatusOK, models.PostReadModel{
ID: post.ID,
ImagePreviewPath: "/data/" + post.Blob.PreviewFilePath,
ImagePath: "/data/" + post.Blob.FilePath,
SourceURL: post.SourceURL,
Tags: tagStrings,
Tags: tagObjs,
Width: post.Blob.Width,
Height: post.Blob.Height,
Uploader: post.User.Username,

View File

@ -1,12 +1,12 @@
package models
type PostReadModel struct {
ID string `json:"id"`
ImagePreviewPath string `json:"preview_path"`
ImagePath string `json:"image_path"`
SourceURL string `json:"source_url"`
Tags []string `json:"tags"`
Width int `json:"width"`
Height int `json:"height"`
Uploader string `json:"uploader"`
ID string `json:"id"`
ImagePreviewPath string `json:"preview_path"`
ImagePath string `json:"image_path"`
SourceURL string `json:"source_url"`
Tags []TagListItem `json:"tags"`
Width int `json:"width"`
Height int `json:"height"`
Uploader string `json:"uploader"`
}

View File

@ -21,8 +21,12 @@ func GetTagAll() []models.TagListItem {
return tags
}
func GetTagFilter(tagString []string) []models.TagListItem {
func GetTagFilterString(tagString []string) []models.TagListItem {
tagObjs, _ := ParseReadTags(tagString)
return GetTagFilter(tagObjs)
}
func GetTagFilter(tagObjs []database.Tag) []models.TagListItem {
var tagIds []string
for _, val := range tagObjs {
tagIds = append(tagIds, val.ID)
@ -38,7 +42,6 @@ func GetTagFilter(tagString []string) []models.TagListItem {
Find(&tags, tagIds)
return tags
}
func GetTagAutocomplete() []string {
var tags []string
result := database.DB.Model(&database.Tag{}).

View File

@ -1,11 +0,0 @@
<script>
import { Link } from "svelte-routing";
export let tag;
let tagType = tag.split(":")[0] ?? "";
let tagName = tag.split(":")[1] ?? "";
let tagDisplay = tagName.split("_").join(" ");
</script>
<Link to="/posts?tags={tagName}">{tagDisplay}</Link>

View File

@ -1,6 +1,6 @@
<script>
import AuthCheck from "./AuthCheck.svelte";
import TagLink from "./TagLink.svelte";
import TagLinkNumbered from "./TagLinkNumbered.svelte";
export let post;
export let toggleEditMenu;
export let toggleDeleteMenu;
@ -55,7 +55,11 @@
{#if post.tags}
{#each post.tags as tag (tag)}
<li>
<TagLink class="" {tag} />
<TagLinkNumbered
class=""
tag={tag.tagType + ":" + tag.tagName}
num={tag.postCount}
/>
</li>
{/each}
{/if}

View File

@ -1,8 +1,7 @@
<script>
import { onMount } from "svelte";
import TagLink from "../TagLink.svelte";
import { getPost, postCreate, postDelete } from "../api.js";
import { Link, navigate } from "svelte-routing";
import { getPost, postDelete } from "../api.js";
import { navigate } from "svelte-routing";
import EditPostPanel from "../EditPostPanel.svelte";
import ViewPostPanel from "../ViewPostPanel.svelte";
export let id;