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.

64 lines
1.5 KiB

  1. package mutations
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  5. "git.aiterp.net/rpdata/api/internal/session"
  6. "git.aiterp.net/rpdata/api/model/change"
  7. "git.aiterp.net/rpdata/api/model/character"
  8. )
  9. // CharacterEditInput is args for mutation addCharacterNick/removeCharacterNick
  10. type CharacterEditInput struct {
  11. ID string
  12. Name *string
  13. ShortName *string
  14. Description *string
  15. }
  16. // EditCharacter resolves the editCharacter mutation
  17. func (r *MutationResolver) EditCharacter(ctx context.Context, args struct{ Input *CharacterEditInput }) (*types.CharacterResolver, error) {
  18. input := args.Input
  19. user := session.FromContext(ctx).User()
  20. if user == nil || !user.Permitted("member") {
  21. return nil, ErrUnauthorized
  22. }
  23. character, err := character.FindID(input.ID)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if character.Author != user.ID && !user.Permitted("character.edit") {
  28. return nil, ErrPermissionDenied
  29. }
  30. name := ""
  31. if input.Name != nil {
  32. name = *input.Name
  33. }
  34. shortName := ""
  35. if input.ShortName != nil {
  36. shortName = *input.ShortName
  37. }
  38. description := ""
  39. if input.Description != nil {
  40. description = *input.Description
  41. }
  42. err = character.Edit(name, shortName, description)
  43. if err != nil {
  44. return nil, err
  45. }
  46. go change.Submit("Character", "edit", user.ID, character.ID, map[string]interface{}{
  47. "name": character.Name,
  48. "shortName": character.ShortName,
  49. "description": character.Description,
  50. })
  51. return &types.CharacterResolver{C: character}, nil
  52. }