...
|
...
|
@@ -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 CompanyRepository struct {
|
|
|
*cache.CachedRepository
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
...
|
...
|
@@ -66,6 +69,7 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp |
|
|
return company, err
|
|
|
}
|
|
|
} else {
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&company.CompanyId,
|
...
|
...
|
@@ -85,6 +89,11 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp |
|
|
); err != nil {
|
|
|
return company, err
|
|
|
}
|
|
|
return company, nil
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, company.CacheKeyFunc()); err != nil {
|
|
|
return company, err
|
|
|
}
|
|
|
}
|
|
|
return company, nil
|
|
|
}
|
...
|
...
|
@@ -92,14 +101,21 @@ func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Co |
|
|
tx := repository.transactionContext.PgTx
|
|
|
companyModel := new(models.Company)
|
|
|
companyModel.CompanyId = company.Identify().(int64)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
if _, err := tx.Model(companyModel).WherePK().Delete(); err != nil {
|
|
|
return company, err
|
|
|
}
|
|
|
return company, nil
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, company.CacheKeyFunc()); err != nil {
|
|
|
return company, err
|
|
|
}
|
|
|
return company, nil
|
|
|
}
|
|
|
func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
companyModel := new(models.Company)
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(companyModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("company.company_id = ?", "companyId")
|
|
|
|
...
|
...
|
@@ -113,6 +129,15 @@ func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{} |
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
return companyModel, nil
|
|
|
}
|
|
|
var cacheModel = &domain.Company{}
|
|
|
if _, ok := queryOptions["companyId"]; ok {
|
|
|
cacheModel.CompanyId = queryOptions["companyId"].(int64)
|
|
|
}
|
|
|
if err := repository.QueryCache(cacheModel.CacheKeyFunc, companyModel, queryFunc, cache.WithObjectToExpire(constant.REPOSITORY_CACHE_EXPIRE)); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if companyModel.CompanyId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
...
|
...
|
@@ -145,6 +170,7 @@ func NewCompanyRepository(transactionContext *pgTransaction.TransactionContext) |
|
|
} else {
|
|
|
return &CompanyRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
CachedRepository: cache.NewDefaultCachedRepository(),
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|