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.

81 lines
2.2 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. return r.s.Stories.CreateStory(ctx, input.Name, input.Author, input.Category, listed, open, tags, time.Now(), fictionalDate)
  30. }
  31. func (r *mutationResolver) AddStoryTag(ctx context.Context, input graphcore.StoryTagAddInput) (*models.Story, error) {
  32. story, err := r.s.Stories.FindStory(ctx, input.ID)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return r.s.Stories.AddStoryTag(ctx, *story, *input.Tag)
  37. }
  38. func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input graphcore.StoryTagRemoveInput) (*models.Story, error) {
  39. story, err := r.s.Stories.FindStory(ctx, input.ID)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return r.s.Stories.RemoveStoryTag(ctx, *story, *input.Tag)
  44. }
  45. func (r *mutationResolver) EditStory(ctx context.Context, input graphcore.StoryEditInput) (*models.Story, error) {
  46. story, err := r.s.Stories.FindStory(ctx, input.ID)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return r.s.Stories.EditStory(ctx, story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate)
  51. }
  52. func (r *mutationResolver) RemoveStory(ctx context.Context, input graphcore.StoryRemoveInput) (*models.Story, error) {
  53. story, err := r.s.Stories.FindStory(ctx, input.ID)
  54. if err != nil {
  55. return nil, err
  56. }
  57. err = r.s.Stories.RemoveStory(ctx, story)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return story, err
  62. }