Count method

This commit is contained in:
Eden Kirin
2024-06-25 01:52:50 +02:00
parent 4615d55309
commit 3dc8d0d79f
5 changed files with 122 additions and 7 deletions

View File

@ -0,0 +1,32 @@
package repository
import (
"repo-pattern/app/repository/smartfilter"
"gorm.io/gorm/schema"
)
type CountMethod[T schema.Tabler] struct {
repo *RepoBase[T]
}
func (m *CountMethod[T]) Init(repo *RepoBase[T]) {
m.repo = repo
}
func (m CountMethod[T]) Count(filter interface{}) (int64, error) {
var (
model T
count int64
)
query := m.repo.dbConn.Model(model)
query, err := smartfilter.ToQuery(model, filter, query)
if err != nil {
return 0, err
}
query.Count(&count)
return count, nil
}