31 lines
656 B
Go
31 lines
656 B
Go
package persistence
|
|
|
|
import (
|
|
domain "RenewCMS/internal/domain/transaction"
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var _ domain.Transactor = &Transactor{}
|
|
|
|
type Transactor struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewTransactor(db *gorm.DB) *Transactor {
|
|
return &Transactor{db}
|
|
}
|
|
|
|
func (t *Transactor) Run(ctx context.Context, work func(domain.Repositories) error) error {
|
|
return t.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
return work(domain.Repositories{
|
|
Articles: NewArticleRepository(tx),
|
|
Sites: NewSiteRepository(tx),
|
|
Authors: NewAuthorRepository(tx),
|
|
Entries: NewEntryRepository(tx),
|
|
Images: NewImageRepository(tx),
|
|
})
|
|
})
|
|
}
|