作者 郑周

1 删除无用Comment结构

... ... @@ -29,7 +29,6 @@ type ServiceContext struct {
ArticleAndTagRepository domain.ArticleAndTagRepository
CompanyRepository domain.CompanyRepository
CommentRepository domain.CommentRepository // 待移除
DepartmentRepository domain.DepartmentRepository
MessageBusinessRepository domain.MessageBusinessRepository
MessageSystemRepository domain.MessageSystemRepository
... ... @@ -60,7 +59,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
ApiAuthService: apiAuth,
LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
CommentRepository: repository.NewCommentRepository(cache.NewCachedRepository(mlCache)),
ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)),
ArticleDraftRepository: repository.NewArticleDraftRepository(cache.NewCachedRepository(mlCache)),
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
"time"
)
type Comment struct {
Id int64 // 唯一标识
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
}
func (m *Comment) TableName() string {
return "t_comment"
}
func (m *Comment) BeforeCreate(tx *gorm.DB) (err error) {
m.CreatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
return
}
func (m *Comment) BeforeUpdate(tx *gorm.DB) (err error) {
m.UpdatedAt = time.Now().Unix()
return
}
func (m *Comment) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *Comment) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*Comment); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *Comment) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
package repository
import (
"context"
"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 CommentRepository struct {
*cache.CachedRepository
}
func (repository *CommentRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Comment) (*domain.Comment, error) {
var (
err error
m = &models.Comment{}
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 *CommentRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Comment) (*domain.Comment, error) {
var (
err error
m *models.Comment
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *CommentRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Comment) (*domain.Comment, error) {
var (
err error
m *models.Comment
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 *CommentRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Comment) (*domain.Comment, error) {
var (
tx = conn.DB()
m = &models.Comment{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 *CommentRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Comment, error) {
var (
err error
tx = conn.DB()
m = new(models.Comment)
)
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.Comment)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *CommentRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Comment, error) {
var (
tx = conn.DB()
ms []*models.Comment
dms = make([]*domain.Comment, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
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 *CommentRepository) ModelToDomainModel(from *models.Comment) (*domain.Comment, error) {
to := &domain.Comment{}
err := copier.Copy(to, from)
return to, err
}
func (repository *CommentRepository) DomainModelToModel(from *domain.Comment) (*models.Comment, error) {
to := &models.Comment{}
err := copier.Copy(to, from)
return to, err
}
func NewCommentRepository(cache *cache.CachedRepository) domain.CommentRepository {
return &CommentRepository{CachedRepository: cache}
}
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
type Comment struct {
Id int64 // 唯一标识
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
type CommentRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *Comment) (*Comment, error)
Update(ctx context.Context, conn transaction.Conn, dm *Comment) (*Comment, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Comment) (*Comment, error)
Delete(ctx context.Context, conn transaction.Conn, dm *Comment) (*Comment, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Comment, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Comment, error)
}
func (m *Comment) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}