1
|
-package repository
|
|
|
2
|
-
|
|
|
3
|
-import (
|
|
|
4
|
- "context"
|
|
|
5
|
- "github.com/jinzhu/copier"
|
|
|
6
|
- "github.com/pkg/errors"
|
|
|
7
|
- "github.com/tiptok/gocomm/pkg/cache"
|
|
|
8
|
- "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
|
|
|
9
|
- "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
|
|
|
10
|
- "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
|
|
|
11
|
- "gorm.io/gorm"
|
|
|
12
|
-)
|
|
|
13
|
-
|
|
|
14
|
-type CommentRepository struct {
|
|
|
15
|
- *cache.CachedRepository
|
|
|
16
|
-}
|
|
|
17
|
-
|
|
|
18
|
-func (repository *CommentRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Comment) (*domain.Comment, error) {
|
|
|
19
|
- var (
|
|
|
20
|
- err error
|
|
|
21
|
- m = &models.Comment{}
|
|
|
22
|
- tx = conn.DB()
|
|
|
23
|
- )
|
|
|
24
|
- if m, err = repository.DomainModelToModel(dm); err != nil {
|
|
|
25
|
- return nil, err
|
|
|
26
|
- }
|
|
|
27
|
- if tx = tx.Model(m).Save(m); tx.Error != nil {
|
|
|
28
|
- return nil, tx.Error
|
|
|
29
|
- }
|
|
|
30
|
- dm.Id = m.Id
|
|
|
31
|
- return repository.ModelToDomainModel(m)
|
|
|
32
|
-
|
|
|
33
|
-}
|
|
|
34
|
-
|
|
|
35
|
-func (repository *CommentRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Comment) (*domain.Comment, error) {
|
|
|
36
|
- var (
|
|
|
37
|
- err error
|
|
|
38
|
- m *models.Comment
|
|
|
39
|
- tx = conn.DB()
|
|
|
40
|
- )
|
|
|
41
|
- if m, err = repository.DomainModelToModel(dm); err != nil {
|
|
|
42
|
- return nil, err
|
|
|
43
|
- }
|
|
|
44
|
- queryFunc := func() (interface{}, error) {
|
|
|
45
|
- tx = tx.Model(m).Updates(m)
|
|
|
46
|
- return nil, tx.Error
|
|
|
47
|
- }
|
|
|
48
|
- if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
|
|
|
49
|
- return nil, err
|
|
|
50
|
- }
|
|
|
51
|
- return repository.ModelToDomainModel(m)
|
|
|
52
|
-}
|
|
|
53
|
-
|
|
|
54
|
-func (repository *CommentRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Comment) (*domain.Comment, error) {
|
|
|
55
|
- var (
|
|
|
56
|
- err error
|
|
|
57
|
- m *models.Comment
|
|
|
58
|
- tx = transaction.DB()
|
|
|
59
|
- )
|
|
|
60
|
- if m, err = repository.DomainModelToModel(dm); err != nil {
|
|
|
61
|
- return nil, err
|
|
|
62
|
- }
|
|
|
63
|
- oldVersion := dm.Version
|
|
|
64
|
- m.Version += 1
|
|
|
65
|
- queryFunc := func() (interface{}, error) {
|
|
|
66
|
- tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
|
|
|
67
|
- if tx.RowsAffected == 0 {
|
|
|
68
|
- return nil, domain.ErrUpdateFail
|
|
|
69
|
- }
|
|
|
70
|
- return nil, tx.Error
|
|
|
71
|
- }
|
|
|
72
|
- if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
|
|
|
73
|
- return nil, err
|
|
|
74
|
- }
|
|
|
75
|
- return repository.ModelToDomainModel(m)
|
|
|
76
|
-}
|
|
|
77
|
-
|
|
|
78
|
-func (repository *CommentRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Comment) (*domain.Comment, error) {
|
|
|
79
|
- var (
|
|
|
80
|
- tx = conn.DB()
|
|
|
81
|
- m = &models.Comment{Id: dm.Identify().(int64)}
|
|
|
82
|
- )
|
|
|
83
|
- queryFunc := func() (interface{}, error) {
|
|
|
84
|
- tx = tx.Where("id = ?", m.Id).Delete(m)
|
|
|
85
|
- return m, tx.Error
|
|
|
86
|
- }
|
|
|
87
|
- if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
|
|
|
88
|
- return dm, err
|
|
|
89
|
- }
|
|
|
90
|
- return repository.ModelToDomainModel(m)
|
|
|
91
|
-}
|
|
|
92
|
-
|
|
|
93
|
-func (repository *CommentRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Comment, error) {
|
|
|
94
|
- var (
|
|
|
95
|
- err error
|
|
|
96
|
- tx = conn.DB()
|
|
|
97
|
- m = new(models.Comment)
|
|
|
98
|
- )
|
|
|
99
|
- queryFunc := func() (interface{}, error) {
|
|
|
100
|
- tx = tx.Model(m).Where("id = ?", id).First(m)
|
|
|
101
|
- if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
|
|
|
102
|
- return nil, domain.ErrNotFound
|
|
|
103
|
- }
|
|
|
104
|
- return m, tx.Error
|
|
|
105
|
- }
|
|
|
106
|
- cacheModel := new(models.Comment)
|
|
|
107
|
- cacheModel.Id = id
|
|
|
108
|
- if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
|
|
|
109
|
- return nil, err
|
|
|
110
|
- }
|
|
|
111
|
- return repository.ModelToDomainModel(m)
|
|
|
112
|
-}
|
|
|
113
|
-
|
|
|
114
|
-func (repository *CommentRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Comment, error) {
|
|
|
115
|
- var (
|
|
|
116
|
- tx = conn.DB()
|
|
|
117
|
- ms []*models.Comment
|
|
|
118
|
- dms = make([]*domain.Comment, 0)
|
|
|
119
|
- total int64
|
|
|
120
|
- )
|
|
|
121
|
- queryFunc := func() (interface{}, error) {
|
|
|
122
|
- tx = tx.Model(&ms).Order("id desc")
|
|
|
123
|
- if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
|
|
|
124
|
- return dms, tx.Error
|
|
|
125
|
- }
|
|
|
126
|
- return dms, nil
|
|
|
127
|
- }
|
|
|
128
|
-
|
|
|
129
|
- if _, err := repository.Query(queryFunc); err != nil {
|
|
|
130
|
- return 0, nil, err
|
|
|
131
|
- }
|
|
|
132
|
-
|
|
|
133
|
- for _, item := range ms {
|
|
|
134
|
- if dm, err := repository.ModelToDomainModel(item); err != nil {
|
|
|
135
|
- return 0, dms, err
|
|
|
136
|
- } else {
|
|
|
137
|
- dms = append(dms, dm)
|
|
|
138
|
- }
|
|
|
139
|
- }
|
|
|
140
|
- return total, dms, nil
|
|
|
141
|
-}
|
|
|
142
|
-
|
|
|
143
|
-func (repository *CommentRepository) ModelToDomainModel(from *models.Comment) (*domain.Comment, error) {
|
|
|
144
|
- to := &domain.Comment{}
|
|
|
145
|
- err := copier.Copy(to, from)
|
|
|
146
|
- return to, err
|
|
|
147
|
-}
|
|
|
148
|
-
|
|
|
149
|
-func (repository *CommentRepository) DomainModelToModel(from *domain.Comment) (*models.Comment, error) {
|
|
|
150
|
- to := &models.Comment{}
|
|
|
151
|
- err := copier.Copy(to, from)
|
|
|
152
|
- return to, err
|
|
|
153
|
-}
|
|
|
154
|
-
|
|
|
155
|
-func NewCommentRepository(cache *cache.CachedRepository) domain.CommentRepository {
|
|
|
156
|
- return &CommentRepository{CachedRepository: cache}
|
|
|
157
|
-} |
|
|