Verified Commit faf70c19 authored by Daniel Sonck's avatar Daniel Sonck
Browse files

Enable Connection on playlist endpoint

parent 8096497e
Pipeline #426 waiting for manual action with stages
in 3 minutes and 42 seconds
extend type Query {
playlist: [PlayListItem!]!
playlist(first: Int!, after: Cursor): PlaylistConnection!
}
\ No newline at end of file
type PlaylistConnection {
id: ID!
edges: [PlaylistEdge!]!
pageInfo: PageInfo!
}
type PlaylistEdge {
node: PlayListItem!
cursor: Cursor!
}
type PlayListItem {
id: Int!
......@@ -8,4 +19,12 @@ extend type CueTrack @key(fields: "start stop file") {
start: Int64! @external
stop: Int64! @external
file: String! @external
}
\ No newline at end of file
}
type PageInfo {
hasNextPage: Boolean!
startCursor: Cursor!
endCursor: Cursor!
}
scalar Cursor
\ No newline at end of file
This diff is collapsed.
package model
import "go.touhou.fm/radio/lib/protocol"
import (
"encoding/base64"
"fmt"
"go.touhou.fm/radio/lib/protocol"
"strconv"
"strings"
)
type PlayListItem struct {
Id int
......@@ -11,4 +17,28 @@ func (PlayListItem) IsEntity() {}
type CueTrack protocol.Song
func (CueTrack) IsEntity() {}
\ No newline at end of file
func (CueTrack) IsEntity() {}
type Cursor string
func MakeCursor(prefix string, position int) Cursor {
return (Cursor)(base64.StdEncoding.EncodeToString(([]byte)(fmt.Sprintf("%s:%d", prefix, position))))
}
func (after *Cursor) Parse(prefix string) (start int, err error) {
if after != nil {
cursor, err := base64.StdEncoding.DecodeString((string)(*after))
if err != nil {
return 0, fmt.Errorf("illegal cursor: %w", err)
}
if !strings.HasPrefix((string)(cursor), prefix+":") {
return 0, fmt.Errorf("illegal cursor")
}
start, err = strconv.Atoi((string)(*after)[len(prefix)+1:])
if err!=nil {
return 0, fmt.Errorf("illegal cursor: %w", err)
}
}
return -1, nil
}
\ No newline at end of file
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
package model
type PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
StartCursor Cursor `json:"startCursor"`
EndCursor Cursor `json:"endCursor"`
}
type PlaylistConnection struct {
ID string `json:"id"`
Edges []PlaylistEdge `json:"edges"`
PageInfo *PageInfo `json:"pageInfo"`
}
type PlaylistEdge struct {
Node *PlayListItem `json:"node"`
Cursor Cursor `json:"cursor"`
}
......@@ -16,18 +16,36 @@ func (r *queryResolver) PlaylistVersion(ctx context.Context) (string, error) {
return fmt.Sprintf("%s (%s)", internal.VersionText, internal.GoVersionText), nil
}
func (r *queryResolver) Playlist(ctx context.Context) ([]model.PlayListItem, error) {
var result []model.PlayListItem
func (r *queryResolver) Playlist(ctx context.Context, first int, after *model.Cursor) (*model.PlaylistConnection, error) {
start, err := after.Parse("PlaylistItem")
if err != nil {
return nil, err
}
var result []model.PlaylistEdge
playlist := r.PlaylistProvider.Playlist()
count := 0
for item := playlist.Start(); item != nil; item = item.Next() {
result = append(result, model.PlayListItem{
Id: count,
CueTrack: (*model.CueTrack)(&item.Song),
})
for item := playlist.Start(); item != nil && count < first; item = item.Next() {
if count > start {
result = append(result, model.PlaylistEdge{
Cursor: model.MakeCursor("PlaylistItem:%d", count),
Node: &model.PlayListItem{
Id: count,
CueTrack: (*model.CueTrack)(&item.Song),
},
})
}
count++
}
return result, nil
return &model.PlaylistConnection{
ID: fmt.Sprintf("%d:%d", start+1, count-1),
Edges: result,
PageInfo: &model.PageInfo{
HasNextPage: false,
StartCursor: model.MakeCursor("PlaylistItem", start+1),
EndCursor: model.MakeCursor("PlaylistItem", count-1),
},
}, nil
}
// Query returns generated.QueryResolver implementation.
......
package internal
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment