123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package model
- import (
- "git.beejay.kim/Craft/Api/connection"
- "github.com/samber/lo"
- )
- //easyjson:json
- type PageInfo struct {
- Total int `json:"total"`
- StartCursor *string `json:"start_cursor,omitempty"`
- EndCursor *string `json:"end_cursor,omitempty"`
- HasNextPage bool `json:"has_next_page"`
- HasPreviousPage bool `json:"has_previous_page"`
- }
- func (pi *PageInfo) SetTotal(nodes ...connection.Cursor) {
- pi.Total = len(nodes)
- pi.Invalidate(nodes...)
- }
- func (pi *PageInfo) Invalidate(nodes ...connection.Cursor) {
- l := len(nodes)
- if l < 1 {
- pi.HasPreviousPage = pi.StartCursor != nil
- pi.HasNextPage = pi.EndCursor != nil
- pi.StartCursor = nil
- pi.EndCursor = nil
- } else {
- if pi.StartCursor != nil {
- pi.HasPreviousPage = !(*pi.StartCursor == nodes[0].Cursor())
- } else {
- pi.HasPreviousPage = false
- }
- if pi.EndCursor != nil {
- pi.HasNextPage = !(*pi.EndCursor == nodes[l-1].Cursor())
- } else {
- pi.HasNextPage = false
- }
- pi.StartCursor = lo.ToPtr(nodes[0].Cursor())
- pi.EndCursor = lo.ToPtr(nodes[l-1].Cursor())
- }
- }
|