@@ -22,7 +22,10 @@ const (
2222 defaultContentTypHeader = "application/vnd.api+json"
2323)
2424
25- var queryFieldsRegex = regexp .MustCompile (`^fields\[(\w+)\]$` )
25+ var (
26+ queryPageRegex = regexp .MustCompile (`^page\[(\w+)\]$` )
27+ queryFieldsRegex = regexp .MustCompile (`^fields\[(\w+)\]$` )
28+ )
2629
2730type information struct {
2831 prefix string
@@ -404,9 +407,15 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD) *r
404407func buildRequest (c APIContexter , r * http.Request ) Request {
405408 req := Request {PlainRequest : r }
406409 params := make (map [string ][]string )
410+ pagination := make (map [string ]string )
407411 for key , values := range r .URL .Query () {
408412 params [key ] = strings .Split (values [0 ], "," )
413+ pageMatches := queryPageRegex .FindStringSubmatch (key )
414+ if len (pageMatches ) > 1 {
415+ pagination [pageMatches [1 ]] = values [0 ]
416+ }
409417 }
418+ req .Pagination = pagination
410419 req .QueryParams = params
411420 req .Header = r .Header
412421 req .Context = c
@@ -427,25 +436,24 @@ func (res *resource) marshalResponse(resp interface{}, w http.ResponseWriter, st
427436}
428437
429438func (res * resource ) handleIndex (c APIContexter , w http.ResponseWriter , r * http.Request , info information ) error {
430- pagination := newPaginationQueryParams (r )
431- if pagination .isValid () {
432- source , ok := res .source .(PaginatedFindAll )
433- if ! ok {
434- return NewHTTPError (nil , "Resource does not implement the PaginatedFindAll interface" , http .StatusNotFound )
435- }
439+ if source , ok := res .source .(PaginatedFindAll ); ok {
440+ pagination := newPaginationQueryParams (r )
436441
437- count , response , err := source .PaginatedFindAll (buildRequest (c , r ))
438- if err != nil {
439- return err
440- }
442+ if pagination .isValid () {
443+ count , response , err := source .PaginatedFindAll (buildRequest (c , r ))
444+ if err != nil {
445+ return err
446+ }
441447
442- paginationLinks , err := pagination .getLinks (r , count , info )
443- if err != nil {
444- return err
445- }
448+ paginationLinks , err := pagination .getLinks (r , count , info )
449+ if err != nil {
450+ return err
451+ }
446452
447- return res .respondWithPagination (response , info , http .StatusOK , paginationLinks , w , r )
453+ return res .respondWithPagination (response , info , http .StatusOK , paginationLinks , w , r )
454+ }
448455 }
456+
449457 source , ok := res .source .(FindAll )
450458 if ! ok {
451459 return NewHTTPError (nil , "Resource does not implement the FindAll interface" , http .StatusNotFound )
@@ -506,26 +514,23 @@ func (res *resource) handleLinked(c APIContexter, api *API, w http.ResponseWrite
506514 request .QueryParams [res .name + "ID" ] = []string {id }
507515 request .QueryParams [res .name + "Name" ] = []string {linked .Name }
508516
509- // check for pagination, otherwise normal FindAll
510- pagination := newPaginationQueryParams (r )
511- if pagination .isValid () {
512- source , ok := resource .source .(PaginatedFindAll )
513- if ! ok {
514- return NewHTTPError (nil , "Resource does not implement the PaginatedFindAll interface" , http .StatusNotFound )
515- }
517+ if source , ok := resource .source .(PaginatedFindAll ); ok {
518+ // check for pagination, otherwise normal FindAll
519+ pagination := newPaginationQueryParams (r )
520+ if pagination .isValid () {
521+ var count uint
522+ count , response , err := source .PaginatedFindAll (request )
523+ if err != nil {
524+ return err
525+ }
516526
517- var count uint
518- count , response , err := source .PaginatedFindAll (request )
519- if err != nil {
520- return err
521- }
527+ paginationLinks , err := pagination .getLinks (r , count , info )
528+ if err != nil {
529+ return err
530+ }
522531
523- paginationLinks , err := pagination .getLinks (r , count , info )
524- if err != nil {
525- return err
532+ return res .respondWithPagination (response , info , http .StatusOK , paginationLinks , w , r )
526533 }
527-
528- return res .respondWithPagination (response , info , http .StatusOK , paginationLinks , w , r )
529534 }
530535
531536 source , ok := resource .source .(FindAll )
@@ -917,6 +922,15 @@ func (res *resource) respondWith(obj Responder, info information, status int, w
917922 data .Meta = meta
918923 }
919924
925+ if objWithLinks , ok := obj .(LinksResponder ); ok {
926+ baseURL := strings .Trim (info .GetBaseURL (), "/" )
927+ requestURL := fmt .Sprintf ("%s%s" , baseURL , r .URL .Path )
928+ links := objWithLinks .Links (r , requestURL )
929+ if len (links ) > 0 {
930+ data .Links = links
931+ }
932+ }
933+
920934 return res .marshalResponse (data , w , status , r )
921935}
922936
0 commit comments