作者 tangxvhui

更新模型

  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  7 + "gorm.io/gorm"
  8 +)
  9 +
  10 +type Article struct {
  11 + Id int64 // 唯一标识
  12 + CompanyId int64 `json:"companyId"`
  13 + CreatedAt int64 `json:"createdAt,omitempty"`
  14 + UpdatedAt int64 `json:"updatedAt,omitempty"`
  15 + DeletedAt int64 `json:"deletedAt,omitempty"`
  16 + Version int `json:"version,omitempty"`
  17 + AuthorId int64 `json:"authorId"` // 发布人
  18 + Author domain.User `json:"author"` // 发布人
  19 + Title string `json:"title"` // 文章标题
  20 + Images []domain.Image `json:"images"` // 图片
  21 + WhoRead []int64 `json:"whoRead"` // 谁可以看
  22 + WhoReview []int64 `json:"whoReview"` // 评论人
  23 + Location domain.Location `json:"location"` // 坐标
  24 + TargetUser int `json:"targetUser"` // 分发方式 0 分发给所有人 1 分发给指定的人
  25 + CountLove int `json:"countLove"` // 点赞数量
  26 + CountComment int `json:"countComment"` // 评论数量
  27 + Tags []int `json:"tags"` // 标签
  28 + Show int `json:"showState"` // 评论的展示状态(0显示、1不显示)
  29 +}
  30 +
  31 +func (m *Article) TableName() string {
  32 + return "article"
  33 +}
  34 +
  35 +func (m *Article) BeforeCreate(tx *gorm.DB) (err error) {
  36 + // m.CreatedAt = time.Now().Unix()
  37 + // m.UpdatedAt = time.Now().Unix()
  38 + return
  39 +}
  40 +
  41 +func (m *Article) BeforeUpdate(tx *gorm.DB) (err error) {
  42 + // m.UpdatedAt = time.Now().Unix()
  43 + return
  44 +}
  45 +
  46 +func (m *Article) CacheKeyFunc() string {
  47 + if m.Id == 0 {
  48 + return ""
  49 + }
  50 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  51 +}
  52 +
  53 +func (m *Article) CacheKeyFuncByObject(obj interface{}) string {
  54 + if v, ok := obj.(*Article); ok {
  55 + return v.CacheKeyFunc()
  56 + }
  57 + return ""
  58 +}
  59 +
  60 +func (m *Article) CachePrimaryKeyFunc() string {
  61 + if len("") == 0 {
  62 + return ""
  63 + }
  64 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  65 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  7 + "gorm.io/gorm"
  8 +)
  9 +
  10 +type ArticleBackup struct {
  11 + Id int64 // 唯一标识
  12 + Version int
  13 +}
  14 +
  15 +func (m *ArticleBackup) TableName() string {
  16 + return "article_backup"
  17 +}
  18 +
  19 +func (m *ArticleBackup) BeforeCreate(tx *gorm.DB) (err error) {
  20 + // m.CreatedAt = time.Now().Unix()
  21 + // m.UpdatedAt = time.Now().Unix()
  22 + return
  23 +}
  24 +
  25 +func (m *ArticleBackup) BeforeUpdate(tx *gorm.DB) (err error) {
  26 + // m.UpdatedAt = time.Now().Unix()
  27 + return
  28 +}
  29 +
  30 +func (m *ArticleBackup) CacheKeyFunc() string {
  31 + if m.Id == 0 {
  32 + return ""
  33 + }
  34 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  35 +}
  36 +
  37 +func (m *ArticleBackup) CacheKeyFuncByObject(obj interface{}) string {
  38 + if v, ok := obj.(*ArticleBackup); ok {
  39 + return v.CacheKeyFunc()
  40 + }
  41 + return ""
  42 +}
  43 +
  44 +func (m *ArticleBackup) CachePrimaryKeyFunc() string {
  45 + if len("") == 0 {
  46 + return ""
  47 + }
  48 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  49 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  7 + "gorm.io/gorm"
  8 +)
  9 +
  10 +type ArticleComment struct {
  11 + Id int64 // 唯一标识
  12 + Version int
  13 +}
  14 +
  15 +func (m *ArticleComment) TableName() string {
  16 + return "article_comment"
  17 +}
  18 +
  19 +func (m *ArticleComment) BeforeCreate(tx *gorm.DB) (err error) {
  20 + // m.CreatedAt = time.Now().Unix()
  21 + // m.UpdatedAt = time.Now().Unix()
  22 + return
  23 +}
  24 +
  25 +func (m *ArticleComment) BeforeUpdate(tx *gorm.DB) (err error) {
  26 + // m.UpdatedAt = time.Now().Unix()
  27 + return
  28 +}
  29 +
  30 +func (m *ArticleComment) CacheKeyFunc() string {
  31 + if m.Id == 0 {
  32 + return ""
  33 + }
  34 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  35 +}
  36 +
  37 +func (m *ArticleComment) CacheKeyFuncByObject(obj interface{}) string {
  38 + if v, ok := obj.(*ArticleComment); ok {
  39 + return v.CacheKeyFunc()
  40 + }
  41 + return ""
  42 +}
  43 +
  44 +func (m *ArticleComment) CachePrimaryKeyFunc() string {
  45 + if len("") == 0 {
  46 + return ""
  47 + }
  48 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  49 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  7 + "gorm.io/gorm"
  8 +)
  9 +
  10 +type ArticleDraft struct {
  11 + Id int64 // 唯一标识
  12 + Version int
  13 +}
  14 +
  15 +func (m *ArticleDraft) TableName() string {
  16 + return "article_draft"
  17 +}
  18 +
  19 +func (m *ArticleDraft) BeforeCreate(tx *gorm.DB) (err error) {
  20 + // m.CreatedAt = time.Now().Unix()
  21 + // m.UpdatedAt = time.Now().Unix()
  22 + return
  23 +}
  24 +
  25 +func (m *ArticleDraft) BeforeUpdate(tx *gorm.DB) (err error) {
  26 + // m.UpdatedAt = time.Now().Unix()
  27 + return
  28 +}
  29 +
  30 +func (m *ArticleDraft) CacheKeyFunc() string {
  31 + if m.Id == 0 {
  32 + return ""
  33 + }
  34 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  35 +}
  36 +
  37 +func (m *ArticleDraft) CacheKeyFuncByObject(obj interface{}) string {
  38 + if v, ok := obj.(*ArticleDraft); ok {
  39 + return v.CacheKeyFunc()
  40 + }
  41 + return ""
  42 +}
  43 +
  44 +func (m *ArticleDraft) CachePrimaryKeyFunc() string {
  45 + if len("") == 0 {
  46 + return ""
  47 + }
  48 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  49 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  7 + "gorm.io/gorm"
  8 +)
  9 +
  10 +type ArticleSection struct {
  11 + Id int64 // 唯一标识
  12 + Version int
  13 +}
  14 +
  15 +func (m *ArticleSection) TableName() string {
  16 + return "article_section"
  17 +}
  18 +
  19 +func (m *ArticleSection) BeforeCreate(tx *gorm.DB) (err error) {
  20 + // m.CreatedAt = time.Now().Unix()
  21 + // m.UpdatedAt = time.Now().Unix()
  22 + return
  23 +}
  24 +
  25 +func (m *ArticleSection) BeforeUpdate(tx *gorm.DB) (err error) {
  26 + // m.UpdatedAt = time.Now().Unix()
  27 + return
  28 +}
  29 +
  30 +func (m *ArticleSection) CacheKeyFunc() string {
  31 + if m.Id == 0 {
  32 + return ""
  33 + }
  34 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  35 +}
  36 +
  37 +func (m *ArticleSection) CacheKeyFuncByObject(obj interface{}) string {
  38 + if v, ok := obj.(*ArticleSection); ok {
  39 + return v.CacheKeyFunc()
  40 + }
  41 + return ""
  42 +}
  43 +
  44 +func (m *ArticleSection) CachePrimaryKeyFunc() string {
  45 + if len("") == 0 {
  46 + return ""
  47 + }
  48 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  49 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  7 + "gorm.io/gorm"
  8 +)
  9 +
  10 +type UserLoveFlag struct {
  11 + Id int64 // 唯一标识
  12 + Version int
  13 +}
  14 +
  15 +func (m *UserLoveFlag) TableName() string {
  16 + return "user_love_flag"
  17 +}
  18 +
  19 +func (m *UserLoveFlag) BeforeCreate(tx *gorm.DB) (err error) {
  20 + // m.CreatedAt = time.Now().Unix()
  21 + // m.UpdatedAt = time.Now().Unix()
  22 + return
  23 +}
  24 +
  25 +func (m *UserLoveFlag) BeforeUpdate(tx *gorm.DB) (err error) {
  26 + // m.UpdatedAt = time.Now().Unix()
  27 + return
  28 +}
  29 +
  30 +func (m *UserLoveFlag) CacheKeyFunc() string {
  31 + if m.Id == 0 {
  32 + return ""
  33 + }
  34 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  35 +}
  36 +
  37 +func (m *UserLoveFlag) CacheKeyFuncByObject(obj interface{}) string {
  38 + if v, ok := obj.(*UserLoveFlag); ok {
  39 + return v.CacheKeyFunc()
  40 + }
  41 + return ""
  42 +}
  43 +
  44 +func (m *UserLoveFlag) CachePrimaryKeyFunc() string {
  45 + if len("") == 0 {
  46 + return ""
  47 + }
  48 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  49 +}
  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 ArticleBackupRepository struct {
  16 + *cache.CachedRepository
  17 +}
  18 +
  19 +func (repository *ArticleBackupRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleBackup) (*domain.ArticleBackup, error) {
  20 + var (
  21 + err error
  22 + m = &models.ArticleBackup{}
  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 *ArticleBackupRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleBackup) (*domain.ArticleBackup, error) {
  37 + var (
  38 + err error
  39 + m *models.ArticleBackup
  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 *ArticleBackupRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleBackup) (*domain.ArticleBackup, error) {
  56 + var (
  57 + err error
  58 + m *models.ArticleBackup
  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 *ArticleBackupRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleBackup) (*domain.ArticleBackup, error) {
  80 + var (
  81 + tx = conn.DB()
  82 + m = &models.ArticleBackup{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 *ArticleBackupRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleBackup, error) {
  95 + var (
  96 + err error
  97 + tx = conn.DB()
  98 + m = new(models.ArticleBackup)
  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.ArticleBackup)
  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 *ArticleBackupRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleBackup, error) {
  116 + var (
  117 + tx = conn.DB()
  118 + ms []*models.ArticleBackup
  119 + dms = make([]*domain.ArticleBackup, 0)
  120 + total int64
  121 + )
  122 + queryFunc := func() (interface{}, error) {
  123 + tx = tx.Model(&ms).Order("id desc")
  124 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  125 + return dms, tx.Error
  126 + }
  127 + return dms, nil
  128 + }
  129 +
  130 + if _, err := repository.Query(queryFunc); err != nil {
  131 + return 0, nil, err
  132 + }
  133 +
  134 + for _, item := range ms {
  135 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  136 + return 0, dms, err
  137 + } else {
  138 + dms = append(dms, dm)
  139 + }
  140 + }
  141 + return total, dms, nil
  142 +}
  143 +
  144 +func (repository *ArticleBackupRepository) ModelToDomainModel(from *models.ArticleBackup) (*domain.ArticleBackup, error) {
  145 + to := &domain.ArticleBackup{}
  146 + err := copier.Copy(to, from)
  147 + return to, err
  148 +}
  149 +
  150 +func (repository *ArticleBackupRepository) DomainModelToModel(from *domain.ArticleBackup) (*models.ArticleBackup, error) {
  151 + to := &models.ArticleBackup{}
  152 + err := copier.Copy(to, from)
  153 + return to, err
  154 +}
  155 +
  156 +func NewArticleBackupRepository(cache *cache.CachedRepository) domain.ArticleBackupRepository {
  157 + return &ArticleBackupRepository{CachedRepository: cache}
  158 +}
  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 ArticleCommentRepository struct {
  16 + *cache.CachedRepository
  17 +}
  18 +
  19 +func (repository *ArticleCommentRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleComment) (*domain.ArticleComment, error) {
  20 + var (
  21 + err error
  22 + m = &models.ArticleComment{}
  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 *ArticleCommentRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleComment) (*domain.ArticleComment, error) {
  37 + var (
  38 + err error
  39 + m *models.ArticleComment
  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 *ArticleCommentRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleComment) (*domain.ArticleComment, error) {
  56 + var (
  57 + err error
  58 + m *models.ArticleComment
  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 *ArticleCommentRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleComment) (*domain.ArticleComment, error) {
  80 + var (
  81 + tx = conn.DB()
  82 + m = &models.ArticleComment{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 *ArticleCommentRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleComment, error) {
  95 + var (
  96 + err error
  97 + tx = conn.DB()
  98 + m = new(models.ArticleComment)
  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.ArticleComment)
  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 *ArticleCommentRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleComment, error) {
  116 + var (
  117 + tx = conn.DB()
  118 + ms []*models.ArticleComment
  119 + dms = make([]*domain.ArticleComment, 0)
  120 + total int64
  121 + )
  122 + queryFunc := func() (interface{}, error) {
  123 + tx = tx.Model(&ms).Order("id desc")
  124 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  125 + return dms, tx.Error
  126 + }
  127 + return dms, nil
  128 + }
  129 +
  130 + if _, err := repository.Query(queryFunc); err != nil {
  131 + return 0, nil, err
  132 + }
  133 +
  134 + for _, item := range ms {
  135 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  136 + return 0, dms, err
  137 + } else {
  138 + dms = append(dms, dm)
  139 + }
  140 + }
  141 + return total, dms, nil
  142 +}
  143 +
  144 +func (repository *ArticleCommentRepository) ModelToDomainModel(from *models.ArticleComment) (*domain.ArticleComment, error) {
  145 + to := &domain.ArticleComment{}
  146 + err := copier.Copy(to, from)
  147 + return to, err
  148 +}
  149 +
  150 +func (repository *ArticleCommentRepository) DomainModelToModel(from *domain.ArticleComment) (*models.ArticleComment, error) {
  151 + to := &models.ArticleComment{}
  152 + err := copier.Copy(to, from)
  153 + return to, err
  154 +}
  155 +
  156 +func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository {
  157 + return &ArticleCommentRepository{CachedRepository: cache}
  158 +}
  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 ArticleDraftRepository struct {
  16 + *cache.CachedRepository
  17 +}
  18 +
  19 +func (repository *ArticleDraftRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleDraft) (*domain.ArticleDraft, error) {
  20 + var (
  21 + err error
  22 + m = &models.ArticleDraft{}
  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 *ArticleDraftRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleDraft) (*domain.ArticleDraft, error) {
  37 + var (
  38 + err error
  39 + m *models.ArticleDraft
  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 *ArticleDraftRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleDraft) (*domain.ArticleDraft, error) {
  56 + var (
  57 + err error
  58 + m *models.ArticleDraft
  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 *ArticleDraftRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleDraft) (*domain.ArticleDraft, error) {
  80 + var (
  81 + tx = conn.DB()
  82 + m = &models.ArticleDraft{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 *ArticleDraftRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleDraft, error) {
  95 + var (
  96 + err error
  97 + tx = conn.DB()
  98 + m = new(models.ArticleDraft)
  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.ArticleDraft)
  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 *ArticleDraftRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleDraft, error) {
  116 + var (
  117 + tx = conn.DB()
  118 + ms []*models.ArticleDraft
  119 + dms = make([]*domain.ArticleDraft, 0)
  120 + total int64
  121 + )
  122 + queryFunc := func() (interface{}, error) {
  123 + tx = tx.Model(&ms).Order("id desc")
  124 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  125 + return dms, tx.Error
  126 + }
  127 + return dms, nil
  128 + }
  129 +
  130 + if _, err := repository.Query(queryFunc); err != nil {
  131 + return 0, nil, err
  132 + }
  133 +
  134 + for _, item := range ms {
  135 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  136 + return 0, dms, err
  137 + } else {
  138 + dms = append(dms, dm)
  139 + }
  140 + }
  141 + return total, dms, nil
  142 +}
  143 +
  144 +func (repository *ArticleDraftRepository) ModelToDomainModel(from *models.ArticleDraft) (*domain.ArticleDraft, error) {
  145 + to := &domain.ArticleDraft{}
  146 + err := copier.Copy(to, from)
  147 + return to, err
  148 +}
  149 +
  150 +func (repository *ArticleDraftRepository) DomainModelToModel(from *domain.ArticleDraft) (*models.ArticleDraft, error) {
  151 + to := &models.ArticleDraft{}
  152 + err := copier.Copy(to, from)
  153 + return to, err
  154 +}
  155 +
  156 +func NewArticleDraftRepository(cache *cache.CachedRepository) domain.ArticleDraftRepository {
  157 + return &ArticleDraftRepository{CachedRepository: cache}
  158 +}
  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 ArticleRepository struct {
  16 + *cache.CachedRepository
  17 +}
  18 +
  19 +func (repository *ArticleRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
  20 + var (
  21 + err error
  22 + m = &models.Article{}
  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 *ArticleRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
  37 + var (
  38 + err error
  39 + m *models.Article
  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 *ArticleRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Article) (*domain.Article, error) {
  56 + var (
  57 + err error
  58 + m *models.Article
  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 *ArticleRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
  80 + var (
  81 + tx = conn.DB()
  82 + m = &models.Article{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 *ArticleRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Article, error) {
  95 + var (
  96 + err error
  97 + tx = conn.DB()
  98 + m = new(models.Article)
  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.Article)
  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 *ArticleRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Article, error) {
  116 + var (
  117 + tx = conn.DB()
  118 + ms []*models.Article
  119 + dms = make([]*domain.Article, 0)
  120 + total int64
  121 + )
  122 + queryFunc := func() (interface{}, error) {
  123 + tx = tx.Model(&ms).Order("id desc")
  124 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  125 + return dms, tx.Error
  126 + }
  127 + return dms, nil
  128 + }
  129 +
  130 + if _, err := repository.Query(queryFunc); err != nil {
  131 + return 0, nil, err
  132 + }
  133 +
  134 + for _, item := range ms {
  135 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  136 + return 0, dms, err
  137 + } else {
  138 + dms = append(dms, dm)
  139 + }
  140 + }
  141 + return total, dms, nil
  142 +}
  143 +
  144 +func (repository *ArticleRepository) ModelToDomainModel(from *models.Article) (*domain.Article, error) {
  145 + to := &domain.Article{}
  146 + err := copier.Copy(to, from)
  147 + return to, err
  148 +}
  149 +
  150 +func (repository *ArticleRepository) DomainModelToModel(from *domain.Article) (*models.Article, error) {
  151 + to := &models.Article{}
  152 + err := copier.Copy(to, from)
  153 + return to, err
  154 +}
  155 +
  156 +func NewArticleRepository(cache *cache.CachedRepository) domain.ArticleRepository {
  157 + return &ArticleRepository{CachedRepository: cache}
  158 +}
  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 ArticleSectionRepository struct {
  16 + *cache.CachedRepository
  17 +}
  18 +
  19 +func (repository *ArticleSectionRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleSection) (*domain.ArticleSection, error) {
  20 + var (
  21 + err error
  22 + m = &models.ArticleSection{}
  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 *ArticleSectionRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleSection) (*domain.ArticleSection, error) {
  37 + var (
  38 + err error
  39 + m *models.ArticleSection
  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 *ArticleSectionRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleSection) (*domain.ArticleSection, error) {
  56 + var (
  57 + err error
  58 + m *models.ArticleSection
  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 *ArticleSectionRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleSection) (*domain.ArticleSection, error) {
  80 + var (
  81 + tx = conn.DB()
  82 + m = &models.ArticleSection{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 *ArticleSectionRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleSection, error) {
  95 + var (
  96 + err error
  97 + tx = conn.DB()
  98 + m = new(models.ArticleSection)
  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.ArticleSection)
  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 *ArticleSectionRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleSection, error) {
  116 + var (
  117 + tx = conn.DB()
  118 + ms []*models.ArticleSection
  119 + dms = make([]*domain.ArticleSection, 0)
  120 + total int64
  121 + )
  122 + queryFunc := func() (interface{}, error) {
  123 + tx = tx.Model(&ms).Order("id desc")
  124 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  125 + return dms, tx.Error
  126 + }
  127 + return dms, nil
  128 + }
  129 +
  130 + if _, err := repository.Query(queryFunc); err != nil {
  131 + return 0, nil, err
  132 + }
  133 +
  134 + for _, item := range ms {
  135 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  136 + return 0, dms, err
  137 + } else {
  138 + dms = append(dms, dm)
  139 + }
  140 + }
  141 + return total, dms, nil
  142 +}
  143 +
  144 +func (repository *ArticleSectionRepository) ModelToDomainModel(from *models.ArticleSection) (*domain.ArticleSection, error) {
  145 + to := &domain.ArticleSection{}
  146 + err := copier.Copy(to, from)
  147 + return to, err
  148 +}
  149 +
  150 +func (repository *ArticleSectionRepository) DomainModelToModel(from *domain.ArticleSection) (*models.ArticleSection, error) {
  151 + to := &models.ArticleSection{}
  152 + err := copier.Copy(to, from)
  153 + return to, err
  154 +}
  155 +
  156 +func NewArticleSectionRepository(cache *cache.CachedRepository) domain.ArticleSectionRepository {
  157 + return &ArticleSectionRepository{CachedRepository: cache}
  158 +}
  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 UserLoveFlagRepository struct {
  16 + *cache.CachedRepository
  17 +}
  18 +
  19 +func (repository *UserLoveFlagRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.UserLoveFlag) (*domain.UserLoveFlag, error) {
  20 + var (
  21 + err error
  22 + m = &models.UserLoveFlag{}
  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 *UserLoveFlagRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.UserLoveFlag) (*domain.UserLoveFlag, error) {
  37 + var (
  38 + err error
  39 + m *models.UserLoveFlag
  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 *UserLoveFlagRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.UserLoveFlag) (*domain.UserLoveFlag, error) {
  56 + var (
  57 + err error
  58 + m *models.UserLoveFlag
  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 *UserLoveFlagRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.UserLoveFlag) (*domain.UserLoveFlag, error) {
  80 + var (
  81 + tx = conn.DB()
  82 + m = &models.UserLoveFlag{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 *UserLoveFlagRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.UserLoveFlag, error) {
  95 + var (
  96 + err error
  97 + tx = conn.DB()
  98 + m = new(models.UserLoveFlag)
  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.UserLoveFlag)
  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 *UserLoveFlagRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.UserLoveFlag, error) {
  116 + var (
  117 + tx = conn.DB()
  118 + ms []*models.UserLoveFlag
  119 + dms = make([]*domain.UserLoveFlag, 0)
  120 + total int64
  121 + )
  122 + queryFunc := func() (interface{}, error) {
  123 + tx = tx.Model(&ms).Order("id desc")
  124 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  125 + return dms, tx.Error
  126 + }
  127 + return dms, nil
  128 + }
  129 +
  130 + if _, err := repository.Query(queryFunc); err != nil {
  131 + return 0, nil, err
  132 + }
  133 +
  134 + for _, item := range ms {
  135 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  136 + return 0, dms, err
  137 + } else {
  138 + dms = append(dms, dm)
  139 + }
  140 + }
  141 + return total, dms, nil
  142 +}
  143 +
  144 +func (repository *UserLoveFlagRepository) ModelToDomainModel(from *models.UserLoveFlag) (*domain.UserLoveFlag, error) {
  145 + to := &domain.UserLoveFlag{}
  146 + err := copier.Copy(to, from)
  147 + return to, err
  148 +}
  149 +
  150 +func (repository *UserLoveFlagRepository) DomainModelToModel(from *domain.UserLoveFlag) (*models.UserLoveFlag, error) {
  151 + to := &models.UserLoveFlag{}
  152 + err := copier.Copy(to, from)
  153 + return to, err
  154 +}
  155 +
  156 +func NewUserLoveFlagRepository(cache *cache.CachedRepository) domain.UserLoveFlagRepository {
  157 + return &UserLoveFlagRepository{CachedRepository: cache}
  158 +}
@@ -22,7 +22,7 @@ type ArticleSection struct { @@ -22,7 +22,7 @@ type ArticleSection struct {
22 22
23 type ArticleSectionRepository interface { 23 type ArticleSectionRepository interface {
24 Insert(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error) 24 Insert(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error)
25 - Update(ctx context.Context, conn transaction.Conn, dm *Article) (*ArticleSection, error) 25 + Update(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error)
26 Delete(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error) 26 Delete(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error)
27 FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleSection, error) 27 FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleSection, error)
28 Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleSection, error) 28 Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleSection, error)
1 package domain 1 package domain
2 2
  3 +import (
  4 + "context"
  5 +
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  7 +)
  8 +
3 // 人员点赞标记 9 // 人员点赞标记
4 10
5 type UserLoveFlag struct { 11 type UserLoveFlag struct {
@@ -12,3 +18,11 @@ type UserLoveFlag struct { @@ -12,3 +18,11 @@ type UserLoveFlag struct {
12 DeletedAt int64 `json:"deletedAt,omitempty"` 18 DeletedAt int64 `json:"deletedAt,omitempty"`
13 Version int `json:"version,omitempty"` 19 Version int `json:"version,omitempty"`
14 } 20 }
  21 +type UserLoveFlagRepository interface {
  22 + Insert(ctx context.Context, conn transaction.Conn, dm *UserLoveFlag) (*UserLoveFlag, error)
  23 + Update(ctx context.Context, conn transaction.Conn, dm *UserLoveFlag) (*UserLoveFlag, error)
  24 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *UserLoveFlag) (*UserLoveFlag, error)
  25 + Delete(ctx context.Context, conn transaction.Conn, dm *UserLoveFlag) (*UserLoveFlag, error)
  26 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*UserLoveFlag, error)
  27 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*UserLoveFlag, error)
  28 +}