pg_role_repository.go
3.8 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/cache"
. "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"
)
var (
cacheRoleIdKey = func(id int64) string {
return fmt.Sprintf("%v:cache:Role:id:%v", "godevp", id)
}
)
type RoleRepository struct {
*cache.CachedRepository
transactionContext *transaction.TransactionContext
}
func (repository *RoleRepository) Save(dm *domain.Role) (*domain.Role, error) {
var (
err error
m = &models.Role{}
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
}
dm.Id = m.Id
return dm, nil
}
queryFunc := func() (interface{}, error) {
return tx.Exec(`update role set role_name=?,parent_id=?,update_time=now() where id = ?`, m.RoleName, m.ParentId, m.Id)
}
if _, err = repository.Query(queryFunc, cacheRoleIdKey(dm.Id)); err != nil {
return nil, err
}
return dm, nil
}
func (repository *RoleRepository) Remove(Role *domain.Role) (*domain.Role, error) {
var (
tx = repository.transactionContext.PgTx
RoleModel = &models.Role{Id: Role.Identify().(int64)}
)
queryFunc := func() (interface{}, error) {
return tx.Model(RoleModel).Where("id = ?", Role.Id).Delete()
}
if _, err := repository.Query(queryFunc, cacheRoleIdKey(Role.Id)); err != nil {
return Role, err
}
return Role, nil
}
func (repository *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domain.Role, error) {
tx := repository.transactionContext.PgDd
RoleModel := new(models.Role)
queryFunc := func() (interface{}, error) {
query := NewQuery(tx.Model(RoleModel), queryOptions)
query.SetWhere("id = ?", "id")
if err := query.First(); err != nil {
return nil, fmt.Errorf("query row not found")
}
return RoleModel, nil
}
var options []cache.QueryOption
if _, ok := queryOptions["id"]; !ok {
options = append(options, cache.WithNoCacheFlag())
}
if err := repository.QueryCache(cacheRoleIdKey(queryOptions["id"].(int64)), RoleModel, queryFunc, options...); err != nil {
return nil, err
}
if RoleModel.Id == 0 {
return nil, fmt.Errorf("query row not found")
}
return repository.transformPgModelToDomainModel(RoleModel)
}
func (repository *RoleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Role, error) {
tx := repository.transactionContext.PgTx
var RoleModels []*models.Role
Roles := make([]*domain.Role, 0)
query := NewQuery(tx.Model(&RoleModels), queryOptions).
SetWhere("parent_id=?", "parentId").
SetOrder("create_time", "sortByCreateTime").
SetOrder("update_time", "sortByUpdateTime").
SetOrder("id", "orderById")
var err error
if inRoleIds, ok := queryOptions["inRoleIds"]; ok {
query.Where("id in (?)", pg.In(inRoleIds))
}
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, Roles, err
}
for _, RoleModel := range RoleModels {
if Role, err := repository.transformPgModelToDomainModel(RoleModel); err != nil {
return 0, Roles, err
} else {
Roles = append(Roles, Role)
}
}
return int64(query.AffectRow), Roles, nil
}
func (repository *RoleRepository) transformPgModelToDomainModel(RoleModel *models.Role) (*domain.Role, error) {
m := &domain.Role{}
err := common.GobModelTransform(m, RoleModel)
return m, err
}
func NewRoleRepository(transactionContext *transaction.TransactionContext) (*RoleRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &RoleRepository{transactionContext: transactionContext, CachedRepository: cache.NewDefaultCachedRepository()}, nil
}