43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package system
|
|
|
|
import (
|
|
"kra/pkg/database/pagination"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// listRows centralizes the storage-only part shared by simple repositories:
|
|
// 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) {
|
|
var total int64
|
|
if err := db.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if paginate {
|
|
if required {
|
|
db = pagination.ApplyRequired(db, page, size, 100)
|
|
} else {
|
|
db = pagination.Apply(db, page, size, 100)
|
|
}
|
|
}
|
|
var pos []PO
|
|
if err := db.Find(&pos).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
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)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]*DO, 0, len(pos))
|
|
for _, po := range pos {
|
|
out = append(out, convert(po))
|
|
}
|
|
return out, total, nil
|
|
}
|