Files
mtusi2026lab3/backend/internal/storage/postgres/cursor.go
T

42 lines
1023 B
Go

package postgres
import (
"encoding/base64"
"strings"
"time"
"high-loud-apps-labs/internal/domain"
)
// Взял за основу что в inmemery делал
func encodeCursor(scope string, createdAt time.Time, id string) string {
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
return base64.RawURLEncoding.EncodeToString([]byte(raw))
}
func decodeCursor(cursor, expectedScope string) (time.Time, string, error) {
data, err := base64.RawURLEncoding.DecodeString(cursor)
if err != nil {
return time.Time{}, "", badCursor()
}
parts := strings.SplitN(string(data), "|", 3)
if len(parts) != 3 {
return time.Time{}, "", badCursor()
}
if parts[0] != expectedScope {
return time.Time{}, "", badCursor()
}
t, err := time.Parse(time.RFC3339Nano, parts[1])
if err != nil {
return time.Time{}, "", badCursor()
}
return t, parts[2], nil
}
func badCursor() error {
return &domain.ValidationError{
Field: "cursor",
Message: "некорректный формат",
}
}