作者 tangxvhui

更新模型

package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type Article struct {
Id int64 // 唯一标识
CompanyId int64 `json:"companyId"`
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
AuthorId int64 `json:"authorId"` // 发布人
Author domain.User `json:"author"` // 发布人
Title string `json:"title"` // 文章标题
Images []domain.Image `json:"images"` // 图片
WhoRead []int64 `json:"whoRead"` // 谁可以看
WhoReview []int64 `json:"whoReview"` // 评论人
Location domain.Location `json:"location"` // 坐标
TargetUser int `json:"targetUser"` // 分发方式 0 分发给所有人 1 分发给指定的人
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
Tags []int `json:"tags"` // 标签
Show int `json:"showState"` // 评论的展示状态(0显示、1不显示)
}
func (m *Article) TableName() string {
return "article"
}
func (m *Article) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *Article) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *Article) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *Article) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*Article); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *Article) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type ArticleBackup struct {
Id int64 // 唯一标识
Version int
}
func (m *ArticleBackup) TableName() string {
return "article_backup"
}
func (m *ArticleBackup) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleBackup) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleBackup) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ArticleBackup) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ArticleBackup); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ArticleBackup) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type ArticleComment struct {
Id int64 // 唯一标识
Version int
}
func (m *ArticleComment) TableName() string {
return "article_comment"
}
func (m *ArticleComment) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleComment) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleComment) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ArticleComment) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ArticleComment); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ArticleComment) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type ArticleDraft struct {
Id int64 // 唯一标识
Version int
}
func (m *ArticleDraft) TableName() string {
return "article_draft"
}
func (m *ArticleDraft) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleDraft) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleDraft) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ArticleDraft) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ArticleDraft); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ArticleDraft) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type ArticleSection struct {
Id int64 // 唯一标识
Version int
}
func (m *ArticleSection) TableName() string {
return "article_section"
}
func (m *ArticleSection) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleSection) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleSection) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ArticleSection) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ArticleSection); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ArticleSection) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type UserLoveFlag struct {
Id int64 // 唯一标识
Version int
}
func (m *UserLoveFlag) TableName() string {
return "user_love_flag"
}
func (m *UserLoveFlag) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *UserLoveFlag) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *UserLoveFlag) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *UserLoveFlag) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*UserLoveFlag); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *UserLoveFlag) 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 ArticleBackupRepository struct {
*cache.CachedRepository
}
func (repository *ArticleBackupRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleBackup) (*domain.ArticleBackup, error) {
var (
err error
m = &models.ArticleBackup{}
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 *ArticleBackupRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleBackup) (*domain.ArticleBackup, error) {
var (
err error
m *models.ArticleBackup
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 *ArticleBackupRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleBackup) (*domain.ArticleBackup, error) {
var (
err error
m *models.ArticleBackup
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 *ArticleBackupRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleBackup) (*domain.ArticleBackup, error) {
var (
tx = conn.DB()
m = &models.ArticleBackup{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 *ArticleBackupRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleBackup, error) {
var (
err error
tx = conn.DB()
m = new(models.ArticleBackup)
)
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.ArticleBackup)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleBackupRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleBackup, error) {
var (
tx = conn.DB()
ms []*models.ArticleBackup
dms = make([]*domain.ArticleBackup, 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 *ArticleBackupRepository) ModelToDomainModel(from *models.ArticleBackup) (*domain.ArticleBackup, error) {
to := &domain.ArticleBackup{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ArticleBackupRepository) DomainModelToModel(from *domain.ArticleBackup) (*models.ArticleBackup, error) {
to := &models.ArticleBackup{}
err := copier.Copy(to, from)
return to, err
}
func NewArticleBackupRepository(cache *cache.CachedRepository) domain.ArticleBackupRepository {
return &ArticleBackupRepository{CachedRepository: cache}
}
... ...
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 ArticleCommentRepository struct {
*cache.CachedRepository
}
func (repository *ArticleCommentRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleComment) (*domain.ArticleComment, error) {
var (
err error
m = &models.ArticleComment{}
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 *ArticleCommentRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleComment) (*domain.ArticleComment, error) {
var (
err error
m *models.ArticleComment
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 *ArticleCommentRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleComment) (*domain.ArticleComment, error) {
var (
err error
m *models.ArticleComment
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 *ArticleCommentRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleComment) (*domain.ArticleComment, error) {
var (
tx = conn.DB()
m = &models.ArticleComment{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 *ArticleCommentRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleComment, error) {
var (
err error
tx = conn.DB()
m = new(models.ArticleComment)
)
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.ArticleComment)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleCommentRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleComment, error) {
var (
tx = conn.DB()
ms []*models.ArticleComment
dms = make([]*domain.ArticleComment, 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 *ArticleCommentRepository) ModelToDomainModel(from *models.ArticleComment) (*domain.ArticleComment, error) {
to := &domain.ArticleComment{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ArticleCommentRepository) DomainModelToModel(from *domain.ArticleComment) (*models.ArticleComment, error) {
to := &models.ArticleComment{}
err := copier.Copy(to, from)
return to, err
}
func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository {
return &ArticleCommentRepository{CachedRepository: cache}
}
... ...
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 ArticleDraftRepository struct {
*cache.CachedRepository
}
func (repository *ArticleDraftRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleDraft) (*domain.ArticleDraft, error) {
var (
err error
m = &models.ArticleDraft{}
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 *ArticleDraftRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleDraft) (*domain.ArticleDraft, error) {
var (
err error
m *models.ArticleDraft
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 *ArticleDraftRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleDraft) (*domain.ArticleDraft, error) {
var (
err error
m *models.ArticleDraft
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 *ArticleDraftRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleDraft) (*domain.ArticleDraft, error) {
var (
tx = conn.DB()
m = &models.ArticleDraft{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 *ArticleDraftRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleDraft, error) {
var (
err error
tx = conn.DB()
m = new(models.ArticleDraft)
)
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.ArticleDraft)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleDraftRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleDraft, error) {
var (
tx = conn.DB()
ms []*models.ArticleDraft
dms = make([]*domain.ArticleDraft, 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 *ArticleDraftRepository) ModelToDomainModel(from *models.ArticleDraft) (*domain.ArticleDraft, error) {
to := &domain.ArticleDraft{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ArticleDraftRepository) DomainModelToModel(from *domain.ArticleDraft) (*models.ArticleDraft, error) {
to := &models.ArticleDraft{}
err := copier.Copy(to, from)
return to, err
}
func NewArticleDraftRepository(cache *cache.CachedRepository) domain.ArticleDraftRepository {
return &ArticleDraftRepository{CachedRepository: cache}
}
... ...
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 ArticleRepository struct {
*cache.CachedRepository
}
func (repository *ArticleRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
var (
err error
m = &models.Article{}
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 *ArticleRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
var (
err error
m *models.Article
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 *ArticleRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Article) (*domain.Article, error) {
var (
err error
m *models.Article
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 *ArticleRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Article) (*domain.Article, error) {
var (
tx = conn.DB()
m = &models.Article{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 *ArticleRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Article, error) {
var (
err error
tx = conn.DB()
m = new(models.Article)
)
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.Article)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Article, error) {
var (
tx = conn.DB()
ms []*models.Article
dms = make([]*domain.Article, 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 *ArticleRepository) ModelToDomainModel(from *models.Article) (*domain.Article, error) {
to := &domain.Article{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ArticleRepository) DomainModelToModel(from *domain.Article) (*models.Article, error) {
to := &models.Article{}
err := copier.Copy(to, from)
return to, err
}
func NewArticleRepository(cache *cache.CachedRepository) domain.ArticleRepository {
return &ArticleRepository{CachedRepository: cache}
}
... ...
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 ArticleSectionRepository struct {
*cache.CachedRepository
}
func (repository *ArticleSectionRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleSection) (*domain.ArticleSection, error) {
var (
err error
m = &models.ArticleSection{}
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 *ArticleSectionRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleSection) (*domain.ArticleSection, error) {
var (
err error
m *models.ArticleSection
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 *ArticleSectionRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleSection) (*domain.ArticleSection, error) {
var (
err error
m *models.ArticleSection
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 *ArticleSectionRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleSection) (*domain.ArticleSection, error) {
var (
tx = conn.DB()
m = &models.ArticleSection{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 *ArticleSectionRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleSection, error) {
var (
err error
tx = conn.DB()
m = new(models.ArticleSection)
)
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.ArticleSection)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleSectionRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ArticleSection, error) {
var (
tx = conn.DB()
ms []*models.ArticleSection
dms = make([]*domain.ArticleSection, 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 *ArticleSectionRepository) ModelToDomainModel(from *models.ArticleSection) (*domain.ArticleSection, error) {
to := &domain.ArticleSection{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ArticleSectionRepository) DomainModelToModel(from *domain.ArticleSection) (*models.ArticleSection, error) {
to := &models.ArticleSection{}
err := copier.Copy(to, from)
return to, err
}
func NewArticleSectionRepository(cache *cache.CachedRepository) domain.ArticleSectionRepository {
return &ArticleSectionRepository{CachedRepository: cache}
}
... ...
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 UserLoveFlagRepository struct {
*cache.CachedRepository
}
func (repository *UserLoveFlagRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.UserLoveFlag) (*domain.UserLoveFlag, error) {
var (
err error
m = &models.UserLoveFlag{}
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 *UserLoveFlagRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.UserLoveFlag) (*domain.UserLoveFlag, error) {
var (
err error
m *models.UserLoveFlag
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 *UserLoveFlagRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.UserLoveFlag) (*domain.UserLoveFlag, error) {
var (
err error
m *models.UserLoveFlag
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 *UserLoveFlagRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.UserLoveFlag) (*domain.UserLoveFlag, error) {
var (
tx = conn.DB()
m = &models.UserLoveFlag{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 *UserLoveFlagRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.UserLoveFlag, error) {
var (
err error
tx = conn.DB()
m = new(models.UserLoveFlag)
)
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.UserLoveFlag)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserLoveFlagRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.UserLoveFlag, error) {
var (
tx = conn.DB()
ms []*models.UserLoveFlag
dms = make([]*domain.UserLoveFlag, 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 *UserLoveFlagRepository) ModelToDomainModel(from *models.UserLoveFlag) (*domain.UserLoveFlag, error) {
to := &domain.UserLoveFlag{}
err := copier.Copy(to, from)
return to, err
}
func (repository *UserLoveFlagRepository) DomainModelToModel(from *domain.UserLoveFlag) (*models.UserLoveFlag, error) {
to := &models.UserLoveFlag{}
err := copier.Copy(to, from)
return to, err
}
func NewUserLoveFlagRepository(cache *cache.CachedRepository) domain.UserLoveFlagRepository {
return &UserLoveFlagRepository{CachedRepository: cache}
}
... ...
... ... @@ -22,7 +22,7 @@ type ArticleSection struct {
type ArticleSectionRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error)
Update(ctx context.Context, conn transaction.Conn, dm *Article) (*ArticleSection, error)
Update(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleSection, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleSection, error)
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
// 人员点赞标记
type UserLoveFlag struct {
... ... @@ -12,3 +18,11 @@ type UserLoveFlag struct {
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
}
type UserLoveFlagRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *UserLoveFlag) (*UserLoveFlag, error)
Update(ctx context.Context, conn transaction.Conn, dm *UserLoveFlag) (*UserLoveFlag, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *UserLoveFlag) (*UserLoveFlag, error)
Delete(ctx context.Context, conn transaction.Conn, dm *UserLoveFlag) (*UserLoveFlag, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*UserLoveFlag, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*UserLoveFlag, error)
}
... ...