1
0
mirror of https://github.com/rancher/norman.git synced 2025-06-20 12:41:56 +00:00
norman/api/handler/list.go

44 lines
978 B
Go
Raw Permalink Normal View History

2017-11-21 20:46:30 +00:00
package handler
2017-11-11 04:44:02 +00:00
import (
"net/http"
"github.com/rancher/norman/httperror"
2017-11-11 04:44:02 +00:00
"github.com/rancher/norman/parse"
"github.com/rancher/norman/types"
)
2018-01-30 23:49:37 +00:00
func ListHandler(request *types.APIContext, next types.RequestHandler) error {
2017-11-11 04:44:02 +00:00
var (
err error
data interface{}
)
store := request.Schema.Store
if store == nil {
return httperror.NewAPIError(httperror.NotFound, "no store found")
2017-11-11 04:44:02 +00:00
}
if request.ID == "" {
2017-11-21 20:46:30 +00:00
opts := parse.QueryOptions(request, request.Schema)
// Save the pagination on the context so it's not reset later
request.Pagination = opts.Pagination
2017-12-28 15:47:10 +00:00
data, err = store.List(request, request.Schema, &opts)
2017-11-11 04:44:02 +00:00
} else if request.Link == "" {
data, err = store.ByID(request, request.Schema, request.ID)
} else {
2018-03-22 22:53:36 +00:00
_, err = store.ByID(request, request.Schema, request.ID)
if err != nil {
return err
}
2018-01-30 23:49:37 +00:00
return request.Schema.LinkHandler(request, nil)
2017-11-11 04:44:02 +00:00
}
if err != nil {
return err
}
request.WriteResponse(http.StatusOK, data)
return nil
}