46 lines
910 B
Go
46 lines
910 B
Go
package user_repo
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/XingfenD/yoresee_doc/internal/model"
|
|
"github.com/XingfenD/yoresee_doc/pkg/key"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UserCreateOperation struct {
|
|
repo *UserRepository
|
|
user *model.User
|
|
tx *gorm.DB
|
|
}
|
|
|
|
func (r *UserRepository) Create(user *model.User) *UserCreateOperation {
|
|
return &UserCreateOperation{
|
|
repo: r,
|
|
user: user,
|
|
}
|
|
}
|
|
|
|
func (op *UserCreateOperation) WithTx(tx *gorm.DB) *UserCreateOperation {
|
|
op.tx = tx
|
|
return op
|
|
}
|
|
|
|
func (op *UserCreateOperation) Exec() error {
|
|
if op.tx != nil {
|
|
if err := op.tx.Create(op.user).Error; err != nil {
|
|
return err
|
|
}
|
|
return op.clearQueryCache()
|
|
}
|
|
if err := op.repo.db.Create(op.user).Error; err != nil {
|
|
return err
|
|
}
|
|
return op.clearQueryCache()
|
|
}
|
|
|
|
func (op *UserCreateOperation) clearQueryCache() error {
|
|
_, _ = op.repo.redis.Incr(context.Background(), key.KeyUserQueryVersion()).Result()
|
|
return nil
|
|
}
|