作者 tangxvhui

调整文章标签模型

  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  8 + "gorm.io/gorm"
  9 +)
  10 +
  11 +type ArticleAndTag struct {
  12 + Id int64 // 唯一标识
  13 + CompanyId int64 `json:"companyId"`
  14 + CreatedAt int64 `json:"createdAt,omitempty"`
  15 + UpdatedAt int64 `json:"updatedAt,omitempty"`
  16 + ArticleId int64 `json:"articleId"`
  17 + TagId int64 `json:"tagId"`
  18 +}
  19 +
  20 +func (m *ArticleAndTag) TableName() string {
  21 + return "article_and_tag"
  22 +}
  23 +
  24 +func (m *ArticleAndTag) BeforeCreate(tx *gorm.DB) (err error) {
  25 + m.CreatedAt = time.Now().Unix()
  26 + m.UpdatedAt = time.Now().Unix()
  27 + return
  28 +}
  29 +
  30 +func (m *ArticleAndTag) BeforeUpdate(tx *gorm.DB) (err error) {
  31 + // m.UpdatedAt = time.Now().Unix()
  32 + return
  33 +}
  34 +
  35 +func (m *ArticleAndTag) CacheKeyFunc() string {
  36 + if m.Id == 0 {
  37 + return ""
  38 + }
  39 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  40 +}
  41 +
  42 +func (m *ArticleAndTag) CacheKeyFuncByObject(obj interface{}) string {
  43 + if v, ok := obj.(*ArticleAndTag); ok {
  44 + return v.CacheKeyFunc()
  45 + }
  46 + return ""
  47 +}
  48 +
  49 +func (m *ArticleAndTag) CachePrimaryKeyFunc() string {
  50 + if len("") == 0 {
  51 + return ""
  52 + }
  53 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  54 +}
  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 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  7 +)
  8 +
  9 +// 保存文章和标签的关系,主要用于分组统计用
  10 +type ArticleAndTag struct {
  11 + Id int64 `json:"id"`
  12 + CompanyId int64 `json:"companyId"`
  13 + CreatedAt int64 `json:"createdAt"`
  14 + UpdatedAt int64 `json:"updatedAt"`
  15 + ArticleId int64 `json:"articleId"`
  16 + TagId int64 `json:"tagId"`
  17 +}
  18 +
  19 +type ArticleAndTagRepository interface {
  20 + Insert(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
  21 + Update(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
  22 + Delete(ctx context.Context, conn transaction.Conn, dm *ArticleAndTag) (*ArticleAndTag, error)
  23 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleAndTag, error)
  24 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleAndTag, error)
  25 +}