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.

71 lines
1.9 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. // Queries
  9. func (r *queryResolver) Chapter(ctx context.Context, id string) (*models.Chapter, error) {
  10. return r.s.Stories.FindChapter(ctx, id)
  11. }
  12. // Mutations
  13. func (r *mutationResolver) AddChapter(ctx context.Context, input graphcore.ChapterAddInput) (*models.Chapter, error) {
  14. story, err := r.s.Stories.FindStory(ctx, input.StoryID)
  15. if err != nil {
  16. return nil, err
  17. }
  18. commentMode := models.ChapterCommentModeDisabled
  19. if input.CommentMode != nil {
  20. commentMode = *input.CommentMode
  21. }
  22. return r.s.Stories.CreateChapter(ctx, *story, input.Title, input.Source, input.Author, time.Now(), input.FictionalDate, commentMode)
  23. }
  24. func (r *mutationResolver) MoveChapter(ctx context.Context, input graphcore.ChapterMoveInput) (*models.Chapter, error) {
  25. chapter, err := r.s.Stories.FindChapter(ctx, input.ID)
  26. if err != nil {
  27. return nil, err
  28. }
  29. from, err := r.s.Stories.FindStory(ctx, chapter.StoryID)
  30. if err != nil {
  31. return nil, err
  32. }
  33. to, err := r.s.Stories.FindStory(ctx, input.StoryID)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return r.s.Stories.MoveChapter(ctx, chapter, *from, *to)
  38. }
  39. func (r *mutationResolver) EditChapter(ctx context.Context, input graphcore.ChapterEditInput) (*models.Chapter, error) {
  40. chapter, err := r.s.Stories.FindChapter(ctx, input.ID)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return r.s.Stories.EditChapter(ctx, chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked)
  45. }
  46. func (r *mutationResolver) RemoveChapter(ctx context.Context, input graphcore.ChapterRemoveInput) (*models.Chapter, error) {
  47. chapter, err := r.s.Stories.FindChapter(ctx, input.ID)
  48. if err != nil {
  49. return nil, err
  50. }
  51. err = r.s.Stories.RemoveChapter(ctx, chapter)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return chapter, nil
  56. }