5 changed files with 90 additions and 7 deletions
@ -0,0 +1,57 @@ |
|||
package models |
|||
|
|||
import ( |
|||
"git.kirsle.net/apps/gophertype/pkg/console" |
|||
) |
|||
|
|||
// TaggedPost associates tags to their posts.
|
|||
type TaggedPost struct { |
|||
ID uint `gorm:"primary_key"` |
|||
Tag string |
|||
PostID uint // 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"). |
|||
Group("tagged_posts.tag"). |
|||
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 |
|||
} |
@ -0,0 +1,13 @@ |
|||
{{ define "title" }}Tags{{ end }} |
|||
{{ define "content" }} |
|||
<h1>Tags</h1> |
|||
|
|||
<p>Sorted by most frequently used:</p> |
|||
|
|||
<ul> |
|||
{{ range $tag := .V.tags }} |
|||
<li><a href="/tagged/{{ $tag.Tag }}">{{ $tag.Tag }}</a> ({{ $tag.Count }})</li> |
|||
{{ end }} |
|||
</ul> |
|||
|
|||
{{ end }} |
Loading…
Reference in new issue