This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package story
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// TagKind represents the kind of tags.
|
||||
type TagKind string
|
||||
|
||||
const (
|
||||
// TagKindOrganization is a tag kind, see GraphQL documentation.
|
||||
TagKindOrganization TagKind = "Organization"
|
||||
|
||||
// TagKindCharacter is a tag kind, see GraphQL documentation.
|
||||
TagKindCharacter TagKind = "Character"
|
||||
|
||||
// TagKindLocation is a tag kind, see GraphQL documentation.
|
||||
TagKindLocation TagKind = "Location"
|
||||
|
||||
// TagKindEvent is a tag kind, see GraphQL documentation.
|
||||
TagKindEvent TagKind = "Event"
|
||||
|
||||
// TagKindSeries is a tag kind, see GraphQL documentation.
|
||||
TagKindSeries TagKind = "Series"
|
||||
)
|
||||
|
||||
// IsValid returns true if the TagKind is one of the constants
|
||||
func (e TagKind) IsValid() bool {
|
||||
switch e {
|
||||
case TagKindOrganization, TagKindCharacter, TagKindLocation, TagKindEvent, TagKindSeries:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e TagKind) String() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
// UnmarshalGQL unmarshals
|
||||
func (e *TagKind) UnmarshalGQL(v interface{}) error {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("enums must be strings")
|
||||
}
|
||||
|
||||
*e = TagKind(str)
|
||||
if !e.IsValid() {
|
||||
return fmt.Errorf("%s is not a valid TagKind", str)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalGQL turns it into a JSON string
|
||||
func (e TagKind) MarshalGQL(w io.Writer) {
|
||||
fmt.Fprint(w, "\""+e.String(), "\"")
|
||||
}
|
||||
+3
-3
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
// A Tag associates a story with other content, like other stories, logs and more.
|
||||
type Tag struct {
|
||||
Kind string `bson:"kind"`
|
||||
Name string `bson:"name"`
|
||||
Kind TagKind `bson:"kind"`
|
||||
Name string `bson:"name"`
|
||||
}
|
||||
|
||||
// Equal returns true if the tags match one another.
|
||||
@@ -24,7 +24,7 @@ func ListTags() ([]Tag, error) {
|
||||
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(tags[i].Kind, tags[j].Kind)
|
||||
kindCmp := strings.Compare(string(tags[i].Kind), string(tags[j].Kind))
|
||||
if kindCmp != 0 {
|
||||
return kindCmp < 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user