pg_casbin_rule_repository.go
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package repository
import (
"fmt"
"github.com/tiptok/gocomm/common"
. "github.com/tiptok/gocomm/pkg/orm/pgx"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
)
type CasbinRuleRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *CasbinRuleRepository) Save(dm *domain.CasbinRule) (*domain.CasbinRule, error) {
var (
err error
m = &models.CasbinRule{}
tx = repository.transactionContext.PgTx
)
if err = common.GobModelTransform(m, dm); err != nil {
return nil, err
}
if dm.Identify() == nil {
if err = tx.Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if err = tx.Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *CasbinRuleRepository) Remove(CasbinRule *domain.CasbinRule) (*domain.CasbinRule, error) {
var (
tx = repository.transactionContext.PgTx
CasbinRuleModel = &models.CasbinRule{Id: CasbinRule.Identify().(int64)}
)
if _, err := tx.Model(CasbinRuleModel).Where("id = ?", CasbinRule.Id).Delete(); err != nil {
return CasbinRule, err
}
return CasbinRule, nil
}
func (repository *CasbinRuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.CasbinRule, error) {
tx := repository.transactionContext.PgDd
CasbinRuleModel := new(models.CasbinRule)
query := NewQuery(tx.Model(CasbinRuleModel), queryOptions)
query.SetWhere("id = ?", "id")
if err := query.First(); err != nil {
return nil, fmt.Errorf("query row not found")
}
if CasbinRuleModel.Id == 0 {
return nil, fmt.Errorf("query row not found")
}
return repository.transformPgModelToDomainModel(CasbinRuleModel)
}
func (repository *CasbinRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CasbinRule, error) {
tx := repository.transactionContext.PgDd
var CasbinRuleModels []*models.CasbinRule
CasbinRules := make([]*domain.CasbinRule, 0)
query := NewQuery(tx.Model(&CasbinRuleModels), queryOptions).
SetOrder("create_time", "sortByCreateTime").
SetOrder("update_time", "sortByUpdateTime")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, CasbinRules, err
}
for _, CasbinRuleModel := range CasbinRuleModels {
if CasbinRule, err := repository.transformPgModelToDomainModel(CasbinRuleModel); err != nil {
return 0, CasbinRules, err
} else {
CasbinRules = append(CasbinRules, CasbinRule)
}
}
return int64(query.AffectRow), CasbinRules, nil
}
func (repository *CasbinRuleRepository) transformPgModelToDomainModel(CasbinRuleModel *models.CasbinRule) (*domain.CasbinRule, error) {
m := &domain.CasbinRule{}
err := common.GobModelTransform(m, CasbinRuleModel)
return m, err
}
func NewCasbinRuleRepository(transactionContext *transaction.TransactionContext) (*CasbinRuleRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &CasbinRuleRepository{transactionContext: transactionContext}, nil
}