feat: add tags post count

This commit is contained in:
Damillora 2021-09-11 01:26:54 +07:00
parent e79b033c59
commit a420b50877
5 changed files with 25 additions and 21 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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
}

View File

@ -35,6 +35,7 @@
<div class="navbar-menu" class:is-active={menu_shown}>
<div class="navbar-start">
<Link class="navbar-item" to="/posts">Posts</Link>
<Link class="navbar-item" to="/tags">Tags</Link>
{#if loggedIn}
<Link class="navbar-item" to="/upload">Upload</Link>
{/if}

View File

@ -1,6 +1,6 @@
<script>
import { getTags } from "../api";
import { getTags } from "../api";
import { Link } from "svelte-routing";
let tags = [];
@ -25,15 +25,19 @@ import { getTags } from "../api";
<table class="table is-fullwidth">
<thead>
<tr>
<th>Tag</th>
<th>Tag Type</th>
<th >Tag</th>
<th style="width: 30%;">Tag Type</th>
<th style="width: 10%;">Post Count</th>
</tr>
</thead>
<tbody>
{#each tags as tag}
<tr>
<td>{tag.name}</td>
<td>
<Link to="/posts?tags={tag.tagType}:{tag.tagName}">{tag.tagName}</Link>
</td>
<td>{tag.tagType}</td>
<td>{tag.postCount}</td>
</tr>
{/each}
</tbody>