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.

65 lines
1.9 KiB

  1. package resolvers
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/graph2/graphcore"
  6. "git.aiterp.net/rpdata/api/models"
  7. )
  8. // Queries
  9. func (r *queryResolver) Character(ctx context.Context, id *string, nick *string) (*models.Character, error) {
  10. if id != nil {
  11. return r.s.Characters.Find(ctx, *id)
  12. } else if nick != nil {
  13. return r.s.Characters.FindNick(ctx, *nick)
  14. } else {
  15. return nil, errors.New("You must specify either an ID or a nick")
  16. }
  17. }
  18. func (r *queryResolver) Characters(ctx context.Context, filter *models.CharacterFilter) ([]*models.Character, error) {
  19. if filter == nil {
  20. filter = &models.CharacterFilter{}
  21. }
  22. return r.s.Characters.List(ctx, *filter)
  23. }
  24. // Mutations
  25. func (r *mutationResolver) AddCharacter(ctx context.Context, input graphcore.CharacterAddInput) (*models.Character, error) {
  26. author := ""
  27. if input.Author != nil {
  28. author = *input.Author
  29. }
  30. shortName := ""
  31. if input.ShortName != nil {
  32. shortName = *input.ShortName
  33. }
  34. description := ""
  35. if input.Description != nil {
  36. description = *input.Description
  37. }
  38. return r.s.Characters.Create(ctx, input.Nick, input.Name, shortName, author, description)
  39. }
  40. func (r *mutationResolver) AddCharacterNick(ctx context.Context, input graphcore.CharacterNickInput) (*models.Character, error) {
  41. return r.s.Characters.AddNick(ctx, input.ID, input.Nick)
  42. }
  43. func (r *mutationResolver) RemoveCharacterNick(ctx context.Context, input graphcore.CharacterNickInput) (*models.Character, error) {
  44. return r.s.Characters.RemoveNick(ctx, input.ID, input.Nick)
  45. }
  46. func (r *mutationResolver) EditCharacter(ctx context.Context, input graphcore.CharacterEditInput) (*models.Character, error) {
  47. return r.s.Characters.Update(ctx, input.ID, input.Name, input.ShortName, input.Description)
  48. }
  49. func (r *mutationResolver) RemoveCharacter(ctx context.Context, input graphcore.CharacterRemoveInput) (*models.Character, error) {
  50. return r.s.Characters.Delete(ctx, input.ID)
  51. }