|
|
@ -2,52 +2,57 @@ package queries |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"errors" |
|
|
|
|
|
|
|
"git.aiterp.net/rpdata/api/graphql/loader" |
|
|
|
"git.aiterp.net/rpdata/api/graphql/resolver/types" |
|
|
|
"git.aiterp.net/rpdata/api/model/character" |
|
|
|
) |
|
|
|
|
|
|
|
// CharactersArgs is args for characters query
|
|
|
|
type CharactersArgs struct { |
|
|
|
Filter *struct { |
|
|
|
IDs *[]string |
|
|
|
Nicks *[]string |
|
|
|
Names *[]string |
|
|
|
Author *string |
|
|
|
Search *string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Characters resolves the characters query
|
|
|
|
func (r *QueryResolver) Characters(ctx context.Context, args *CharactersArgs) ([]*types.CharacterResolver, error) { |
|
|
|
var chars []character.Character |
|
|
|
var characters []character.Character |
|
|
|
var err error |
|
|
|
|
|
|
|
loader := loader.FromContext(ctx) |
|
|
|
if loader == nil { |
|
|
|
return nil, errors.New("no loader") |
|
|
|
} |
|
|
|
filter := args.Filter |
|
|
|
if filter != nil { |
|
|
|
var ids []string |
|
|
|
var nicks []string |
|
|
|
var names []string |
|
|
|
|
|
|
|
switch { |
|
|
|
case args.IDs != nil: |
|
|
|
chars, err = character.ListIDs(*args.IDs...) |
|
|
|
case args.Nicks != nil: |
|
|
|
chars, err = character.ListNicks(*args.Nicks...) |
|
|
|
case args.Author != nil: |
|
|
|
chars, err = character.ListAuthor(*args.Author) |
|
|
|
default: |
|
|
|
chars, err = character.List() |
|
|
|
if filter.IDs != nil { |
|
|
|
ids = *filter.IDs |
|
|
|
} |
|
|
|
if filter.Nicks != nil { |
|
|
|
nicks = *filter.Nicks |
|
|
|
} |
|
|
|
if filter.Names != nil { |
|
|
|
names = *filter.Names |
|
|
|
} |
|
|
|
|
|
|
|
characters, err = character.ListFilter(ids, nicks, names, filter.Author, filter.Search) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} else { |
|
|
|
characters, err = character.List() |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
resolvers := make([]*types.CharacterResolver, 0, len(chars)) |
|
|
|
for i := range chars { |
|
|
|
if chars[i].ID == "" { |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
resolvers = append(resolvers, &types.CharacterResolver{C: chars[i]}) |
|
|
|
resolvers := make([]*types.CharacterResolver, 0, len(characters)) |
|
|
|
for _, character := range characters { |
|
|
|
resolvers = append(resolvers, &types.CharacterResolver{C: character}) |
|
|
|
} |
|
|
|
|
|
|
|
return resolvers, nil |
|
|
|