2017-11-21 20:46:30 +00:00
|
|
|
package handler
|
2017-11-11 04:44:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-02-14 17:53:32 +00:00
|
|
|
"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 {
|
2018-02-14 17:53:32 +00:00
|
|
|
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)
|
2018-06-21 18:20:19 +00:00
|
|
|
// 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
|
|
|
|
}
|