feat(scope): added sorting
All checks were successful
Gitea Build Check / Build (push) Successful in 2m20s
Gitea Build Check / Build (pull_request) Successful in 2m18s

This commit is contained in:
SoXX 2024-08-14 15:21:29 +02:00
parent 2eeae0e5c7
commit 975e084b16
2 changed files with 127 additions and 8 deletions

View File

@ -59,10 +59,6 @@ func AdvancedPagination(value any, pagination *Pagination, db *gorm.DB) func(db
pagination.Page = 1 pagination.Page = 1
} }
if pagination.Sort == "" {
pagination.Sort = "id desc"
}
switch { switch {
case pagination.Limit > models.MaxPageSizeLimit: case pagination.Limit > models.MaxPageSizeLimit:
pagination.Limit = models.MaxPageSizeLimit pagination.Limit = models.MaxPageSizeLimit
@ -79,6 +75,17 @@ func AdvancedPagination(value any, pagination *Pagination, db *gorm.DB) func(db
offset := (pagination.Page - 1) * pagination.Limit offset := (pagination.Page - 1) * pagination.Limit
return func(db *gorm.DB) *gorm.DB { return func(db *gorm.DB) *gorm.DB {
return db.Offset(offset).Limit(pagination.Limit).Order(pagination.Sort) return db.Offset(offset).Limit(pagination.Limit)
} }
} }
// OrderBy applies an order operation to a GORM query.
// Parameters:
//
// - sort: a SQL order query like "id desc".
func OrderBy(sort string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Order(sort)
}
}

View File

@ -7,7 +7,6 @@ import (
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/test" "git.anthrove.art/Anthrove/otter-space-sdk/v3/test"
"github.com/davecgh/go-spew/spew"
"go.opentelemetry.io/contrib/bridges/otellogrus" "go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"gorm.io/gorm" "gorm.io/gorm"
@ -199,8 +198,6 @@ func TestAdvancedPagination(t *testing.T) {
var tags []models.Tag var tags []models.Tag
result := client.WithContext(ctx).Scopes(AdvancedPagination(tt.args.value, tt.args.pagination, tt.args.db)).Find(&tags) result := client.WithContext(ctx).Scopes(AdvancedPagination(tt.args.value, tt.args.pagination, tt.args.db)).Find(&tags)
spew.Dump(tt.args.pagination)
if result.Error != nil { if result.Error != nil {
t.Errorf("Paginate() = %v", result.Error) t.Errorf("Paginate() = %v", result.Error)
} }
@ -214,3 +211,118 @@ func TestAdvancedPagination(t *testing.T) {
}) })
} }
} }
func TestOrderBy(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tags to test with
validTags := []models.Tag{
{
Name: "a",
Type: models.General,
},
{
Name: "b",
Type: models.General,
},
{
Name: "c",
Type: models.Lore,
},
{
Name: "d",
Type: models.Artist,
},
{
Name: "e",
Type: models.Artist,
},
{
Name: "f",
Type: models.Copyright,
},
{
Name: "g",
Type: models.Meta,
},
{
Name: "h",
Type: models.Species,
},
{
Name: "i",
Type: models.Invalid,
},
{
Name: "j",
Type: models.General,
},
}
err = CreateTagInBatch(ctx, validTags, len(validTags))
if err != nil {
logger.Fatalf("Could not create tags: %v", err)
}
// --
// -- -- Tests
type args struct {
sort string
}
tests := []struct {
name string
args args
want []models.Tag
wantErr bool
}{
{
name: "Test 01: Valid Data",
args: args{
sort: "name asc",
},
want: validTags,
wantErr: false,
},
{
name: "Test 01: Invalid Data",
args: args{
sort: "name desc",
},
want: validTags,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tags []models.Tag
result := client.WithContext(ctx).Scopes(OrderBy(tt.args.sort)).Find(&tags)
if result.Error != nil {
t.Errorf("Paginate() = %v", result.Error)
}
if !reflect.DeepEqual(tt.want, tags) != tt.wantErr {
t.Errorf("Length of tags: %d", len(tags))
t.Errorf("Length of want: %d", len(tt.want))
t.Errorf("Paginate() = %v, want %v", tags, tt.want)
}
})
}
}