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.
36 lines
830 B
36 lines
830 B
package story
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/globalsign/mgo/bson"
|
|
)
|
|
|
|
// A Tag associates a story with other content, like other stories, logs and more.
|
|
type Tag struct {
|
|
Kind TagKind `bson:"kind"`
|
|
Name string `bson:"name"`
|
|
}
|
|
|
|
// Equal returns true if the tags match one another.
|
|
func (tag *Tag) Equal(other Tag) bool {
|
|
return tag.Kind == other.Kind && tag.Name == other.Name
|
|
}
|
|
|
|
// ListTags lists all tags
|
|
func ListTags() ([]Tag, error) {
|
|
tags := make([]Tag, 0, 64)
|
|
err := storyCollection.Find(bson.M{"listed": true, "tags": bson.M{"$ne": nil}}).Distinct("tags", &tags)
|
|
|
|
sort.Slice(tags, func(i, j int) bool {
|
|
kindCmp := strings.Compare(string(tags[i].Kind), string(tags[j].Kind))
|
|
if kindCmp != 0 {
|
|
return kindCmp < 0
|
|
}
|
|
|
|
return strings.Compare(tags[i].Name, tags[j].Name) < 0
|
|
})
|
|
|
|
return tags, err
|
|
}
|