pg_project_module_version_repository.go
4.4 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
package repository
import (
"fmt"
//"github.com/tal-tech/go-zero/core/stringx"
"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"
//"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
//"strings"
)
//var (
// fieldNames = builderx.FieldNames(&models.ProjectModuleVersion{})
// rows = strings.Join(fieldNames, ",")
// rowsExpectAutoSet = strings.Join(stringx.Remove(fieldNames, "id","create_time", "update_time"), ",")
// rowsWithPlaceHolder = strings.Join(stringx.Remove(fieldNames, "id","project_module_id","create_time", "update_time"), "=?,") + "=?"
//)
type ProjectModuleVersionRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *ProjectModuleVersionRepository) Save(dm *domain.ProjectModuleVersion) (*domain.ProjectModuleVersion, error) {
var (
err error
m = &models.ProjectModuleVersion{}
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
}
if _, err = tx.Exec(`update project_module_version set version=?,description=?,status=?,update_time=? where id = ?`, m.Version, m.Description, m.Status, m.UpdateTime, m.Id); err != nil {
return nil, err
}
return dm, nil
}
func (repository *ProjectModuleVersionRepository) Remove(ProjectModuleVersion *domain.ProjectModuleVersion) (*domain.ProjectModuleVersion, error) {
var (
tx = repository.transactionContext.PgTx
ProjectModuleVersionModel = &models.ProjectModuleVersion{Id: ProjectModuleVersion.Identify().(int64)}
)
if _, err := tx.Model(ProjectModuleVersionModel).Where("id = ?", ProjectModuleVersion.Id).Delete(); err != nil {
return ProjectModuleVersion, err
}
return ProjectModuleVersion, nil
}
func (repository *ProjectModuleVersionRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProjectModuleVersion, error) {
tx := repository.transactionContext.PgDd
ProjectModuleVersionModel := new(models.ProjectModuleVersion)
query := NewQuery(tx.Model(ProjectModuleVersionModel), queryOptions)
query.SetWhere("id = ?", "id")
query.SetWhere("project_module_id = ?", "projectModuleId")
query.SetWhere("version = ?", "version")
query.SetWhere("status = ?", "status")
if err := query.First(); err != nil {
return nil, fmt.Errorf("query row not found")
}
if ProjectModuleVersionModel.Id == 0 {
return nil, fmt.Errorf("query row not found")
}
return repository.transformPgModelToDomainModel(ProjectModuleVersionModel)
}
func (repository *ProjectModuleVersionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProjectModuleVersion, error) {
tx := repository.transactionContext.PgDd
var ProjectModuleVersionModels []*models.ProjectModuleVersion
ProjectModuleVersions := make([]*domain.ProjectModuleVersion, 0)
query := NewQuery(tx.Model(&ProjectModuleVersionModels), queryOptions).
SetWhere("status=?", "status").
SetWhere("project_module_id=?", "projectModuleId").
SetOrder("create_time", "sortByCreateTime").
SetOrder("update_time", "sortByUpdateTime")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, ProjectModuleVersions, err
}
for _, ProjectModuleVersionModel := range ProjectModuleVersionModels {
if ProjectModuleVersion, err := repository.transformPgModelToDomainModel(ProjectModuleVersionModel); err != nil {
return 0, ProjectModuleVersions, err
} else {
ProjectModuleVersions = append(ProjectModuleVersions, ProjectModuleVersion)
}
}
return int64(query.AffectRow), ProjectModuleVersions, nil
}
func (repository *ProjectModuleVersionRepository) transformPgModelToDomainModel(ProjectModuleVersionModel *models.ProjectModuleVersion) (*domain.ProjectModuleVersion, error) {
m := &domain.ProjectModuleVersion{}
err := common.GobModelTransform(m, ProjectModuleVersionModel)
return m, err
}
func NewProjectModuleVersionRepository(transactionContext *transaction.TransactionContext) (*ProjectModuleVersionRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &ProjectModuleVersionRepository{transactionContext: transactionContext}, nil
}