40 lines
957 B
Go
40 lines
957 B
Go
package persistence
|
|
|
|
import (
|
|
domain "RenewCMS/internal/domain/transaction"
|
|
"RenewCMS/internal/infrastructure/persistence/database"
|
|
"context"
|
|
"fmt"
|
|
|
|
"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 {
|
|
err := t.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
articles := NewArticleRepository(tx)
|
|
return work(domain.Repositories{
|
|
Articles: articles,
|
|
Sites: NewSiteRepository(tx),
|
|
Authors: NewAuthorRepository(tx),
|
|
Entries: NewEntryRepository(tx),
|
|
Images: NewImageRepository(tx),
|
|
Localization: NewLocalizationRepository(tx),
|
|
ArticleSEO: articles,
|
|
})
|
|
})
|
|
if database.IsLockContention(t.db, err) {
|
|
return fmt.Errorf("%w: %w", domain.ErrLockContention, err)
|
|
}
|
|
return err
|
|
}
|