Browse Source

Added logged filter to characters query: character appears in logs.

1.0
Gisle Aune 6 years ago
parent
commit
0ec923d686
  1. 3
      graphql/resolver/queries/characters.go
  2. 3
      graphql/schema/types/character.graphql
  3. 28
      model/character/character.go

3
graphql/resolver/queries/characters.go

@ -15,6 +15,7 @@ type CharactersArgs struct {
Names *[]string Names *[]string
Author *string Author *string
Search *string Search *string
Logged *bool
} }
} }
@ -39,7 +40,7 @@ func (r *QueryResolver) Characters(ctx context.Context, args *CharactersArgs) ([
names = *filter.Names names = *filter.Names
} }
characters, err = character.ListFilter(ids, nicks, names, filter.Author, filter.Search)
characters, err = character.ListFilter(ids, nicks, names, filter.Author, filter.Search, filter.Logged)
if err != nil { if err != nil {
return nil, err return nil, err
} }

3
graphql/schema/types/character.graphql

@ -38,6 +38,9 @@ input CharactersFilter {
# Filter by text search matching against the character's description # Filter by text search matching against the character's description
search: String search: String
# Filter by whether they've been part of a log.
logged: Boolean
} }
# Input for adding characters # Input for adding characters

28
model/character/character.go

@ -14,6 +14,8 @@ import (
var collection *mgo.Collection var collection *mgo.Collection
var logsCollection *mgo.Collection
// Character is a common data model representing an RP character or NPC. // Character is a common data model representing an RP character or NPC.
type Character struct { type Character struct {
ID string `json:"id" bson:"_id"` ID string `json:"id" bson:"_id"`
@ -155,9 +157,31 @@ func ListIDs(ids ...string) ([]Character, error) {
} }
// ListFilter lists all logs matching the filters. // ListFilter lists all logs matching the filters.
func ListFilter(ids []string, nicks []string, names []string, author *string, search *string) ([]Character, error) {
func ListFilter(ids []string, nicks []string, names []string, author *string, search *string, logged *bool) ([]Character, error) {
query := bson.M{} query := bson.M{}
if logged != nil {
loggedIDs := make([]string, 0, 64)
err := logsCollection.Find(bson.M{"characterIds": bson.M{"$ne": nil}}).Distinct("characterIds", &loggedIDs)
if err != nil {
return nil, err
}
if len(ids) > 0 {
newIds := make([]string, 0, len(ids))
for _, id := range ids {
for _, loggedID := range loggedIDs {
if id == loggedID {
newIds = append(newIds, id)
break
}
}
}
ids = newIds
} else {
ids = loggedIDs
}
}
if len(ids) > 0 { if len(ids) > 0 {
query["_id"] = bson.M{"$in": ids} query["_id"] = bson.M{"$in": ids}
} }
@ -256,5 +280,7 @@ func init() {
if err != nil { if err != nil {
log.Fatalln("init common.characters:", err) log.Fatalln("init common.characters:", err)
} }
logsCollection = db.C("logbot3.logs")
}) })
} }
Loading…
Cancel
Save