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.

83 lines
1.8 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/session"
  7. "git.aiterp.net/rpdata/api/model/change"
  8. "git.aiterp.net/rpdata/api/model/story"
  9. )
  10. // StoryAddArgs is args for the addStory mutation
  11. type StoryAddArgs struct {
  12. Input *struct {
  13. Name string
  14. Category string
  15. Author *string
  16. Open *bool
  17. Listed *bool
  18. FictionalDate *string
  19. Tags *[]struct {
  20. Kind string
  21. Name string
  22. }
  23. }
  24. }
  25. // AddStory resolves the addStory mutation
  26. func (r *MutationResolver) AddStory(ctx context.Context, args *StoryAddArgs) (*types.StoryResolver, error) {
  27. input := args.Input
  28. user := session.FromContext(ctx).User()
  29. if user == nil || !user.Permitted("member", "story.add") {
  30. return nil, ErrUnauthorized
  31. }
  32. author := user.ID
  33. if input.Author != nil {
  34. author = *input.Author
  35. if user.ID != author && !user.Permitted("story.add") {
  36. return nil, ErrPermissionDenied
  37. }
  38. }
  39. listed := (input.Listed != nil && *input.Listed == true)
  40. open := (input.Open != nil && *input.Open == true)
  41. tags := make([]story.Tag, 0, 8)
  42. if input.Tags != nil {
  43. for _, tagInput := range *input.Tags {
  44. tags = append(tags, story.Tag{
  45. Kind: tagInput.Kind,
  46. Name: tagInput.Name,
  47. })
  48. }
  49. }
  50. fictionalDate := time.Time{}
  51. if input.FictionalDate != nil {
  52. date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate)
  53. if err != nil {
  54. return nil, err
  55. }
  56. fictionalDate = date
  57. }
  58. story, err := story.New(input.Name, author, input.Category, listed, open, tags, time.Now(), fictionalDate)
  59. if err != nil {
  60. return nil, err
  61. }
  62. go change.Submit("Story", "add", user.ID, story.ID, map[string]interface{}{
  63. "name": input.Name,
  64. "category": input.Category,
  65. "author": input.Author,
  66. })
  67. return &types.StoryResolver{S: story}, nil
  68. }