Count method
This commit is contained in:
32
app/repository/method_count.go
Normal file
32
app/repository/method_count.go
Normal 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
|
||||
}
|
||||
60
app/repository/method_count_test.go
Normal file
60
app/repository/method_count_test.go
Normal file
@ -0,0 +1,60 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCountMethod(t *testing.T) {
|
||||
t.Run("Count without filter", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
filter := MyModelFilter{}
|
||||
|
||||
sql := "SELECT count(*) FROM my_models"
|
||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
|
||||
|
||||
result, err := repo.Count(filter)
|
||||
assert.Equal(t, result, int64(0))
|
||||
assert.Nil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Count with filter", func(t *testing.T) {
|
||||
sqldb, db, mock := NewMockDB()
|
||||
defer sqldb.Close()
|
||||
|
||||
repo := RepoBase[MyModel]{}
|
||||
repo.Init(db, nil)
|
||||
|
||||
id1 := uuid.New()
|
||||
id2 := uuid.New()
|
||||
id3 := uuid.New()
|
||||
filter := MyModelFilter{
|
||||
Ids: &[]uuid.UUID{id1, id2, id3},
|
||||
}
|
||||
|
||||
sql := "SELECT count(*) FROM my_models WHERE my_models.id IN ($1,$2,$3)"
|
||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||
WithArgs(id1, id2, id3)
|
||||
|
||||
result, err := repo.Count(filter)
|
||||
assert.Equal(t, result, int64(0))
|
||||
assert.Nil(t, err)
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -43,9 +43,10 @@ func (m MyModel) TableName() string {
|
||||
}
|
||||
|
||||
type MyModelFilter struct {
|
||||
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
||||
Value *string `filterfield:"field=value,operator=EQ"`
|
||||
Count *int `filterfield:"field=count,operator=GT"`
|
||||
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
||||
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
|
||||
Value *string `filterfield:"field=value,operator=EQ"`
|
||||
Count *int `filterfield:"field=count,operator=GT"`
|
||||
}
|
||||
|
||||
func TestListMethod(t *testing.T) {
|
||||
|
||||
@ -20,10 +20,11 @@ type RepoBase[T schema.Tabler] struct {
|
||||
ListMethod[T]
|
||||
GetMethod[T]
|
||||
ExistsMethod[T]
|
||||
CountMethod[T]
|
||||
methods []MethodInitInterface[T]
|
||||
}
|
||||
|
||||
func (repo *RepoBase[T]) InitMethods(dbConn *gorm.DB) {
|
||||
func (repo *RepoBase[T]) InitMethods() {
|
||||
for _, method := range repo.methods {
|
||||
method.Init(repo)
|
||||
}
|
||||
@ -41,6 +42,11 @@ func (m *RepoBase[T]) Init(dbConn *gorm.DB, options *RepoOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
m.methods = []MethodInitInterface[T]{&m.ListMethod, &m.GetMethod, &m.ExistsMethod}
|
||||
m.InitMethods(dbConn)
|
||||
m.methods = []MethodInitInterface[T]{
|
||||
&m.ListMethod,
|
||||
&m.GetMethod,
|
||||
&m.ExistsMethod,
|
||||
&m.CountMethod,
|
||||
}
|
||||
m.InitMethods()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user