company_repository.go
5.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package repository
import (
"context"
"fmt"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type CompanyRepository struct {
*cache.CachedRepository
}
func (repository *CompanyRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Company) (*domain.Company, error) {
var (
err error
m = &models.Company{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *CompanyRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Company) (*domain.Company, error) {
var (
err error
m *models.Company
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *CompanyRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Company) (*domain.Company, error) {
var (
err error
m *models.Company
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *CompanyRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Company) (*domain.Company, error) {
var (
tx = conn.DB()
m = &models.Company{Id: dm.Identify().(int64)}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *CompanyRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Company, error) {
var (
err error
tx = conn.DB()
m = new(models.Company)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.Company)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *CompanyRepository) FindOneByCode(ctx context.Context, conn transaction.Conn, code string) (*domain.Company, error) {
var (
err error
tx = conn.DB()
m = new(models.Company)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("code = ?", code).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
if _, err = repository.Query(queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *CompanyRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Company, error) {
var (
tx = conn.DB()
ms []*models.Company
dms = make([]*domain.Company, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["ids"]; ok {
tx.Where("id in (?)", v)
}
if v, ok := queryOptions["excludeIds"]; ok {
tx.Where("id not in (?)", v)
}
if v, ok := queryOptions["likeCodeOrName"]; ok {
tx.Where(fmt.Sprintf("code like '%%%v%%' or name like '%%%v%%'", v, v))
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *CompanyRepository) ModelToDomainModel(from *models.Company) (*domain.Company, error) {
to := &domain.Company{}
err := copier.Copy(to, from)
return to, err
}
func (repository *CompanyRepository) DomainModelToModel(from *domain.Company) (*models.Company, error) {
to := &models.Company{}
err := copier.Copy(to, from)
return to, err
}
func NewCompanyRepository(cache *cache.CachedRepository) domain.CompanyRepository {
return &CompanyRepository{CachedRepository: cache}
}