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 ( import (
"net/http" "net/http"
"github.com/Damillora/Shioriko/pkg/models"
"github.com/Damillora/Shioriko/pkg/services" "github.com/Damillora/Shioriko/pkg/services"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -17,13 +16,5 @@ func InitializeTagRoutes(g *gin.Engine) {
func tagGet(c *gin.Context) { func tagGet(c *gin.Context) {
tags := services.GetTagAll() tags := services.GetTagAll()
var tagResult []models.TagListItem c.JSON(http.StatusOK, tags)
for _, tag := range tags {
tagResult = append(tagResult, models.TagListItem{
ID: tag.ID,
Name: tag.Name,
TagType: tag.TagType.Name,
})
}
c.JSON(http.StatusOK, tagResult)
} }

View File

@ -6,9 +6,10 @@ type TagTypeListItem struct {
} }
type TagListItem struct { type TagListItem struct {
ID string `json:"id"` TagID string `json:"tagId"`
Name string `json:"name"` TagName string `json:"tagName"`
TagType string `json:"tagType"` TagType string `json:"tagType"`
PostCount int `json:"postCount"`
} }
type PostListItem struct { type PostListItem struct {

View File

@ -5,12 +5,19 @@ import (
"strings" "strings"
"github.com/Damillora/Shioriko/pkg/database" "github.com/Damillora/Shioriko/pkg/database"
"github.com/Damillora/Shioriko/pkg/models"
"github.com/google/uuid" "github.com/google/uuid"
) )
func GetTagAll() []database.Tag { func GetTagAll() []models.TagListItem {
var tags []database.Tag var tags []models.TagListItem
database.DB.Joins("TagType").Find(&tags) 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 return tags
} }

View File

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

View File

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