...
|
...
|
@@ -3,6 +3,8 @@ package repository |
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
"github.com/linmadan/egglib-go/persistent/cache"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
...
|
...
|
@@ -13,6 +15,7 @@ import ( |
|
|
)
|
|
|
|
|
|
type UserBaseRepository struct {
|
|
|
*cache.CachedRepository
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
...
|
...
|
@@ -80,6 +83,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U |
|
|
return userBase, err
|
|
|
}
|
|
|
} else {
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&userBase.UserBaseId,
|
...
|
...
|
@@ -105,6 +109,11 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U |
|
|
); err != nil {
|
|
|
return userBase, err
|
|
|
}
|
|
|
return userBase, nil
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil {
|
|
|
return userBase, err
|
|
|
}
|
|
|
}
|
|
|
return userBase, nil
|
|
|
}
|
...
|
...
|
@@ -112,14 +121,21 @@ func (repository *UserBaseRepository) Remove(userBase *domain.UserBase) (*domain |
|
|
tx := repository.transactionContext.PgTx
|
|
|
userBaseModel := new(models.UserBase)
|
|
|
userBaseModel.UserBaseId = userBase.Identify().(int64)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
if _, err := tx.Model(userBaseModel).WherePK().Delete(); err != nil {
|
|
|
return userBase, err
|
|
|
}
|
|
|
return userBase, nil
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, userBase.CacheKeyFunc()); err != nil {
|
|
|
return userBase, err
|
|
|
}
|
|
|
return userBase, nil
|
|
|
}
|
|
|
func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{}) (*domain.UserBase, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
userBaseModel := new(models.UserBase)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(userBaseModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("account = ?", "account")
|
|
|
query.SetWhereByQueryOption("user_base.user_base_id = ?", "userBaseId")
|
...
|
...
|
@@ -130,6 +146,16 @@ func (repository *UserBaseRepository) FindOne(queryOptions map[string]interface{ |
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
return userBaseModel, nil
|
|
|
}
|
|
|
|
|
|
var cacheModel = &domain.UserBase{}
|
|
|
if _, ok := queryOptions["userBaseId"]; ok {
|
|
|
cacheModel.UserBaseId = queryOptions["userBaseId"].(int64)
|
|
|
}
|
|
|
if err := repository.QueryCache(cacheModel.CacheKeyFunc, userBaseModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if userBaseModel.UserBaseId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
...
|
...
|
@@ -162,6 +188,7 @@ func NewUserBaseRepository(transactionContext *pgTransaction.TransactionContext) |
|
|
} else {
|
|
|
return &UserBaseRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
CachedRepository: cache.NewDefaultCachedRepository(),
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|