正在显示
2 个修改的文件
包含
168 行增加
和
2 行删除
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 ArticleTagRepository struct { | ||
16 | + *cache.CachedRepository | ||
17 | +} | ||
18 | + | ||
19 | +func (repository *ArticleTagRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleTag) (*domain.ArticleTag, error) { | ||
20 | + var ( | ||
21 | + err error | ||
22 | + m = &models.ArticleTag{} | ||
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 *ArticleTagRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleTag) (*domain.ArticleTag, error) { | ||
37 | + var ( | ||
38 | + err error | ||
39 | + m *models.ArticleTag | ||
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 *ArticleTagRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleTag) (*domain.ArticleTag, error) { | ||
56 | + var ( | ||
57 | + err error | ||
58 | + m *models.ArticleTag | ||
59 | + tx = transaction.DB() | ||
60 | + ) | ||
61 | + if m, err = repository.DomainModelToModel(dm); err != nil { | ||
62 | + return nil, err | ||
63 | + } | ||
64 | + oldVersion := dm.Version | ||
65 | + m.Version += 1 | ||
66 | + queryFunc := func() (interface{}, error) { | ||
67 | + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m) | ||
68 | + if tx.RowsAffected == 0 { | ||
69 | + return nil, domain.ErrUpdateFail | ||
70 | + } | ||
71 | + return nil, tx.Error | ||
72 | + } | ||
73 | + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { | ||
74 | + return nil, err | ||
75 | + } | ||
76 | + return repository.ModelToDomainModel(m) | ||
77 | +} | ||
78 | + | ||
79 | +func (repository *ArticleTagRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleTag) (*domain.ArticleTag, error) { | ||
80 | + var ( | ||
81 | + tx = conn.DB() | ||
82 | + m = &models.ArticleTag{Id: dm.Id} | ||
83 | + ) | ||
84 | + queryFunc := func() (interface{}, error) { | ||
85 | + tx = tx.Where("id = ?", m.Id).Delete(m) | ||
86 | + return m, tx.Error | ||
87 | + } | ||
88 | + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { | ||
89 | + return dm, err | ||
90 | + } | ||
91 | + return repository.ModelToDomainModel(m) | ||
92 | +} | ||
93 | + | ||
94 | +func (repository *ArticleTagRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleTag, error) { | ||
95 | + var ( | ||
96 | + err error | ||
97 | + tx = conn.DB() | ||
98 | + m = new(models.ArticleTag) | ||
99 | + ) | ||
100 | + queryFunc := func() (interface{}, error) { | ||
101 | + tx = tx.Model(m).Where("id = ?", id).First(m) | ||
102 | + if errors.Is(tx.Error, gorm.ErrRecordNotFound) { | ||
103 | + return nil, domain.ErrNotFound | ||
104 | + } | ||
105 | + return m, tx.Error | ||
106 | + } | ||
107 | + cacheModel := new(models.ArticleTag) | ||
108 | + cacheModel.Id = id | ||
109 | + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil { | ||
110 | + return nil, err | ||
111 | + } | ||
112 | + return repository.ModelToDomainModel(m) | ||
113 | +} | ||
114 | + | ||
115 | +func (repository *ArticleTagRepository) Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*domain.ArticleTag, error) { | ||
116 | + var ( | ||
117 | + tx = conn.DB() | ||
118 | + ms []*models.ArticleTag | ||
119 | + dms = make([]*domain.ArticleTag, 0) | ||
120 | + total int64 | ||
121 | + ) | ||
122 | + queryFunc := func() (interface{}, error) { | ||
123 | + tx = tx.Model(&ms). | ||
124 | + Where("company_id=?", companyId). | ||
125 | + Order("id desc") | ||
126 | + | ||
127 | + if v, ok := queryOptions["name"]; ok { | ||
128 | + tx.Where("id like ?", v) | ||
129 | + } | ||
130 | + if v, ok := queryOptions["group"]; ok { | ||
131 | + tx.Where("group like ?", v) | ||
132 | + } | ||
133 | + | ||
134 | + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { | ||
135 | + return dms, tx.Error | ||
136 | + } | ||
137 | + return dms, nil | ||
138 | + } | ||
139 | + | ||
140 | + if _, err := repository.Query(queryFunc); err != nil { | ||
141 | + return 0, nil, err | ||
142 | + } | ||
143 | + | ||
144 | + for _, item := range ms { | ||
145 | + if dm, err := repository.ModelToDomainModel(item); err != nil { | ||
146 | + return 0, dms, err | ||
147 | + } else { | ||
148 | + dms = append(dms, dm) | ||
149 | + } | ||
150 | + } | ||
151 | + return total, dms, nil | ||
152 | +} | ||
153 | + | ||
154 | +func (repository *ArticleTagRepository) ModelToDomainModel(from *models.ArticleTag) (*domain.ArticleTag, error) { | ||
155 | + to := &domain.ArticleTag{} | ||
156 | + err := copier.Copy(to, from) | ||
157 | + return to, err | ||
158 | +} | ||
159 | + | ||
160 | +func (repository *ArticleTagRepository) DomainModelToModel(from *domain.ArticleTag) (*models.ArticleTag, error) { | ||
161 | + to := &models.ArticleTag{} | ||
162 | + err := copier.Copy(to, from) | ||
163 | + return to, err | ||
164 | +} | ||
165 | + | ||
166 | +func NewArticleTagRepository(cache *cache.CachedRepository) domain.ArticleTagRepository { | ||
167 | + return &ArticleTagRepository{CachedRepository: cache} | ||
168 | +} |
-
请 注册 或 登录 后发表评论