emissary/internal/server/api/query_spec_definitions.go

68 lines
1.5 KiB
Go

package api
import (
"net/http"
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/api"
"gitlab.com/wpetit/goweb/logger"
)
func (m *Mount) querySpecDefinitions(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
limit, ok := getIntQueryParam(w, r, "limit", 10)
if !ok {
return
}
offset, ok := getIntQueryParam(w, r, "offset", 0)
if !ok {
return
}
options := []datastore.SpecDefinitionQueryOptionFunc{
datastore.WithSpecDefinitionQueryLimit(int(limit)),
datastore.WithSpecDefinitionQueryOffset(int(offset)),
}
names, ok := getStringSliceValues(w, r, "names", nil)
if !ok {
return
}
if len(names) > 0 {
options = append(options, datastore.WithSpecDefinitionQueryNames(names...))
}
versions, ok := getStringSliceValues(w, r, "versions", nil)
if !ok {
return
}
if len(names) > 0 {
options = append(options, datastore.WithSpecDefinitionQueryVersions(versions...))
}
specDefinitions, total, err := m.specDefRepo.Query(
ctx,
options...,
)
if err != nil {
err = errors.WithStack(err)
logger.Error(ctx, "could not list spec definitions", logger.CapturedE(err))
api.ErrorResponse(w, http.StatusInternalServerError, ErrCodeUnknownError, nil)
return
}
api.DataResponse(w, http.StatusOK, struct {
SpecDefinitions []datastore.SpecDefinitionHeader `json:"specDefinitions"`
Total int `json:"total"`
}{
SpecDefinitions: specDefinitions,
Total: total,
})
}