优化结构
This commit is contained in:
@@ -10,13 +10,18 @@ import (
|
||||
// count the filtered query, apply the bounded page, load POs, and convert them
|
||||
// at the data/biz boundary. Domain-specific filters and ordering stay in each
|
||||
// repository.
|
||||
func queryRows[PO any](db *gorm.DB, page, size int, paginate, required bool) ([]PO, int64, error) {
|
||||
type rowQueryOptions struct {
|
||||
Paginate bool
|
||||
Required bool
|
||||
}
|
||||
|
||||
func queryRows[PO any](db *gorm.DB, page, size int, options rowQueryOptions) ([]PO, int64, error) {
|
||||
var total int64
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if paginate {
|
||||
if required {
|
||||
if options.Paginate {
|
||||
if options.Required {
|
||||
db = pagination.ApplyRequired(db, page, size, 100)
|
||||
} else {
|
||||
db = pagination.Apply(db, page, size, 100)
|
||||
@@ -29,8 +34,8 @@ func queryRows[PO any](db *gorm.DB, page, size int, paginate, required bool) ([]
|
||||
return pos, total, nil
|
||||
}
|
||||
|
||||
func listRows[PO any, DO any](db *gorm.DB, page, size int, paginate bool, convert func(PO) *DO) ([]*DO, int64, error) {
|
||||
pos, total, err := queryRows[PO](db, page, size, paginate, false)
|
||||
func listRows[PO any, DO any](db *gorm.DB, page, size int, options rowQueryOptions, convert func(PO) *DO) ([]*DO, int64, error) {
|
||||
pos, total, err := queryRows[PO](db, page, size, options)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user