Compare commits
3 Commits
ff51420b71
...
3dc8d0d79f
| Author | SHA1 | Date | |
|---|---|---|---|
| 3dc8d0d79f | |||
| 4615d55309 | |||
| c2bcbea5d2 |
24
app/main.go
24
app/main.go
@ -73,7 +73,7 @@ func doMagic(db *gorm.DB) {
|
|||||||
|
|
||||||
func doList(db *gorm.DB) {
|
func doList(db *gorm.DB) {
|
||||||
repo := repository.RepoBase[models.Cert]{}
|
repo := repository.RepoBase[models.Cert]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
filter := CertFilter{
|
filter := CertFilter{
|
||||||
Alive: &TRUE,
|
Alive: &TRUE,
|
||||||
@ -89,9 +89,24 @@ func doList(db *gorm.DB) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doCount(db *gorm.DB) {
|
||||||
|
repo := repository.RepoBase[models.Cert]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
filter := CertFilter{
|
||||||
|
Alive: &TRUE,
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := repo.Count(filter)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Printf(">>> count: %d\n", count)
|
||||||
|
}
|
||||||
|
|
||||||
func doGet(db *gorm.DB) {
|
func doGet(db *gorm.DB) {
|
||||||
repo := repository.RepoBase[models.Cert]{}
|
repo := repository.RepoBase[models.Cert]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
id, _ := uuid.Parse("db9fb837-3483-4736-819d-f427dc8cda23")
|
id, _ := uuid.Parse("db9fb837-3483-4736-819d-f427dc8cda23")
|
||||||
|
|
||||||
@ -108,7 +123,7 @@ func doGet(db *gorm.DB) {
|
|||||||
|
|
||||||
func doExists(db *gorm.DB) {
|
func doExists(db *gorm.DB) {
|
||||||
repo := repository.RepoBase[models.Cert]{}
|
repo := repository.RepoBase[models.Cert]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
id, _ := uuid.Parse("db9fb837-3483-4736-819d-f427dc8cda23")
|
id, _ := uuid.Parse("db9fb837-3483-4736-819d-f427dc8cda23")
|
||||||
|
|
||||||
@ -130,8 +145,9 @@ func main() {
|
|||||||
|
|
||||||
db := db.InitDB()
|
db := db.InitDB()
|
||||||
|
|
||||||
doMagic(db)
|
// doMagic(db)
|
||||||
// doList(db)
|
// doList(db)
|
||||||
|
doCount(db)
|
||||||
// doGet(db)
|
// doGet(db)
|
||||||
// doExists(db)
|
// doExists(db)
|
||||||
// inheritance.DoInheritanceTest()
|
// inheritance.DoInheritanceTest()
|
||||||
|
|||||||
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -9,11 +9,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ExistsMethod[T schema.Tabler] struct {
|
type ExistsMethod[T schema.Tabler] struct {
|
||||||
DbConn *gorm.DB
|
repo *RepoBase[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ExistsMethod[T]) Init(dbConn *gorm.DB) {
|
func (m *ExistsMethod[T]) Init(repo *RepoBase[T]) {
|
||||||
m.DbConn = dbConn
|
m.repo = repo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
|
func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
|
||||||
@ -21,14 +21,14 @@ func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
|
|||||||
model T
|
model T
|
||||||
)
|
)
|
||||||
|
|
||||||
query := m.DbConn.Model(model)
|
query := m.repo.dbConn.Model(model)
|
||||||
|
|
||||||
query, err := smartfilter.ToQuery(model, filter, query)
|
query, err := smartfilter.ToQuery(model, filter, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result := query.Select("*").First(&model)
|
result := query.Select(m.repo.IdField).Take(&model)
|
||||||
|
|
||||||
exists := !errors.Is(result.Error, gorm.ErrRecordNotFound) && result.Error == nil
|
exists := !errors.Is(result.Error, gorm.ErrRecordNotFound) && result.Error == nil
|
||||||
return exists, nil
|
return exists, nil
|
||||||
|
|||||||
60
app/repository/method_exists_test.go
Normal file
60
app/repository/method_exists_test.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExistsMethod(t *testing.T) {
|
||||||
|
t.Run("Without repo options", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
id := uuid.New()
|
||||||
|
filter := MyModelFilter{
|
||||||
|
Id: &id,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "SELECT id FROM my_models WHERE my_models.id = $1 LIMIT $2"
|
||||||
|
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).WithArgs(id, 1)
|
||||||
|
|
||||||
|
result, err := repo.Exists(filter)
|
||||||
|
assert.False(t, result)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With id field set in repo options", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, &RepoOptions{IdField: "some_other_pk"})
|
||||||
|
|
||||||
|
id := uuid.New()
|
||||||
|
filter := MyModelFilter{
|
||||||
|
Id: &id,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "SELECT some_other_pk FROM my_models WHERE my_models.id = $1 LIMIT $2"
|
||||||
|
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).WithArgs(id, 1)
|
||||||
|
|
||||||
|
result, err := repo.Exists(filter)
|
||||||
|
assert.False(t, result)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -3,20 +3,20 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"repo-pattern/app/repository/smartfilter"
|
"repo-pattern/app/repository/smartfilter"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"gorm.io/gorm/schema"
|
"gorm.io/gorm/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetOptions struct {
|
type GetOptions struct {
|
||||||
Only *[]string
|
Only *[]string
|
||||||
|
RaiseError *bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetMethod[T schema.Tabler] struct {
|
type GetMethod[T schema.Tabler] struct {
|
||||||
DbConn *gorm.DB
|
repo *RepoBase[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *GetMethod[T]) Init(dbConn *gorm.DB) {
|
func (m *GetMethod[T]) Init(repo *RepoBase[T]) {
|
||||||
m.DbConn = dbConn
|
m.repo = repo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m GetMethod[T]) Get(filter interface{}, options *GetOptions) (*T, error) {
|
func (m GetMethod[T]) Get(filter interface{}, options *GetOptions) (*T, error) {
|
||||||
@ -24,7 +24,7 @@ func (m GetMethod[T]) Get(filter interface{}, options *GetOptions) (*T, error) {
|
|||||||
model T
|
model T
|
||||||
)
|
)
|
||||||
|
|
||||||
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
|
query, err := smartfilter.ToQuery(model, filter, m.repo.dbConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -34,9 +34,12 @@ func (m GetMethod[T]) Get(filter interface{}, options *GetOptions) (*T, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result := query.First(&model)
|
result := query.First(&model)
|
||||||
if result.Error != nil {
|
if result.Error == nil {
|
||||||
return nil, result.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
return &model, nil
|
return &model, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options != nil && options.RaiseError != nil && *options.RaiseError {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|||||||
67
app/repository/method_get_test.go
Normal file
67
app/repository/method_get_test.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetMethod(t *testing.T) {
|
||||||
|
t.Run("With id filter, suppress errors", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
id := uuid.New()
|
||||||
|
filter := MyModelFilter{
|
||||||
|
Id: &id,
|
||||||
|
}
|
||||||
|
options := GetOptions{}
|
||||||
|
|
||||||
|
sql := "SELECT * FROM my_models WHERE my_models.id = $1 ORDER BY my_models.id LIMIT $2"
|
||||||
|
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).WithArgs(id, 1)
|
||||||
|
|
||||||
|
result, err := repo.Get(filter, &options)
|
||||||
|
assert.Nil(t, result)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With id filter, raise error", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
id := uuid.New()
|
||||||
|
raiseError := true
|
||||||
|
filter := MyModelFilter{
|
||||||
|
Id: &id,
|
||||||
|
}
|
||||||
|
options := GetOptions{
|
||||||
|
RaiseError: &raiseError,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "SELECT * FROM my_models WHERE my_models.id = $1 ORDER BY my_models.id LIMIT $2"
|
||||||
|
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||||
|
WithArgs(id, 1).
|
||||||
|
WillReturnError(nil)
|
||||||
|
|
||||||
|
result, err := repo.Get(filter, &options)
|
||||||
|
assert.Nil(t, result)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -3,7 +3,6 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"repo-pattern/app/repository/smartfilter"
|
"repo-pattern/app/repository/smartfilter"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"gorm.io/gorm/schema"
|
"gorm.io/gorm/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,11 +13,11 @@ type ListOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ListMethod[T schema.Tabler] struct {
|
type ListMethod[T schema.Tabler] struct {
|
||||||
DbConn *gorm.DB
|
repo *RepoBase[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ListMethod[T]) Init(dbConn *gorm.DB) {
|
func (m *ListMethod[T]) Init(repo *RepoBase[T]) {
|
||||||
m.DbConn = dbConn
|
m.repo = repo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m ListMethod[T]) List(filter interface{}, options *ListOptions) (*[]T, error) {
|
func (m ListMethod[T]) List(filter interface{}, options *ListOptions) (*[]T, error) {
|
||||||
@ -27,7 +26,7 @@ func (m ListMethod[T]) List(filter interface{}, options *ListOptions) (*[]T, err
|
|||||||
models []T
|
models []T
|
||||||
)
|
)
|
||||||
|
|
||||||
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
|
query, err := smartfilter.ToQuery(model, filter, m.repo.dbConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,6 +44,7 @@ func (m MyModel) TableName() string {
|
|||||||
|
|
||||||
type MyModelFilter struct {
|
type MyModelFilter struct {
|
||||||
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
||||||
|
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
|
||||||
Value *string `filterfield:"field=value,operator=EQ"`
|
Value *string `filterfield:"field=value,operator=EQ"`
|
||||||
Count *int `filterfield:"field=count,operator=GT"`
|
Count *int `filterfield:"field=count,operator=GT"`
|
||||||
}
|
}
|
||||||
@ -54,7 +55,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
defer sqldb.Close()
|
defer sqldb.Close()
|
||||||
|
|
||||||
repo := RepoBase[MyModel]{}
|
repo := RepoBase[MyModel]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
filter := MyModelFilter{}
|
filter := MyModelFilter{}
|
||||||
options := ListOptions{
|
options := ListOptions{
|
||||||
@ -86,7 +87,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
defer sqldb.Close()
|
defer sqldb.Close()
|
||||||
|
|
||||||
repo := RepoBase[MyModel]{}
|
repo := RepoBase[MyModel]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
filter := MyModelFilter{}
|
filter := MyModelFilter{}
|
||||||
options := ListOptions{
|
options := ListOptions{
|
||||||
@ -112,7 +113,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
defer sqldb.Close()
|
defer sqldb.Close()
|
||||||
|
|
||||||
repo := RepoBase[MyModel]{}
|
repo := RepoBase[MyModel]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
filter := MyModelFilter{}
|
filter := MyModelFilter{}
|
||||||
options := ListOptions{
|
options := ListOptions{
|
||||||
@ -138,7 +139,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
defer sqldb.Close()
|
defer sqldb.Close()
|
||||||
|
|
||||||
repo := RepoBase[MyModel]{}
|
repo := RepoBase[MyModel]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
filter := MyModelFilter{}
|
filter := MyModelFilter{}
|
||||||
options := ListOptions{
|
options := ListOptions{
|
||||||
@ -165,7 +166,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
defer sqldb.Close()
|
defer sqldb.Close()
|
||||||
|
|
||||||
repo := RepoBase[MyModel]{}
|
repo := RepoBase[MyModel]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
id := uuid.New()
|
id := uuid.New()
|
||||||
filter := MyModelFilter{
|
filter := MyModelFilter{
|
||||||
@ -189,7 +190,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
defer sqldb.Close()
|
defer sqldb.Close()
|
||||||
|
|
||||||
repo := RepoBase[MyModel]{}
|
repo := RepoBase[MyModel]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
id := uuid.New()
|
id := uuid.New()
|
||||||
count := 456
|
count := 456
|
||||||
@ -217,7 +218,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
defer sqldb.Close()
|
defer sqldb.Close()
|
||||||
|
|
||||||
repo := RepoBase[MyModel]{}
|
repo := RepoBase[MyModel]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
id := uuid.New()
|
id := uuid.New()
|
||||||
count := 456
|
count := 456
|
||||||
@ -251,7 +252,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
defer sqldb.Close()
|
defer sqldb.Close()
|
||||||
|
|
||||||
repo := RepoBase[MyModel]{}
|
repo := RepoBase[MyModel]{}
|
||||||
repo.Init(db)
|
repo.Init(db, nil)
|
||||||
|
|
||||||
filter := MyModelFilter{}
|
filter := MyModelFilter{}
|
||||||
options := ListOptions{
|
options := ListOptions{
|
||||||
@ -5,25 +5,48 @@ import (
|
|||||||
"gorm.io/gorm/schema"
|
"gorm.io/gorm/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MethodInitInterface interface {
|
type MethodInitInterface[T schema.Tabler] interface {
|
||||||
Init(dbConn *gorm.DB)
|
Init(repo *RepoBase[T])
|
||||||
|
}
|
||||||
|
|
||||||
|
type RepoOptions struct {
|
||||||
|
IdField string
|
||||||
}
|
}
|
||||||
|
|
||||||
type RepoBase[T schema.Tabler] struct {
|
type RepoBase[T schema.Tabler] struct {
|
||||||
DbConn int
|
IdField string
|
||||||
|
dbConn *gorm.DB
|
||||||
|
|
||||||
ListMethod[T]
|
ListMethod[T]
|
||||||
GetMethod[T]
|
GetMethod[T]
|
||||||
ExistsMethod[T]
|
ExistsMethod[T]
|
||||||
methods []MethodInitInterface
|
CountMethod[T]
|
||||||
|
methods []MethodInitInterface[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *RepoBase[T]) InitMethods(dbConn *gorm.DB) {
|
func (repo *RepoBase[T]) InitMethods() {
|
||||||
for _, method := range b.methods {
|
for _, method := range repo.methods {
|
||||||
method.Init(dbConn)
|
method.Init(repo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *RepoBase[T]) Init(dbConn *gorm.DB) {
|
func (m *RepoBase[T]) Init(dbConn *gorm.DB, options *RepoOptions) {
|
||||||
m.methods = []MethodInitInterface{&m.ListMethod, &m.GetMethod, &m.ExistsMethod}
|
m.dbConn = dbConn
|
||||||
m.InitMethods(dbConn)
|
|
||||||
|
if options == nil {
|
||||||
|
// no options provided? set defaults
|
||||||
|
m.IdField = "id"
|
||||||
|
} else {
|
||||||
|
if len(options.IdField) > 0 {
|
||||||
|
m.IdField = options.IdField
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.methods = []MethodInitInterface[T]{
|
||||||
|
&m.ListMethod,
|
||||||
|
&m.GetMethod,
|
||||||
|
&m.ExistsMethod,
|
||||||
|
&m.CountMethod,
|
||||||
|
}
|
||||||
|
m.InitMethods()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user