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
2.3 KiB

  1. package resolvers
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/graph2/graphcore"
  6. "git.aiterp.net/rpdata/api/models"
  7. )
  8. func (r *queryResolver) Story(ctx context.Context, id string) (*models.Story, error) {
  9. return r.s.Stories.FindStory(ctx, id)
  10. }
  11. func (r *queryResolver) Stories(ctx context.Context, filter *models.StoryFilter) ([]*models.Story, error) {
  12. if filter == nil {
  13. filter = &models.StoryFilter{}
  14. }
  15. return r.s.Stories.ListStories(ctx, *filter)
  16. }
  17. // Mutations
  18. func (r *mutationResolver) AddStory(ctx context.Context, input graphcore.StoryAddInput) (*models.Story, error) {
  19. listed := input.Listed != nil && *input.Listed
  20. open := input.Open != nil && *input.Open
  21. tags := make([]models.Tag, len(input.Tags))
  22. for i, tag := range input.Tags {
  23. tags[i] = *tag
  24. }
  25. fictionalDate := time.Time{}
  26. if input.FictionalDate != nil {
  27. fictionalDate = *input.FictionalDate
  28. }
  29. sortByFictionalDate := input.SortByFictionalDate != nil && *input.SortByFictionalDate
  30. return r.s.Stories.CreateStory(ctx, input.Name, input.Author, input.Category, listed, open, tags, time.Now(), fictionalDate, sortByFictionalDate)
  31. }
  32. func (r *mutationResolver) AddStoryTag(ctx context.Context, input graphcore.StoryTagAddInput) (*models.Story, error) {
  33. story, err := r.s.Stories.FindStory(ctx, input.ID)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return r.s.Stories.AddStoryTag(ctx, *story, *input.Tag)
  38. }
  39. func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input graphcore.StoryTagRemoveInput) (*models.Story, error) {
  40. story, err := r.s.Stories.FindStory(ctx, input.ID)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return r.s.Stories.RemoveStoryTag(ctx, *story, *input.Tag)
  45. }
  46. func (r *mutationResolver) EditStory(ctx context.Context, input graphcore.StoryEditInput) (*models.Story, error) {
  47. story, err := r.s.Stories.FindStory(ctx, input.ID)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return r.s.Stories.EditStory(ctx, story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate, input.SortByFictionalDate)
  52. }
  53. func (r *mutationResolver) RemoveStory(ctx context.Context, input graphcore.StoryRemoveInput) (*models.Story, error) {
  54. story, err := r.s.Stories.FindStory(ctx, input.ID)
  55. if err != nil {
  56. return nil, err
  57. }
  58. err = r.s.Stories.RemoveStory(ctx, story)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return story, err
  63. }