作者 tangxvhui

调整文章标签模型

package models
import (
"fmt"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type ArticleAndTag struct {
Id int64 // 唯一标识
CompanyId int64 `json:"companyId"`
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
ArticleId int64 `json:"articleId"`
TagId int64 `json:"tagId"`
}
func (m *ArticleAndTag) TableName() string {
return "article_and_tag"
}
func (m *ArticleAndTag) BeforeCreate(tx *gorm.DB) (err error) {
m.CreatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleAndTag) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleAndTag) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ArticleAndTag) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ArticleAndTag); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ArticleAndTag) 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 ArticleAndTagRepository struct {
*cache.CachedRepository
}
func (repository *ArticleAndTagRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleAndTag) (*domain.ArticleAndTag, error) {
var (
err error
m = &models.ArticleAndTag{}
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 *ArticleAndTagRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleAndTag) (*domain.ArticleAndTag, error) {
var (
err error
m *models.ArticleAndTag
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 *ArticleAndTagRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleAndTag) (*domain.ArticleAndTag, error) {
var (
tx = conn.DB()
m = &models.ArticleAndTag{Id: dm.Id}
)
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 *ArticleAndTagRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleAndTag, error) {
var (
err error
tx = conn.DB()
m = new(models.ArticleAndTag)
)
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.ArticleAndTag)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleAndTagRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleAndTag, error) {
var (
tx = conn.DB()
ms []*models.ArticleAndTag
dms = make([]*domain.ArticleAndTag, 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 *ArticleAndTagRepository) ModelToDomainModel(from *models.ArticleAndTag) (*domain.ArticleAndTag, error) {
to := &domain.ArticleAndTag{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ArticleAndTagRepository) DomainModelToModel(from *domain.ArticleAndTag) (*models.ArticleAndTag, error) {
to := &models.ArticleAndTag{}
err := copier.Copy(to, from)
return to, err
}
func NewArticleAndTagRepository(cache *cache.CachedRepository) domain.ArticleAndTagRepository {
return &ArticleAndTagRepository{CachedRepository: cache}
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
// 保存文章和标签的关系,主要用于分组统计用
type ArticleAndTag struct {
Id int64 `json:"id"`
CompanyId int64 `json:"companyId"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
ArticleId int64 `json:"articleId"`
TagId int64 `json:"tagId"`
}
type ArticleAndTagRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
Update(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleAndTag, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleAndTag, error)
}
... ...