53 lines
857 B
Go
53 lines
857 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]
|
|
CountMethod[T]
|
|
methods []MethodInitInterface[T]
|
|
}
|
|
|
|
func (repo *RepoBase[T]) InitMethods() {
|
|
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.CountMethod,
|
|
}
|
|
m.InitMethods()
|
|
}
|