A web blog and personal homepage engine written in Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.2 KiB

package models
import (
// TaggedPost associates tags to their posts.
type TaggedPost struct {
ID int `gorm:"primary_key"`
Tag string
PostID int // foreign key to Post
// SummarizeTags returns the list of all tags ordered by frequency used.
// public: true for public view, don't count tags on private posts.
func SummarizeTags(public bool) []TagSummary {
var result []TagSummary
query := DB.Table("tagged_posts").
Select("tagged_posts.tag, count(tagged_posts.post_id) AS count").
Joins("JOIN posts ON posts.id = tagged_posts.post_id").
Order("count desc")
if public {
query = query.Where("posts.privacy = ?", Public)
rows, err := query.Rows()
if err != nil {
console.Error("SummarizeTags: query.Rows: %s", err)
for rows.Next() {
var (
tagName string
count int
if err := rows.Scan(&tagName, &count); err != nil {
console.Error("SummarizeTags: rows.Scan: %s", err)
result = append(result, TagSummary{
Tag: tagName,
Count: count,
return result
// TagSummary holds the result for SummarizeTags
type TagSummary struct {
Tag string
Count int