diff --git a/api/handler.go b/api/handler.go index b4e51053..de42c558 100644 --- a/api/handler.go +++ b/api/handler.go @@ -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 { diff --git a/api/lead.go b/api/lead.go index f45072f9..f498fb5a 100644 --- a/api/lead.go +++ b/api/lead.go @@ -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 diff --git a/api/signal.go b/api/signal.go index 5b081446..7a665803 100644 --- a/api/signal.go +++ b/api/signal.go @@ -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]()) /*