Sort messages by creation time

This commit is contained in:
Eli Ribble 2026-03-10 15:46:17 +00:00
parent 78d47c4035
commit 4ac7e29909
No known key found for this signature in database
4 changed files with 37 additions and 6 deletions

View file

@ -3,6 +3,7 @@ package api
import ( import (
"context" "context"
"net/http" "net/http"
"slices"
"time" "time"
"github.com/Gleipnir-Technology/nidus-sync/config" "github.com/Gleipnir-Technology/nidus-sync/config"
@ -72,6 +73,16 @@ func listCommunication(ctx context.Context, r *http.Request, org *models.Organiz
Type: "water", Type: "water",
} }
} }
_by_created := func(a, b communication) int {
if a.Created == b.Created {
return 0
} else if a.Created.Before(b.Created) {
return 1
} else {
return -1
}
}
slices.SortFunc(comms, _by_created)
return &contentListCommunication{ return &contentListCommunication{
Communications: comms, Communications: comms,
}, nil }, nil

View file

@ -19,12 +19,6 @@ import (
var decoder = schema.NewDecoder() var decoder = schema.NewDecoder()
type queryParams struct {
Limit *int `schema:"limit"`
Sort *string `schema:"sort"`
Type *string `schema:"type"`
}
type handlerFunctionGet[T any] func(context.Context, *http.Request, *models.Organization, *models.User, queryParams) (*T, *nhttp.ErrorWithStatus) 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 wrappedHandler func(http.ResponseWriter, *http.Request)
type contentAuthenticated[T any] struct { type contentAuthenticated[T any] struct {

25
api/query_params.go Normal file
View file

@ -0,0 +1,25 @@
package api
type queryParams struct {
Limit *int `schema:"limit"`
Sort *string `schema:"sort"`
Type *string `schema:"type"`
}
func (qp queryParams) SortOrDefault(default_name string, ascending bool) (string, bool) {
if qp.Sort == nil {
return default_name, ascending
}
s := *qp.Sort
if s == "" {
return default_name, ascending
}
a := true
if s[0] == '-' {
a = false
}
if s[0] == '+' || s[0] == '-' {
s = s[1:]
}
return s, a
}

View file

@ -130,6 +130,7 @@
try { try {
// Build query parameters from filters // Build query parameters from filters
const params = new URLSearchParams(); const params = new URLSearchParams();
params.append("sort", "-created");
if (this.typeFilter) params.append("type", this.typeFilter); if (this.typeFilter) params.append("type", this.typeFilter);
const response = await fetch( const response = await fetch(