GraphQL API and utilities for the rpdata project
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
812 B

  1. package story
  2. import (
  3. "sort"
  4. "strings"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // A Tag associates a story with other content, like other stories, logs and more.
  8. type Tag struct {
  9. Kind string `bson:"kind"`
  10. Name string `bson:"name"`
  11. }
  12. // Equal returns true if the tags match one another.
  13. func (tag *Tag) Equal(other Tag) bool {
  14. return tag.Kind == other.Kind && tag.Name == other.Name
  15. }
  16. // ListTags lists all tags
  17. func ListTags() ([]Tag, error) {
  18. tags := make([]Tag, 0, 64)
  19. err := storyCollection.Find(bson.M{"listed": true, "tags": bson.M{"$ne": nil}}).Distinct("tags", &tags)
  20. sort.Slice(tags, func(i, j int) bool {
  21. kindCmp := strings.Compare(tags[i].Kind, tags[j].Kind)
  22. if kindCmp != 0 {
  23. return kindCmp < 0
  24. }
  25. return strings.Compare(tags[i].Name, tags[j].Name) < 0
  26. })
  27. return tags, err
  28. }