47 lines
834 B
Go
47 lines
834 B
Go
package repository
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/schema"
|
|
)
|
|
|
|
type MethodInitInterface[T schema.Tabler] interface {
|
|
Init(repo *RepoBase[T])
|
|
}
|
|
|
|
type RepoOptions struct {
|
|
IdField string
|
|
}
|
|
|
|
type RepoBase[T schema.Tabler] struct {
|
|
IdField string
|
|
dbConn *gorm.DB
|
|
|
|
ListMethod[T]
|
|
GetMethod[T]
|
|
ExistsMethod[T]
|
|
methods []MethodInitInterface[T]
|
|
}
|
|
|
|
func (repo *RepoBase[T]) InitMethods(dbConn *gorm.DB) {
|
|
for _, method := range repo.methods {
|
|
method.Init(repo)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|