fix: need a Database and DatabaseService getter, and a getter on Database

This commit is contained in:
Derrick Hammer 2024-01-16 01:24:47 -05:00
parent 0c5827ce0b
commit c2075989fa
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 17 additions and 6 deletions

View File

@ -72,7 +72,7 @@ func initAccess(p interfaces.Portal) error {
} }
func initDatabase(p interfaces.Portal) error { func initDatabase(p interfaces.Portal) error {
return p.Database().Init(p) return p.DatabaseService().Init(p)
} }
func initProtocols(p interfaces.Portal) error { func initProtocols(p interfaces.Portal) error {

View File

@ -31,9 +31,12 @@ type PortalImpl struct {
accounts interfaces.AccountService accounts interfaces.AccountService
} }
func (p *PortalImpl) Database() interfaces.Database { func (p *PortalImpl) DatabaseService() interfaces.Database {
return p.database return p.database
} }
func (p *PortalImpl) Database() *gorm.DB {
return p.database.Get()
}
func NewPortal() interfaces.Portal { func NewPortal() interfaces.Portal {
portal := &PortalImpl{ portal := &PortalImpl{

View File

@ -15,7 +15,7 @@ var (
) )
type DatabaseImpl struct { type DatabaseImpl struct {
DB *gorm.DB db *gorm.DB
portal interfaces.Portal portal interfaces.Portal
} }
@ -43,14 +43,14 @@ func (d *DatabaseImpl) Init(p interfaces.Portal) error {
if err != nil { if err != nil {
p.Logger().Error("Failed to connect to database", zap.Error(err)) p.Logger().Error("Failed to connect to database", zap.Error(err))
} }
d.DB = db d.db = db
return nil return nil
} }
// Start performs any additional setup // Start performs any additional setup
func (d *DatabaseImpl) Start() error { func (d *DatabaseImpl) Start() error {
return d.DB.AutoMigrate( return d.db.AutoMigrate(
&models.APIKey{}, &models.APIKey{},
&models.Blocklist{}, &models.Blocklist{},
&models.Download{}, &models.Download{},
@ -60,3 +60,7 @@ func (d *DatabaseImpl) Start() error {
&models.User{}, &models.User{},
) )
} }
func (d *DatabaseImpl) Get() *gorm.DB {
return d.db
}

View File

@ -1,6 +1,9 @@
package interfaces package interfaces
import "gorm.io/gorm"
type Database interface { type Database interface {
Init(p Portal) error Init(p Portal) error
Start() error Start() error
Get() *gorm.DB
} }

View File

@ -20,7 +20,8 @@ type Portal interface {
Storage() StorageService Storage() StorageService
SetIdentity(identity ed25519.PrivateKey) SetIdentity(identity ed25519.PrivateKey)
SetLogger(logger *zap.Logger) SetLogger(logger *zap.Logger)
Database() Database Database() *gorm.DB
DatabaseService() Database
Casbin() *casbin.Enforcer Casbin() *casbin.Enforcer
SetCasbin(e *casbin.Enforcer) SetCasbin(e *casbin.Enforcer)
Accounts() AccountService Accounts() AccountService