feat: now back sendind real new cursor

This commit is contained in:
Iron_Felix 2025-11-22 02:04:40 +03:00
parent af0492cdf1
commit e16adcf6cd
2 changed files with 22 additions and 6 deletions

View file

@ -38,7 +38,7 @@ func ParseCursorInto(sortBy, cursorStr string, target any) error {
// 3. Get reflect value of target (must be ptr to struct)
v := reflect.ValueOf(target)
if v.Kind() != reflect.Ptr || v.IsNil() {
if v.Kind() != reflect.Pointer || v.IsNil() {
return fmt.Errorf("target must be non-nil pointer to struct")
}
v = v.Elem()
@ -56,7 +56,7 @@ func ParseCursorInto(sortBy, cursorStr string, target any) error {
vv := reflect.ValueOf(value)
// Case: field is *T, value is T → wrap in pointer
if ft.Kind() == reflect.Ptr {
if ft.Kind() == reflect.Pointer {
elemType := ft.Elem()
if vv.Type().AssignableTo(elemType) {
ptr := reflect.New(elemType)
@ -143,7 +143,7 @@ func extractInt64(m map[string]any, key string) (int64, error) {
return 0, fmt.Errorf("%q must be integer", key)
}
func extractString(m map[string]interface{}, key string) (string, error) {
func extractString(m map[string]any, key string) (string, error) {
v, ok := m[key]
if !ok {
return "", fmt.Errorf("missing %q", key)

View file

@ -6,6 +6,7 @@ import (
"fmt"
oapi "nyanimedb/api"
sqlc "nyanimedb/sql"
"strconv"
"github.com/jackc/pgx/v5"
log "github.com/sirupsen/logrus"
@ -249,11 +250,12 @@ func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObje
ReleaseYear: request.Params.ReleaseYear,
ReleaseSeason: season,
Forward: true,
// SortBy: "id",
SortBy: "id",
Limit: request.Params.Limit,
}
if request.Params.Sort != nil {
params.SortBy = string(*request.Params.Sort)
if request.Params.Cursor != nil {
err := ParseCursorInto(string(*request.Params.Sort), string(*request.Params.Cursor), &params)
if err != nil {
@ -272,6 +274,8 @@ func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObje
return oapi.GetTitles204Response{}, nil
}
var new_cursor oapi.CursorObj
for _, title := range titles {
t, err := s.mapTitle(ctx, title)
@ -280,7 +284,19 @@ func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObje
return oapi.GetTitles500Response{}, nil
}
opai_titles = append(opai_titles, t)
new_cursor.Id = t.Id
if request.Params.Sort != nil {
switch string(*request.Params.Sort) {
case "year":
tmp := string(*t.ReleaseYear)
new_cursor.Param = &tmp
case "rating":
tmp := strconv.FormatFloat(*t.Rating, 'f', -1, 64)
new_cursor.Param = &tmp
}
}
}
return oapi.GetTitles200JSONResponse{Cursor: oapi.CursorObj{}, Data: opai_titles}, nil
return oapi.GetTitles200JSONResponse{Cursor: new_cursor, Data: opai_titles}, nil
}