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.

77 lines
1.6 KiB

  1. package queries
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  6. "git.aiterp.net/rpdata/api/model/story"
  7. )
  8. // StoriesArg is args for stories query
  9. type StoriesArg struct {
  10. Filter *struct {
  11. Author *string
  12. Tags *[]struct {
  13. Kind string
  14. Name string
  15. }
  16. EarliestFictionalDate *string
  17. LatestFictionalDate *string
  18. Limit *int32
  19. }
  20. }
  21. // Stories resolves the stories query
  22. func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types.StoryResolver, error) {
  23. filter := args.Filter
  24. author := ""
  25. if filter != nil && filter.Author != nil {
  26. author = *filter.Author
  27. }
  28. tags := make([]story.Tag, 0, 8)
  29. if filter != nil && filter.Tags != nil {
  30. for _, tagInput := range *filter.Tags {
  31. tags = append(tags, story.Tag{
  32. Kind: tagInput.Kind,
  33. Name: tagInput.Name,
  34. })
  35. }
  36. }
  37. earliest := time.Time{}
  38. err := error(nil)
  39. if filter != nil && filter.EarliestFictionalDate != nil {
  40. earliest, err = time.Parse(time.RFC3339Nano, *filter.EarliestFictionalDate)
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. latest := time.Time{}
  46. if filter != nil && filter.LatestFictionalDate != nil {
  47. latest, err = time.Parse(time.RFC3339Nano, *filter.LatestFictionalDate)
  48. if err != nil {
  49. return nil, err
  50. }
  51. }
  52. limit := 30
  53. if filter != nil && filter.Limit != nil {
  54. limit = int(*filter.Limit)
  55. }
  56. stories, err := story.List(author, tags, earliest, latest, limit)
  57. if err != nil {
  58. return nil, err
  59. }
  60. resolvers := make([]*types.StoryResolver, len(stories))
  61. for i, story := range stories {
  62. resolvers[i] = &types.StoryResolver{S: story}
  63. }
  64. return resolvers, nil
  65. }