55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package util
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
type PaginationInfo struct {
|
|
NextCursor string `json:"next_cursor"`
|
|
PrevCursor string `json:"prev_cursor"`
|
|
}
|
|
|
|
type Cursor map[string]interface{}
|
|
|
|
func CreateCursor(id string, createdAt time.Time, pointsNext bool) Cursor {
|
|
return Cursor{
|
|
"id": id,
|
|
"created_at": createdAt,
|
|
"points_next": pointsNext,
|
|
}
|
|
}
|
|
|
|
func GeneratePager(next Cursor, prev Cursor) PaginationInfo {
|
|
return PaginationInfo{
|
|
NextCursor: encodeCursor(next),
|
|
PrevCursor: encodeCursor(prev),
|
|
}
|
|
}
|
|
|
|
func encodeCursor(cursor Cursor) string {
|
|
if len(cursor) == 0 {
|
|
return ""
|
|
}
|
|
serializedCursor, err := json.Marshal(cursor)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
encodedCursor := base64.StdEncoding.EncodeToString(serializedCursor)
|
|
return encodedCursor
|
|
}
|
|
|
|
func DecodeCursor(cursor string) (Cursor, error) {
|
|
decodedCursor, err := base64.StdEncoding.DecodeString(cursor)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var cur Cursor
|
|
if err := json.Unmarshal(decodedCursor, &cur); err != nil {
|
|
return nil, err
|
|
}
|
|
return cur, nil
|
|
}
|