pg_admin_permission.go
2.3 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
package repository
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type AdminPermissionRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.AdminPermissionRepository = (*AdminPermissionRepository)(nil)
)
func NewAdminPermissionRepository(transactionContext *transaction.TransactionContext) (*AdminPermissionRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &AdminPermissionRepository{transactionContext: transactionContext}, nil
}
func (reponsitory AdminPermissionRepository) transformPgModelToDomainModel(permissionModel *models.AdminPermission) (domain.AdminPermission, error) {
result := domain.AdminPermission{
Id: permissionModel.Id,
ParentId: permissionModel.ParentId,
Name: permissionModel.Name,
Icon: permissionModel.Icon,
Code: permissionModel.Code,
}
return result, nil
}
func (reponsitory AdminPermissionRepository) Find(queryOptions domain.PermissionFindOption) ([]domain.AdminPermission, error) {
db := reponsitory.transactionContext.PgDd
permissionModels := make([]models.AdminPermission, 0)
query := db.Model(&permissionModels)
if len(queryOptions.Ids) > 0 {
query = query.WhereIn("id in (?) ", queryOptions.Ids)
}
if err := query.Select(); err != nil {
return nil, err
}
var result []domain.AdminPermission
for i := range permissionModels {
v, _ := reponsitory.transformPgModelToDomainModel(&permissionModels[i])
result = append(result, v)
}
return result, nil
}
func (reponsitory AdminPermissionRepository) FindOne(queryOptions domain.PermissionFindOneOption) (domain.AdminPermission, error) {
db := reponsitory.transactionContext.PgDd
var permissionModels models.AdminPermission
query := db.Model(&permissionModels)
if len(queryOptions.Code) > 0 {
query = query.Where("code=?", queryOptions.Code)
}
if queryOptions.Id > 0 {
query = query.Where("id=?", queryOptions.Id)
}
var result domain.AdminPermission
err := query.First()
if err != nil {
return result, err
}
result, _ = reponsitory.transformPgModelToDomainModel(&permissionModels)
return result, nil
}