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