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.

162 lines
3.5 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/model/story"
  6. )
  7. // StoryResolver for the Story graphql type
  8. type StoryResolver struct{ S story.Story }
  9. // StoryArgs is args for story query
  10. type StoryArgs struct {
  11. ID string
  12. }
  13. // Story implements the story query
  14. func (r *QueryResolver) Story(ctx context.Context, args *StoryArgs) (*StoryResolver, error) {
  15. story, err := story.FindID(args.ID)
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &StoryResolver{S: story}, nil
  20. }
  21. // StoriesArg is args for stories query
  22. type StoriesArg struct {
  23. Input *StoriesInput
  24. }
  25. // StoriesInput resolves the TagInput input
  26. type StoriesInput struct {
  27. Author *string
  28. Tags *[]TagInput
  29. EarliestFictionalDate *string
  30. LatestFictionalDate *string
  31. Limit *int32
  32. }
  33. // Stories implements the stories query
  34. func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*StoryResolver, error) {
  35. author := ""
  36. if args.Input.Author != nil {
  37. author = *args.Input.Author
  38. }
  39. tags := make([]story.Tag, 0, 8)
  40. if args.Input.Tags != nil {
  41. for _, tagInput := range *args.Input.Tags {
  42. tags = append(tags, story.Tag{
  43. Kind: tagInput.Kind,
  44. Name: tagInput.Name,
  45. })
  46. }
  47. }
  48. earliest := time.Time{}
  49. err := error(nil)
  50. if args.Input.EarliestFictionalDate != nil {
  51. earliest, err = time.Parse(time.RFC3339Nano, *args.Input.EarliestFictionalDate)
  52. if err != nil {
  53. return nil, err
  54. }
  55. }
  56. latest := time.Time{}
  57. if args.Input.LatestFictionalDate != nil {
  58. latest, err = time.Parse(time.RFC3339Nano, *args.Input.LatestFictionalDate)
  59. if err != nil {
  60. return nil, err
  61. }
  62. }
  63. limit := 30
  64. if args.Input.Limit != nil {
  65. limit = int(*args.Input.Limit)
  66. }
  67. stories, err := story.List(author, tags, earliest, latest, limit)
  68. if err != nil {
  69. return nil, err
  70. }
  71. resolvers := make([]*StoryResolver, len(stories))
  72. for i, story := range stories {
  73. resolvers[i] = &StoryResolver{S: story}
  74. }
  75. return resolvers, nil
  76. }
  77. // ID resolves Story.id
  78. func (r *StoryResolver) ID() string {
  79. return r.S.ID
  80. }
  81. // Author resolves Story.author
  82. func (r *StoryResolver) Author() string {
  83. return r.S.Author
  84. }
  85. // Name resolves Story.name
  86. func (r *StoryResolver) Name() string {
  87. return r.S.Name
  88. }
  89. // Category resolves Story.category
  90. func (r *StoryResolver) Category() string {
  91. return r.S.Category
  92. }
  93. // Open resolves Story.open
  94. func (r *StoryResolver) Open() bool {
  95. return r.S.Open
  96. }
  97. // Listed resolves Story.listed
  98. func (r *StoryResolver) Listed() bool {
  99. return r.S.Listed
  100. }
  101. // Tags resolves Story.tags
  102. func (r *StoryResolver) Tags() []*TagResolver {
  103. resolvers := make([]*TagResolver, len(r.S.Tags))
  104. for i, tag := range r.S.Tags {
  105. resolvers[i] = &TagResolver{T: tag}
  106. }
  107. return resolvers
  108. }
  109. // Chapters resolves Story.chapters
  110. func (r *StoryResolver) Chapters() ([]*ChapterResolver, error) {
  111. chapters, err := r.S.Chapters()
  112. if err != nil {
  113. return nil, err
  114. }
  115. resolvers := make([]*ChapterResolver, len(chapters))
  116. for i, chapter := range chapters {
  117. resolvers[i] = &ChapterResolver{C: chapter}
  118. }
  119. return resolvers, nil
  120. }
  121. // CreatedDate resolves Story.createdDate
  122. func (r *StoryResolver) CreatedDate() string {
  123. return r.S.CreatedDate.Format(time.RFC3339Nano)
  124. }
  125. // FictionalDate resolves Story.fictionalDate
  126. func (r *StoryResolver) FictionalDate() string {
  127. return r.S.FictionalDate.Format(time.RFC3339Nano)
  128. }
  129. // UpdatedDate resolves Story.updatedDate
  130. func (r *StoryResolver) UpdatedDate() string {
  131. return r.S.UpdatedDate.Format(time.RFC3339Nano)
  132. }