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.

69 lines
1.6 KiB

  1. package mutations
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  6. "git.aiterp.net/rpdata/api/internal/auth"
  7. "git.aiterp.net/rpdata/api/model/change"
  8. "git.aiterp.net/rpdata/api/model/story"
  9. )
  10. // StoryEditArgs is args for the addStory mutation
  11. type StoryEditArgs struct {
  12. Input *struct {
  13. ID string
  14. Name *string
  15. Category *string
  16. Author *string
  17. Open *bool
  18. Listed *bool
  19. FictionalDate *string
  20. }
  21. }
  22. // EditStory resolves the editStory mutation
  23. func (r *MutationResolver) EditStory(ctx context.Context, args *StoryEditArgs) (*types.StoryResolver, error) {
  24. input := args.Input
  25. token := auth.TokenFromContext(ctx)
  26. if !token.Permitted("member", "story.edit") {
  27. return nil, ErrUnauthorized
  28. }
  29. story, err := story.FindID(input.ID)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if story.Author != token.UserID && !token.Permitted("story.edit") {
  34. return nil, ErrPermissionDenied
  35. }
  36. var fictionalDate *time.Time
  37. if input.FictionalDate != nil {
  38. date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate)
  39. if err != nil {
  40. return nil, err
  41. }
  42. fictionalDate = &date
  43. }
  44. err = story.Edit(input.Name, input.Category, input.Listed, input.Open, fictionalDate)
  45. if err != nil {
  46. return nil, err
  47. }
  48. go change.Submit("Story", "edit", token.UserID, story.ID, map[string]interface{}{
  49. "name": input.Name,
  50. "category": input.Category,
  51. "author": input.Author,
  52. "open": input.Open,
  53. "listed": input.Listed,
  54. "fictionalDate": input.FictionalDate,
  55. })
  56. return &types.StoryResolver{S: story}, nil
  57. }