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.

50 lines
1010 B

  1. package types
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "git.aiterp.net/rpdata/api/models"
  7. "git.aiterp.net/rpdata/api/models/comments"
  8. )
  9. type chapterResolver struct{}
  10. func (r *chapterResolver) FictionalDate(ctx context.Context, chapter *models.Chapter) (*time.Time, error) {
  11. if chapter.FictionalDate.IsZero() {
  12. return nil, nil
  13. }
  14. return &chapter.FictionalDate, nil
  15. }
  16. func (r *chapterResolver) Comments(ctx context.Context, chapter *models.Chapter, limit *int) ([]*models.Comment, error) {
  17. limitValue := 0
  18. if limit != nil {
  19. if *limit < 0 {
  20. return nil, errors.New("Limit cannot be negative")
  21. }
  22. limitValue = *limit
  23. }
  24. if !chapter.CommentMode.IsEnabled() {
  25. return nil, nil
  26. }
  27. comments, err := comments.ListChapterID(chapter.ID, limitValue)
  28. if err != nil {
  29. return nil, err
  30. }
  31. comments2 := make([]*models.Comment, len(comments))
  32. for i := range comments {
  33. comments2[i] = &comments[i]
  34. }
  35. return comments2, nil
  36. }
  37. // ChapterResolver is a resolver
  38. var ChapterResolver chapterResolver