page_info.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package model
  2. import (
  3. "git.beejay.kim/Craft/Api/connection"
  4. "github.com/samber/lo"
  5. )
  6. //easyjson:json
  7. type PageInfo struct {
  8. Total int `json:"total"`
  9. StartCursor *string `json:"start_cursor,omitempty"`
  10. EndCursor *string `json:"end_cursor,omitempty"`
  11. HasNextPage bool `json:"has_next_page"`
  12. HasPreviousPage bool `json:"has_previous_page"`
  13. }
  14. func (pi *PageInfo) SetTotal(nodes []connection.Cursor) {
  15. pi.Total = len(nodes)
  16. pi.Invalidate(nodes)
  17. }
  18. func (pi *PageInfo) Invalidate(nodes []connection.Cursor) {
  19. l := len(nodes)
  20. if l < 1 {
  21. pi.HasPreviousPage = pi.StartCursor != nil
  22. pi.HasNextPage = pi.EndCursor != nil
  23. pi.StartCursor = nil
  24. pi.EndCursor = nil
  25. } else {
  26. if pi.StartCursor != nil {
  27. pi.HasPreviousPage = !(*pi.StartCursor == nodes[0].Cursor())
  28. } else {
  29. pi.HasPreviousPage = false
  30. }
  31. if pi.EndCursor != nil {
  32. pi.HasNextPage = !(*pi.EndCursor == nodes[l-1].Cursor())
  33. } else {
  34. pi.HasNextPage = false
  35. }
  36. pi.StartCursor = lo.ToPtr(nodes[0].Cursor())
  37. pi.EndCursor = lo.ToPtr(nodes[l-1].Cursor())
  38. }
  39. }