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.

67 lines
1.5 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. // AddChapterArgs is args for the addChapter mutation
  11. type AddChapterArgs struct {
  12. Input *struct {
  13. StoryID string
  14. Title string
  15. Author *string
  16. Source string
  17. FictionalDate *string
  18. }
  19. }
  20. // AddChapter resolves the addChapter mutation
  21. func (r *MutationResolver) AddChapter(ctx context.Context, args *AddChapterArgs) (*types.ChapterResolver, error) {
  22. input := args.Input
  23. token := auth.TokenFromContext(ctx)
  24. if !token.Permitted("member", "chapter.add") {
  25. return nil, ErrUnauthorized
  26. }
  27. story, err := story.FindID(input.StoryID)
  28. if err != nil {
  29. return nil, err
  30. }
  31. author := token.UserID
  32. if input.Author != nil {
  33. author = *input.Author
  34. if token.UserID != author && !token.Permitted("chapter.add") {
  35. return nil, ErrPermissionDenied
  36. }
  37. }
  38. fictionalDate := time.Time{}
  39. if input.FictionalDate != nil {
  40. fictionalDate, err = time.Parse(time.RFC3339Nano, *input.FictionalDate)
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. chapter, err := story.AddChapter(input.Title, author, input.Source, time.Now(), fictionalDate)
  46. if err != nil {
  47. return nil, err
  48. }
  49. go change.Submit("Chapter", "add", token.UserID, chapter.ID, map[string]interface{}{
  50. "title": input.Title,
  51. "author": author,
  52. "fictionalDate": fictionalDate,
  53. })
  54. return &types.ChapterResolver{C: chapter}, nil
  55. }