作者 郑周

1 删除无用Comment结构

@@ -29,7 +29,6 @@ type ServiceContext struct { @@ -29,7 +29,6 @@ type ServiceContext struct {
29 ArticleAndTagRepository domain.ArticleAndTagRepository 29 ArticleAndTagRepository domain.ArticleAndTagRepository
30 30
31 CompanyRepository domain.CompanyRepository 31 CompanyRepository domain.CompanyRepository
32 - CommentRepository domain.CommentRepository // 待移除  
33 DepartmentRepository domain.DepartmentRepository 32 DepartmentRepository domain.DepartmentRepository
34 MessageBusinessRepository domain.MessageBusinessRepository 33 MessageBusinessRepository domain.MessageBusinessRepository
35 MessageSystemRepository domain.MessageSystemRepository 34 MessageSystemRepository domain.MessageSystemRepository
@@ -60,7 +59,6 @@ func NewServiceContext(c config.Config) *ServiceContext { @@ -60,7 +59,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
60 ApiAuthService: apiAuth, 59 ApiAuthService: apiAuth,
61 LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle, 60 LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
62 61
63 - CommentRepository: repository.NewCommentRepository(cache.NewCachedRepository(mlCache)),  
64 ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)), 62 ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
65 ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)), 63 ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)),
66 ArticleDraftRepository: repository.NewArticleDraftRepository(cache.NewCachedRepository(mlCache)), 64 ArticleDraftRepository: repository.NewArticleDraftRepository(cache.NewCachedRepository(mlCache)),
1 -package models  
2 -  
3 -import (  
4 - "fmt"  
5 - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"  
6 - "gorm.io/gorm"  
7 - "gorm.io/plugin/soft_delete"  
8 - "time"  
9 -)  
10 -  
11 -type Comment struct {  
12 - Id int64 // 唯一标识  
13 -  
14 - CreatedAt int64 `json:",omitempty"`  
15 - UpdatedAt int64 `json:",omitempty"`  
16 - DeletedAt int64 `json:",omitempty"`  
17 - Version int `json:",omitempty"`  
18 - IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`  
19 -}  
20 -  
21 -func (m *Comment) TableName() string {  
22 - return "t_comment"  
23 -}  
24 -  
25 -func (m *Comment) BeforeCreate(tx *gorm.DB) (err error) {  
26 - m.CreatedAt = time.Now().Unix()  
27 - m.UpdatedAt = time.Now().Unix()  
28 - return  
29 -}  
30 -  
31 -func (m *Comment) BeforeUpdate(tx *gorm.DB) (err error) {  
32 - m.UpdatedAt = time.Now().Unix()  
33 - return  
34 -}  
35 -  
36 -func (m *Comment) CacheKeyFunc() string {  
37 - if m.Id == 0 {  
38 - return ""  
39 - }  
40 - return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)  
41 -}  
42 -  
43 -func (m *Comment) CacheKeyFuncByObject(obj interface{}) string {  
44 - if v, ok := obj.(*Comment); ok {  
45 - return v.CacheKeyFunc()  
46 - }  
47 - return ""  
48 -}  
49 -  
50 -func (m *Comment) CachePrimaryKeyFunc() string {  
51 - if len("") == 0 {  
52 - return ""  
53 - }  
54 - return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")  
55 -}  
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 -}  
1 -package domain  
2 -  
3 -import (  
4 - "context"  
5 - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"  
6 -)  
7 -  
8 -type Comment struct {  
9 - Id int64 // 唯一标识  
10 -  
11 - CreatedAt int64 `json:",omitempty"`  
12 - UpdatedAt int64 `json:",omitempty"`  
13 - DeletedAt int64 `json:",omitempty"`  
14 - Version int `json:",omitempty"`  
15 -}  
16 -  
17 -type CommentRepository interface {  
18 - Insert(ctx context.Context, conn transaction.Conn, dm *Comment) (*Comment, error)  
19 - Update(ctx context.Context, conn transaction.Conn, dm *Comment) (*Comment, error)  
20 - UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Comment) (*Comment, error)  
21 - Delete(ctx context.Context, conn transaction.Conn, dm *Comment) (*Comment, error)  
22 - FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Comment, error)  
23 - Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Comment, error)  
24 -}  
25 -  
26 -func (m *Comment) Identify() interface{} {  
27 - if m.Id == 0 {  
28 - return nil  
29 - }  
30 - return m.Id  
31 -}