pg_project_module_files_repository.go
4.2 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
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 ProjectModuleFilesRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *ProjectModuleFilesRepository) Save(dm *domain.ProjectModuleFiles) (*domain.ProjectModuleFiles, error) {
var (
err error
m = &models.ProjectModuleFiles{}
tx = repository.transactionContext.PgDd
)
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
}
if err = tx.Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *ProjectModuleFilesRepository) Remove(ProjectModuleFiles *domain.ProjectModuleFiles) (*domain.ProjectModuleFiles, error) {
var (
tx = repository.transactionContext.PgTx
ProjectModuleFilesModel = &models.ProjectModuleFiles{Id: ProjectModuleFiles.Identify().(int64)}
)
if _, err := tx.Model(ProjectModuleFilesModel).Where("id = ?", ProjectModuleFiles.Id).Delete(); err != nil {
return ProjectModuleFiles, err
}
return ProjectModuleFiles, nil
}
func (repository *ProjectModuleFilesRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProjectModuleFiles, error) {
tx := repository.transactionContext.PgDd
ProjectModuleFilesModel := new(models.ProjectModuleFiles)
query := NewQuery(tx.Model(ProjectModuleFilesModel), queryOptions)
query.SetWhere("id = ?", "id")
query.SetWhere("project_module_id = ?", "projectModuleId")
query.SetWhere("project_module_version_id = ?", "projectModuleVersionId")
query.SetWhere("parent_id = ?", "parentId")
query.SetWhere("file_key = ?", "fileKey")
query.SetWhere("path = ?", "path")
query.SetOrder("tag", "orderByTag")
query.SetOrder("id", "sortById")
if err := query.First(); err != nil {
return nil, fmt.Errorf("query row not found")
}
if ProjectModuleFilesModel.Id == 0 {
return nil, fmt.Errorf("query row not found")
}
return repository.transformPgModelToDomainModel(ProjectModuleFilesModel)
}
func (repository *ProjectModuleFilesRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProjectModuleFiles, error) {
tx := repository.transactionContext.PgDd
var ProjectModuleFilesModels []*models.ProjectModuleFiles
ProjectModuleFiless := make([]*domain.ProjectModuleFiles, 0)
query := NewQuery(tx.Model(&ProjectModuleFilesModels), queryOptions).
SetWhere("project_module_id=?", "projectModuleId").
SetWhere("project_module_version_id=?", "projectModuleVersionId").
SetOrder("create_time", "sortByCreateTime").
SetOrder("update_time", "sortByUpdateTime").
SetOrder("parent_id", "sortByParentId").
SetOrder("file_type", "sortByFileType").
SetOrder("file_name", "sortByFileName").
//SetOrder("tag", "sortByTag").
SetOrder("id", "sortById")
query.SetWhere("parent_id = ?", "parentId")
query.SetWhere("file_name = ?", "fileName")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, ProjectModuleFiless, err
}
for _, ProjectModuleFilesModel := range ProjectModuleFilesModels {
if ProjectModuleFiles, err := repository.transformPgModelToDomainModel(ProjectModuleFilesModel); err != nil {
return 0, ProjectModuleFiless, err
} else {
ProjectModuleFiless = append(ProjectModuleFiless, ProjectModuleFiles)
}
}
return int64(query.AffectRow), ProjectModuleFiless, nil
}
func (repository *ProjectModuleFilesRepository) transformPgModelToDomainModel(ProjectModuleFilesModel *models.ProjectModuleFiles) (*domain.ProjectModuleFiles, error) {
m := &domain.ProjectModuleFiles{}
err := common.GobModelTransform(m, ProjectModuleFilesModel)
return m, err
}
func NewProjectModuleFilesRepository(transactionContext *transaction.TransactionContext) (*ProjectModuleFilesRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &ProjectModuleFilesRepository{transactionContext: transactionContext}, nil
}