Add query args passing to API endpoints
This commit is contained in:
parent
7723f03915
commit
de63a47c64
3 changed files with 21 additions and 4 deletions
|
|
@ -19,7 +19,12 @@ import (
|
|||
|
||||
var decoder = schema.NewDecoder()
|
||||
|
||||
type handlerFunctionGet[T any] func(context.Context, *http.Request, *models.Organization, *models.User) (*T, *nhttp.ErrorWithStatus)
|
||||
type queryParams struct {
|
||||
Limit *int `schema:"limit"`
|
||||
Sort *string `schema:"sort"`
|
||||
}
|
||||
|
||||
type handlerFunctionGet[T any] func(context.Context, *http.Request, *models.Organization, *models.User, queryParams) (*T, *nhttp.ErrorWithStatus)
|
||||
type wrappedHandler func(http.ResponseWriter, *http.Request)
|
||||
type contentAuthenticated[T any] struct {
|
||||
C T
|
||||
|
|
@ -44,7 +49,14 @@ func authenticatedHandlerJSON[T any](f handlerFunctionGet[T]) http.Handler {
|
|||
return
|
||||
}
|
||||
var body []byte
|
||||
resp, e := f(ctx, r, org, u)
|
||||
var params queryParams
|
||||
err = decoder.Decode(¶ms, r.URL.Query())
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("decode query failure")
|
||||
http.Error(w, "failed to decode query", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
resp, e := f(ctx, r, org, u, params)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
//log.Info().Str("template", template).Err(e).Msg("handler done")
|
||||
if e != nil {
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ type lead struct {
|
|||
ID int32 `json:"id"`
|
||||
}
|
||||
|
||||
func listLead(ctx context.Context, r *http.Request, org *models.Organization, user *models.User) (*contentListLead, *nhttp.ErrorWithStatus) {
|
||||
func listLead(ctx context.Context, r *http.Request, org *models.Organization, user *models.User, query queryParams) (*contentListLead, *nhttp.ErrorWithStatus) {
|
||||
return &contentListLead{
|
||||
Leads: make([]lead, 0),
|
||||
}, nil
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ type contentListSignal struct {
|
|||
Signals []signal `json:"signals"`
|
||||
}
|
||||
|
||||
func listSignal(ctx context.Context, r *http.Request, org *models.Organization, user *models.User) (*contentListSignal, *nhttp.ErrorWithStatus) {
|
||||
func listSignal(ctx context.Context, r *http.Request, org *models.Organization, user *models.User, query queryParams) (*contentListSignal, *nhttp.ErrorWithStatus) {
|
||||
type _Row struct {
|
||||
Address Address `db:"address"`
|
||||
Addressed *time.Time `db:"addressed"`
|
||||
|
|
@ -56,6 +56,10 @@ func listSignal(ctx context.Context, r *http.Request, org *models.Organization,
|
|||
Title string `db:"title"`
|
||||
Type string `db:"type"`
|
||||
}
|
||||
limit := 20
|
||||
if query.Limit != nil {
|
||||
limit = *query.Limit
|
||||
}
|
||||
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
|
||||
sm.Columns(
|
||||
"signal.addressed AS addressed",
|
||||
|
|
@ -97,6 +101,7 @@ func listSignal(ctx context.Context, r *http.Request, org *models.Organization,
|
|||
),
|
||||
sm.Where(psql.Quote("signal", "organization_id").EQ(psql.Arg(org.ID))),
|
||||
sm.Where(psql.Quote("signal", "addressed").IsNull()),
|
||||
sm.Limit(limit),
|
||||
), scan.StructMapper[_Row]())
|
||||
|
||||
/*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue