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.

161 lines
3.8 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/internal/session"
  6. "git.aiterp.net/rpdata/api/model/change"
  7. "git.aiterp.net/rpdata/api/model/story"
  8. "git.aiterp.net/rpdata/api/resolver/types"
  9. )
  10. // ChapterArgs is args for chapter query
  11. type ChapterArgs struct {
  12. ID string
  13. }
  14. // Chapter implements the chapter query
  15. func (r *QueryResolver) Chapter(ctx context.Context, args *ChapterArgs) (*types.ChapterResolver, error) {
  16. chapter, err := story.FindChapterID(args.ID)
  17. if err != nil {
  18. return nil, err
  19. }
  20. return &types.ChapterResolver{C: chapter}, nil
  21. }
  22. // AddChapterArgs is args for the addChapter mutation
  23. type AddChapterArgs struct {
  24. Input *struct {
  25. StoryID string
  26. Title string
  27. Author *string
  28. Source string
  29. FictionalDate *string
  30. }
  31. }
  32. // AddChapter implements the addChapter mutation
  33. func (r *MutationResolver) AddChapter(ctx context.Context, args *AddChapterArgs) (*types.ChapterResolver, error) {
  34. input := args.Input
  35. user := session.FromContext(ctx).User()
  36. if user == nil || !user.Permitted("member", "chapter.add") {
  37. return nil, ErrUnauthorized
  38. }
  39. story, err := story.FindID(input.StoryID)
  40. if err != nil {
  41. return nil, err
  42. }
  43. author := user.ID
  44. if input.Author != nil {
  45. author = *input.Author
  46. if user.ID != author && !user.Permitted("chapter.add") {
  47. return nil, ErrPermissionDenied
  48. }
  49. }
  50. fictionalDate := time.Time{}
  51. if input.FictionalDate != nil {
  52. fictionalDate, err = time.Parse(time.RFC3339Nano, *input.FictionalDate)
  53. if err != nil {
  54. return nil, err
  55. }
  56. }
  57. chapter, err := story.AddChapter(input.Title, author, input.Source, time.Now(), fictionalDate)
  58. if err != nil {
  59. return nil, err
  60. }
  61. go change.Submit("Chapter", "add", user.ID, chapter.ID, map[string]interface{}{
  62. "title": input.Title,
  63. "author": author,
  64. "fictionalDate": fictionalDate,
  65. })
  66. return &types.ChapterResolver{C: chapter}, nil
  67. }
  68. // EditChapterArgs is args for the editChapter mutation
  69. type EditChapterArgs struct {
  70. Input *struct {
  71. ID string
  72. Title *string
  73. Source *string
  74. FictionalDate *string
  75. }
  76. }
  77. // EditChapter implements the editChapter mutation
  78. func (r *MutationResolver) EditChapter(ctx context.Context, args *EditChapterArgs) (*types.ChapterResolver, error) {
  79. input := args.Input
  80. user := session.FromContext(ctx).User()
  81. if user == nil || !user.Permitted("member", "chapter.edit") {
  82. return nil, ErrUnauthorized
  83. }
  84. chapter, err := story.FindChapterID(input.ID)
  85. if err != nil {
  86. return nil, err
  87. }
  88. if chapter.Author != user.ID && !user.Permitted("chapter.edit") {
  89. return nil, ErrPermissionDenied
  90. }
  91. var fictionalDate *time.Time
  92. if input.FictionalDate != nil {
  93. date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate)
  94. if err != nil {
  95. return nil, err
  96. }
  97. fictionalDate = &date
  98. }
  99. err = chapter.Edit(input.Title, input.Source, fictionalDate)
  100. if err != nil {
  101. return nil, err
  102. }
  103. go change.Submit("Chapter", "edit", user.ID, chapter.ID, map[string]interface{}{
  104. "title": input.Title,
  105. "source": input.Source,
  106. "fictionalDate": fictionalDate,
  107. })
  108. return &types.ChapterResolver{C: chapter}, nil
  109. }
  110. // DeleteChapterArgs is args for the addChapter mutation
  111. type DeleteChapterArgs struct {
  112. ID string
  113. }
  114. // RemoveChapter implements the removeChapter mutation
  115. func (r *MutationResolver) RemoveChapter(ctx context.Context, args *DeleteChapterArgs) (*types.ChapterResolver, error) {
  116. user := session.FromContext(ctx).User()
  117. if user == nil || !user.Permitted("member", "chapter.edit") {
  118. return nil, ErrUnauthorized
  119. }
  120. chapter, err := story.FindChapterID(args.ID)
  121. if err != nil {
  122. return nil, err
  123. }
  124. err = chapter.Remove()
  125. if err != nil {
  126. return nil, err
  127. }
  128. go change.Submit("Chapter", "remove", user.ID, chapter.ID, nil)
  129. return &types.ChapterResolver{C: chapter}, nil
  130. }