Repo options

This commit is contained in:
Eden Kirin
2024-06-25 01:19:40 +02:00
parent c2bcbea5d2
commit 4615d55309
8 changed files with 112 additions and 37 deletions

View File

@ -9,11 +9,11 @@ import (
)
type ExistsMethod[T schema.Tabler] struct {
DbConn *gorm.DB
repo *RepoBase[T]
}
func (m *ExistsMethod[T]) Init(dbConn *gorm.DB) {
m.DbConn = dbConn
func (m *ExistsMethod[T]) Init(repo *RepoBase[T]) {
m.repo = repo
}
func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
@ -21,14 +21,14 @@ func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
model T
)
query := m.DbConn.Model(model)
query := m.repo.dbConn.Model(model)
query, err := smartfilter.ToQuery(model, filter, query)
if err != nil {
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
return exists, nil

View 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)
}
})
}

View File

@ -3,7 +3,6 @@ package repository
import (
"repo-pattern/app/repository/smartfilter"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
@ -13,11 +12,11 @@ type GetOptions struct {
}
type GetMethod[T schema.Tabler] struct {
DbConn *gorm.DB
repo *RepoBase[T]
}
func (m *GetMethod[T]) Init(dbConn *gorm.DB) {
m.DbConn = dbConn
func (m *GetMethod[T]) Init(repo *RepoBase[T]) {
m.repo = repo
}
func (m GetMethod[T]) Get(filter interface{}, options *GetOptions) (*T, error) {
@ -25,7 +24,7 @@ func (m GetMethod[T]) Get(filter interface{}, options *GetOptions) (*T, error) {
model T
)
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
query, err := smartfilter.ToQuery(model, filter, m.repo.dbConn)
if err != nil {
return nil, err
}

View File

@ -15,7 +15,7 @@ func TestGetMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
id := uuid.New()
filter := MyModelFilter{
@ -40,7 +40,7 @@ func TestGetMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
id := uuid.New()
raiseError := true

View File

@ -3,7 +3,6 @@ package repository
import (
"repo-pattern/app/repository/smartfilter"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
@ -14,11 +13,11 @@ type ListOptions struct {
}
type ListMethod[T schema.Tabler] struct {
DbConn *gorm.DB
repo *RepoBase[T]
}
func (m *ListMethod[T]) Init(dbConn *gorm.DB) {
m.DbConn = dbConn
func (m *ListMethod[T]) Init(repo *RepoBase[T]) {
m.repo = repo
}
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
)
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
query, err := smartfilter.ToQuery(model, filter, m.repo.dbConn)
if err != nil {
return nil, err
}

View File

@ -54,7 +54,7 @@ func TestListMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
filter := MyModelFilter{}
options := ListOptions{
@ -86,7 +86,7 @@ func TestListMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
filter := MyModelFilter{}
options := ListOptions{
@ -112,7 +112,7 @@ func TestListMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
filter := MyModelFilter{}
options := ListOptions{
@ -138,7 +138,7 @@ func TestListMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
filter := MyModelFilter{}
options := ListOptions{
@ -165,7 +165,7 @@ func TestListMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
id := uuid.New()
filter := MyModelFilter{
@ -189,7 +189,7 @@ func TestListMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
id := uuid.New()
count := 456
@ -217,7 +217,7 @@ func TestListMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
id := uuid.New()
count := 456
@ -251,7 +251,7 @@ func TestListMethod(t *testing.T) {
defer sqldb.Close()
repo := RepoBase[MyModel]{}
repo.Init(db)
repo.Init(db, nil)
filter := MyModelFilter{}
options := ListOptions{

View File

@ -5,25 +5,42 @@ import (
"gorm.io/gorm/schema"
)
type MethodInitInterface interface {
Init(dbConn *gorm.DB)
type MethodInitInterface[T schema.Tabler] interface {
Init(repo *RepoBase[T])
}
type RepoOptions struct {
IdField string
}
type RepoBase[T schema.Tabler] struct {
DbConn int
IdField string
dbConn *gorm.DB
ListMethod[T]
GetMethod[T]
ExistsMethod[T]
methods []MethodInitInterface
methods []MethodInitInterface[T]
}
func (b *RepoBase[T]) InitMethods(dbConn *gorm.DB) {
for _, method := range b.methods {
method.Init(dbConn)
func (repo *RepoBase[T]) InitMethods(dbConn *gorm.DB) {
for _, method := range repo.methods {
method.Init(repo)
}
}
func (m *RepoBase[T]) Init(dbConn *gorm.DB) {
m.methods = []MethodInitInterface{&m.ListMethod, &m.GetMethod, &m.ExistsMethod}
func (m *RepoBase[T]) Init(dbConn *gorm.DB, options *RepoOptions) {
m.dbConn = 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.InitMethods(dbConn)
}