...
|
...
|
@@ -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,23 +69,29 @@ func (repository *CompanyRepository) Save(company *domain.Company) (*domain.Comp |
|
|
return company, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&company.CompanyId,
|
|
|
&company.CompanyConfig,
|
|
|
&company.CompanyInfo,
|
|
|
&company.Status,
|
|
|
&company.CreatedAt,
|
|
|
&company.UpdatedAt,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE users.company SET %s WHERE company_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
company.CompanyConfig,
|
|
|
company.CompanyInfo,
|
|
|
company.Status,
|
|
|
company.CreatedAt,
|
|
|
company.UpdatedAt,
|
|
|
company.Identify(),
|
|
|
); err != nil {
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&company.CompanyId,
|
|
|
&company.CompanyConfig,
|
|
|
&company.CompanyInfo,
|
|
|
&company.Status,
|
|
|
&company.CreatedAt,
|
|
|
&company.UpdatedAt,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE users.company SET %s WHERE company_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
company.CompanyConfig,
|
|
|
company.CompanyInfo,
|
|
|
company.Status,
|
|
|
company.CreatedAt,
|
|
|
company.UpdatedAt,
|
|
|
company.Identify(),
|
|
|
); err != nil {
|
|
|
return company, err
|
|
|
}
|
|
|
return company, nil
|
|
|
}
|
|
|
if _, err := repository.Query(queryFunc, company.CacheKeyFunc()); err != nil {
|
|
|
return company, err
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -92,7 +101,13 @@ func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Co |
|
|
tx := repository.transactionContext.PgTx
|
|
|
companyModel := new(models.Company)
|
|
|
companyModel.CompanyId = company.Identify().(int64)
|
|
|
if _, err := tx.Model(companyModel).WherePK().Delete(); err != nil {
|
|
|
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
|
...
|
...
|
@@ -100,18 +115,28 @@ func (repository *CompanyRepository) Remove(company *domain.Company) (*domain.Co |
|
|
func (repository *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
companyModel := new(models.Company)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(companyModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("company.company_id = ?", "companyId")
|
|
|
queryFunc := func() (interface{}, error) {
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(companyModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("company.company_id = ?", "companyId")
|
|
|
|
|
|
if v, ok := queryOptions["companyName"]; ok {
|
|
|
query.Where(fmt.Sprintf(`company_info @>'{"companyName":"%v"}'`, v))
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, domain.ErrorNotFound
|
|
|
} else {
|
|
|
return nil, err
|
|
|
if v, ok := queryOptions["companyName"]; ok {
|
|
|
query.Where(fmt.Sprintf(`company_info @>'{"companyName":"%v"}'`, v))
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, domain.ErrorNotFound
|
|
|
} else {
|
|
|
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
|
...
|
...
|
@@ -145,6 +170,7 @@ func NewCompanyRepository(transactionContext *pgTransaction.TransactionContext) |
|
|
} else {
|
|
|
return &CompanyRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
CachedRepository: cache.NewDefaultCachedRepository(),
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|