This commit is contained in:
@@ -26,6 +26,25 @@ type Character struct {
|
||||
Description string `json:"description" bson:"description"`
|
||||
}
|
||||
|
||||
// Filter is used to filter the list of characters
|
||||
type Filter struct {
|
||||
IDs []string `json:"ids"`
|
||||
Nicks []string `json:"nicks"`
|
||||
Names []string `json:"names"`
|
||||
Author *string `json:"author"`
|
||||
Search *string `json:"search"`
|
||||
Logged *bool `json:"logged"`
|
||||
}
|
||||
|
||||
// Nick gets the character's nick.
|
||||
func (character *Character) Nick() *string {
|
||||
if len(character.Nicks[0]) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &character.Nicks[0]
|
||||
}
|
||||
|
||||
// HasNick returns true if the character has that nick
|
||||
func (character *Character) HasNick(nick string) bool {
|
||||
for i := range character.Nicks {
|
||||
@@ -136,8 +155,48 @@ func FindName(name string) (Character, error) {
|
||||
}
|
||||
|
||||
// List lists all characters
|
||||
func List() ([]Character, error) {
|
||||
return list(bson.M{})
|
||||
func List(filter *Filter) ([]Character, error) {
|
||||
query := bson.M{}
|
||||
|
||||
if filter != nil {
|
||||
if len(filter.IDs) > 1 {
|
||||
query["id"] = bson.M{"$in": filter.IDs}
|
||||
} else if len(filter.IDs) == 1 {
|
||||
query["id"] = filter.IDs[0]
|
||||
}
|
||||
|
||||
if len(filter.Nicks) > 1 {
|
||||
query["nicks"] = bson.M{"$in": filter.Nicks}
|
||||
} else if len(filter.Nicks) == 1 {
|
||||
query["nicks"] = filter.Nicks[0]
|
||||
}
|
||||
|
||||
if len(filter.Names) > 1 {
|
||||
query["$or"] = bson.M{
|
||||
"name": bson.M{"$in": filter.Names},
|
||||
"shortName": bson.M{"$in": filter.Names},
|
||||
}
|
||||
} else if len(filter.Names) == 1 {
|
||||
query["$or"] = bson.M{
|
||||
"name": filter.Names[0],
|
||||
"shortName": filter.Names[0],
|
||||
}
|
||||
}
|
||||
|
||||
if filter.Logged != nil {
|
||||
query["logged"] = *filter.Logged
|
||||
}
|
||||
|
||||
if filter.Author != nil {
|
||||
query["author"] = *filter.Author
|
||||
}
|
||||
|
||||
if filter.Search != nil {
|
||||
query["$text"] = bson.M{"$search": *filter.Search}
|
||||
}
|
||||
}
|
||||
|
||||
return list(query)
|
||||
}
|
||||
|
||||
// ListAuthor lists all characters by author
|
||||
|
||||
@@ -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